文/程序員男神
前言
精力有限棵譬,分享無限显蝌。作為一個(gè)還在學(xué)習(xí)他人代碼的碼農(nóng),深知分享的好處订咸。行走在別人博客路上的拾荒者曼尊,慢慢學(xué)會(huì)自己積累。最近有點(diǎn)忙啊脏嚷,一直在趕項(xiàng)目的路上骆撇,突然想高唱一首歌放松一下,一人我編程累......
概述
先上需求圖:DrawerLayout側(cè)滑菜單的實(shí)現(xiàn)父叙。
實(shí)現(xiàn)的功能細(xì)節(jié):
Drawerlayout是Android v4 包里自帶的神郊,既然是自帶的那么直接拿來用就可以了,當(dāng)然前提是你得工程里有 v4 包趾唱,下面解釋上面的布局文件涌乳,讓你懂得Drawerlayout用法。
首先Drawerlayout支持左劃和右劃甜癞,那它是如何控制的呢夕晓?
慢慢告訴你,以上布局分為三部分悠咱,一般情況下蒸辆,第一部分是主步局,第二部分是左劃的布局析既,第三部分是右劃的布局躬贡,其實(shí)這里的左向滑動(dòng)和右向滑動(dòng)是通過gravity控制,左劃界面android:layout_gravity="left" 當(dāng)然這里的left也可以用start代替眼坏,右劃界面就理所當(dāng)然的是android:layout_gravity="right" 拂玻,同樣right也可以用end代替,其余的應(yīng)該明白了吧!
注意:drawerlayout實(shí)現(xiàn)的側(cè)滑,側(cè)滑部分不響應(yīng)點(diǎn)擊事件纺讲,點(diǎn)擊之后只是側(cè)滑欄收進(jìn)去擂仍,button捕獲不到點(diǎn)擊事件。
解決辦法:http://bbs.csdn.net/topics/391918297
drawerlayout必須放在主界面布局后面熬甚。
實(shí)現(xiàn)步驟:
這篇文章是借著自定義TopBar內(nèi)容的基礎(chǔ)上實(shí)現(xiàn)的......
直接上代碼:activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include layout="@layout/layout_topbars" />
<include
android:id="@+id/drawer_include"
layout="@layout/layout_drawer" />
</android.support.v4.widget.DrawerLayout>
layout_topbars.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.example.djj.drawerlayout.view.TopBar
android:id="@+id/topbar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/colorPrimaryDark"
app:leftBackground="@drawable/right_button_selector"
app:rightBackground="@drawable/left_button_selector"
app:titleText="我的首頁"
app:titleTextColor="#FFF"
app:titleTextSize="6sp"/>
<ListView
android:id="@+id/progress_list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
layout_drawer.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:layout_marginRight="56dp"
android:background="#FFF"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="36dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="36dp"
android:src="@mipmap/zy_cd_ys" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:orientation="vertical">
<TextView
android:id="@+id/tv_menu_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="啦啦啦(5463891)" />
<TextView
android:id="@+id/tv_menu_area"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="14dp"
android:text="xxx病區(qū)"
android:textColor="#FFF"
android:textSize="15sp" />
</LinearLayout>
</LinearLayout>
<ListView
android:id="@+id/left_listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@null"
android:scrollbars="none" />
</LinearLayout>
以上都是一些布局代碼逢渔,一些資源文件后面有傳送門。
接下來準(zhǔn)備我們的適配器adapter的代碼:
/**
* desc: 側(cè)滑菜單的Adapter
* author: dj
* date: 2017/3/30 15:10
*/
public class ContentAdapter extends BaseAdapter {
private Context context;
private List<ContentModel> list;
public ContentAdapter(Context context, List<ContentModel> list) {
super();
this.context = context;
this.list = list;
}
@Override
public int getCount() {
if (list != null) {
return list.size();
}
return 0;
}
@Override
public Object getItem(int position) {
if (list != null) {
return list.get(position);
}
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHold hold;
if (convertView == null) {
hold = new ViewHold();
convertView = LayoutInflater.from(context).inflate(
R.layout.content_item, null);
convertView.setTag(hold);
} else {
hold = (ViewHold) convertView.getTag();
}
hold.imageView = (ImageView) convertView.findViewById(R.id.item_imageview);
hold.textView = (TextView) convertView.findViewById(R.id.item_textview);
hold.imageView.setImageResource(list.get(position).getImageView());
hold.textView.setText(list.get(position).getText());
return convertView;
}
static class ViewHold {
public ImageView imageView;
public TextView textView;
}
}
接下來就是一個(gè)它的content_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="10dp">
<ImageView
android:id="@+id/item_imageview"
android:layout_marginLeft="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/item_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:text="aa"
android:textSize="20dp" />
</LinearLayout>
以及它的實(shí)體類:
/**
* desc: item實(shí)體類
* author: dj
* date: 2017/3/30 15:13
*/
public class ContentModel {
private int imageView;
private String text;
public ContentModel(int imageView, String text) {
super();
this.imageView = imageView;
this.text = text;
}
public int getImageView() {
return imageView;
}
public void setImageView(int imageView) {
this.imageView = imageView;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
最后就是我們綁定數(shù)據(jù)乡括,MainActivity的代碼:
/**
* desc: MainActivty
* author: dj
* date: 2017/3/30 15:17
*/
public class MainActivity extends Activity {
private DrawerLayout drawerLayout;
private List<ContentModel> list;
private ContentAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
View view = findViewById(R.id.drawer_include);
ListView listView = (ListView) view.findViewById(R.id.left_listview);
initData();
adapter = new ContentAdapter(this, list);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position == 0) {
Toast.makeText(MainActivity.this, "你點(diǎn)擊的" + position + 9, Toast.LENGTH_SHORT).show();
}
Toast.makeText(MainActivity.this, "你點(diǎn)擊的" + position, Toast.LENGTH_SHORT).show();
}
});
TopBar topBar = (TopBar) findViewById(R.id.topbar);
topBar.setOnLeftAndRightClickListener(new TopBar.OnLeftAndRightClickListener() {
@Override
public void OnLeftButtonClick() {
drawerLayout.openDrawer(GravityCompat.START);
}
@Override
public void OnRightButtonClick() {
}
});
}
private void initData() {
list = new ArrayList<ContentModel>();
list.add(new ContentModel(R.drawable.doctoradvice2, "新聞"));
list.add(new ContentModel(R.drawable.infusion_selected, "訂閱"));
list.add(new ContentModel(R.drawable.mypatient_selected, "圖片"));
list.add(new ContentModel(R.drawable.mywork_selected, "視頻"));
list.add(new ContentModel(R.drawable.nursingcareplan2, "跟帖"));
list.add(new ContentModel(R.drawable.personal_selected, "投票"));
}
}
這樣我們就可以對側(cè)滑菜單的item進(jìn)行監(jiān)聽了肃廓,不需要用Fragment也能實(shí)現(xiàn)側(cè)滑的功能,說實(shí)話很討厭使用Fragment诲泌。
歡迎轉(zhuǎn)載盲赊,但最好請注明文章原始出處。