การ onClickListener บน Drawer Menu

ตามโจทย์เลย หลังจากที่เราเรียนรู้ในการสร้าง Hamberger Icon บน Drawer ไปแล้ว ที่นี่เราจะต่อยอด โดยการสร้าง TextView บน Drawer และ onClickListener ไปที่ TextView เพื่อ Replace Fragment บน Main Activity กัน

แบบนี่ มะ เรามาเริ่มกัน มาสเตอร์​ขออนุญาติ ย่อนะ เราจะเริ่มจาก เรามี Habmerger Icon และ Deawer Menu แล้ว ใครไม่ทันไป ดูที่นี่ มาสเตอร์ เร่ิมสร้าง Fragment ที่ สอง โดยการสร้าง Layout และ Class

และ เพิ่ม TextView ไปที่ Drawer Menu ที่อยู่ บน activity_main.xml แบบนี้

มาสเตอร์ สร้าง เมธอด mainController() เก็บ Event ที่คลิกที่ TextView Main โดยกำหนด ให้ Replace Fragment ด้วย MainFragment ที่ตำแหน่ง R.id.fragmentContent

สร้าง secondController() เก็บเหตุการณ์ ที่คลิก Second บน Drawer Menu นอกจากจะ Replace Fragment ด้วย SecondFragment แล้ว เราต้อง Remove MainFragment ไม่งั้นจะเกิดการ ซ้อนของ 2 Fragment ครับ เสร็จแล้วลองทดสอบ

เรียบร้อยโรงเรียน มาเตอร์ สำหรับ ใครที่ไม่ทัน ให้โหลด Source Code จาก https://github.com/masterUNG/HambergerIcon มาลองได้ครับ

มาสเตอร์ อึ่ง

จงอย่าคิดที่จะเดิน จงเดินเลย

)

มาสเตอร์ อึ่ง

Written by

จงอย่าคิดจะเดิน จงเดินเลย สโลแกน เตือนใจ ของ มาสเตอร์ อึ่ง เสมอมามาสเตอร์ เขียนแอนดรอยด์ และ iOS มาแยะ เลยอยากแชร์ อะไร ? ที่ง่ายๆ สำหรับผู้เริ่มต้น ครับ

มาสเตอร์ อึ่ง

จงอย่าคิดที่จะเดิน จงเดินเลย

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade