Android DrawLayout結(jié)合ListView用法實(shí)例
想做一個(gè)APP,設(shè)計(jì)中有側(cè)邊欄這個(gè)功能,所以現(xiàn)在開始學(xué)習(xí)下側(cè)邊欄的實(shí)現(xiàn)。
在官方的UI空間中已經(jīng)給出了DrawerLayout這個(gè)側(cè)滑的菜單空間。
因?yàn)樵谑褂肈rawerLayout的時(shí)候遇到了些問(wèn)題,花了一天是時(shí)間才搞定,這里來(lái)記錄一下,免得到時(shí)候自己在掉坑里。
1.主布局一定要是DrawerLayout。
2.側(cè)欄拉出來(lái)時(shí),要點(diǎn)擊空白欄關(guān)閉側(cè)欄的話,一定要把空白欄設(shè)置為FrameLayout
先上個(gè)效果圖吧:
好了,上代碼:
activity_main.xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <!-- 內(nèi)容欄--> <FrameLayout android:id="@+id/ly_content" android:layout_width="match_parent" android:layout_height="match_parent" /> <!-- 側(cè)滑欄--> <ListView android:id="@+id/list_left_drawer" android:layout_width="300dp" android:layout_height="match_parent" android:layout_gravity="start" android:background="#fff000" android:choiceMode="singleChoice" android:divider="#FFFFFF" android:dividerHeight="1dp" /> </android.support.v4.widget.DrawerLayout>
listView里面的布局 item_list.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto"> <!-- 定義一個(gè)用于顯示頭像的ImageView --> <ImageView android:id="@+id/imgtou" android:layout_width="64dp" android:layout_height="64dp" android:baselineAlignBottom="true" android:paddingLeft="8dp" /> <!-- 定義一個(gè)豎直方向的LinearLayout,把QQ呢稱與說(shuō)說(shuō)的文本框設(shè)置出來(lái) --> <LinearLayout android:id="@+id/new_line" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="8px" android:textColor="#1D1D1C" android:textSize="20sp" /> <TextView android:id="@+id/says" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="8px" android:textColor="#B4B4B9" android:textSize="14sp" /> <TextView android:id="@+id/time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="8px" android:textColor="#B4B4B9" android:textSize="14sp" /> </LinearLayout> </LinearLayout>
主程序MainActivity.java
package action.sun.com.testdraw2; import android.support.v4.app.FragmentManager; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.Toast; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener{ private String[] names = new String[]{"Tom", "Jack", "Json"}; private String[] says = new String[]{"111111,2222222", "33333333~", "444444444~"}; private String[] times = new String[]{"1天前", "3天前~", "2天前~"}; private int[] imgIds = new int[]{R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher}; private DrawerLayout drawer_layout; private ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.d("1", "onCreate: xxxxxxxxxxxxxxx"); drawer_layout = (DrawerLayout) findViewById(R.id.drawer_layout); List<Map<String, Object>> listitem = new ArrayList<Map<String, Object>>(); for (int i = 0; i < names.length; i++) { Map<String, Object> showitem = new HashMap<String, Object>(); showitem.put("touxiang", imgIds[i]); showitem.put("name", names[i]); showitem.put("says", says[i]); showitem.put("time", times[i]); listitem.add(showitem); } //創(chuàng)建一個(gè)simpleAdapter SimpleAdapter myAdapter = new SimpleAdapter(getApplicationContext(), listitem, R.layout.item_list, new String[]{"touxiang", "name", "says","time"}, new int[]{R.id.imgtou, R.id.name, R.id.says, R.id.time}); //ListView 容器 listView = (ListView) findViewById(R.id.list_left_drawer); listView.setAdapter(myAdapter); listView.setOnItemClickListener(this); } //點(diǎn)擊Item 顯示在幀頁(yè)面選擇的Item值 @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(MainActivity.this, "提示的內(nèi)容", Toast.LENGTH_LONG).show(); //關(guān)閉 側(cè)邊欄 drawer_layout.closeDrawer(listView); } }
到了現(xiàn)在,代碼完了。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android側(cè)滑菜單之DrawerLayout用法詳解
- Android布局控件DrawerLayout實(shí)現(xiàn)完美側(cè)滑效果
- Android使用DrawerLayout仿QQ6.0雙側(cè)滑菜單
- Android側(cè)滑菜單控件DrawerLayout使用詳解
- Android原生側(cè)滑控件DrawerLayout使用方法詳解
- Android中DrawerLayout實(shí)現(xiàn)側(cè)滑菜單效果
- Android官方的側(cè)滑控件DrawerLayout的示例代碼
- Android中DrawerLayout+ViewPager滑動(dòng)沖突的解決方法
- Android DrawerLayout實(shí)現(xiàn)側(cè)拉菜單功能
相關(guān)文章
Android學(xué)習(xí)教程之九宮格圖片展示(13)
這篇文章主要為大家詳細(xì)介紹了Android學(xué)習(xí)教程之九宮格圖片展示代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11Android編程解析Json格式數(shù)據(jù)的方法
這篇文章主要介紹了Android編程解析Json格式數(shù)據(jù)的方法,涉及Android中json格式數(shù)據(jù)的構(gòu)造、讀取及遍歷等技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11從0快速搭建一個(gè)實(shí)用的MVVM框架(超詳細(xì))
這篇文章主要介紹了從0搭建一個(gè)實(shí)用的MVVM框架,結(jié)合Jetpack,構(gòu)建快速開發(fā)的MVVM框架,支持快速生成ListActivity、ListFragment,主要是基于MVVM進(jìn)行快速開發(fā)上手即用,需要的朋友可以參考下2022-03-03Android SQLite數(shù)據(jù)庫(kù)增刪改查操作的案例分析
本篇文章介紹了,在Android中SQLite數(shù)據(jù)庫(kù)增刪改查操作的案例分析,需要的朋友參考下2013-04-04android okhttp的基礎(chǔ)使用【入門推薦】
本文主要總結(jié)了Android著名網(wǎng)絡(luò)框架-okhttp的基礎(chǔ)使用。具有一定的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-01-01Android百度地圖應(yīng)用之基本地圖功能實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了Android百度地圖應(yīng)用之基本地圖功能實(shí)現(xiàn)的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-06-06Android編程自定義搜索框?qū)崿F(xiàn)方法【附demo源碼下載】
這篇文章主要介紹了Android編程自定義搜索框?qū)崿F(xiàn)方法,涉及Android界面布局、數(shù)據(jù)加載、事件響應(yīng)等相關(guān)操作技巧,并附帶完整demo源碼供讀者下載參考,需要的朋友可以參考下2017-12-12Android實(shí)現(xiàn)網(wǎng)易新聞客戶端首頁(yè)效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)網(wǎng)易新聞客戶端首頁(yè)效果的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11Flutter持久化存儲(chǔ)之?dāng)?shù)據(jù)庫(kù)存儲(chǔ)(sqflite)詳解
這篇文章主要給大家介紹了關(guān)于Flutter持久化存儲(chǔ)之?dāng)?shù)據(jù)庫(kù)存儲(chǔ)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用Flutter具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03