Android抽屉菜单SlidingDrawer 简单使用
2018-07-20 来源:open-open
<!-- 需要设置SlidingDrawer的handle和content的id -->
<SlidingDrawer xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/slidingdrawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:content="@+id/content"
android:handle="@+id/handle"
android:orientation="horizontal" >
<!-- 设置handle,来处理滑动抽屉 -->
<ImageButton
android:id="@id/handle"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/ic_launcher" />
<!-- 设置内容View -->
<RelativeLayout
android:id="@id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F0F0F0" >
<TextView
android:id="@+id/show_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:text="简单抽屉效果"
android:textColor="#333333"
android:textSize="18sp" />
<ListView
android:id="@+id/lv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/show_tv" >
</ListView>
</RelativeLayout>
</SlidingDrawer>
--------------Activity代码 -----------
public class MainActivity extends Activity {
private SlidingDrawer mSlidDrawer;
private ImageButton mImageBtn;
private TextView mShowTv;
private List<String> dataList;
private ListView mLv;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
initDatas();
initDrawerListeners();
}
@SuppressWarnings("deprecation")
private void initDrawerListeners() {
mSlidDrawer.setOnDrawerOpenListener(new SlidingDrawer.OnDrawerOpenListener() {
public void onDrawerOpened() {
mImageBtn.setImageResource(R.drawable.a1);
}
});
mSlidDrawer.setOnDrawerCloseListener(new SlidingDrawer.OnDrawerCloseListener() {
public void onDrawerClosed() {
mImageBtn.setImageResource(R.drawable.ic_launcher);
}
});
mSlidDrawer.setOnDrawerScrollListener(new SlidingDrawer.OnDrawerScrollListener() {
public void onScrollEnded() {
mShowTv.setText("滑动打开");
mLv.setAdapter(new DrawerAdpater(getApplicationContext(), dataList));
}
public void onScrollStarted() {
mShowTv.setText("滑动隐藏");
}
});
mLv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mShowTv.setText(dataList.get(position));
}
});
}
private void initDatas() {
dataList = new ArrayList<String>();
for (int i = 0; i <= 6; i++) {
dataList.add("抽屉菜单第" + i + "层");
}
}
private void initViews() {
mImageBtn = (ImageButton) findViewById(R.id.handle);
mSlidDrawer = (SlidingDrawer) findViewById(R.id.slidingdrawer);
mShowTv = (TextView) findViewById(R.id.show_tv);
mLv = (ListView) findViewById(R.id.lv);
}
}
--------------Activity中ListView的适配器DrawerAdpater ---------
public class DrawerAdpater extends BaseAdapter {List<String> list;
LayoutInflater inflater;
public MyAdpater(Context context, List<String> list) {
this.list = list;
this.inflater = LayoutInflater.from(context);
}
public int getCount() {
return list.size();
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.item, null);
}
TextView textView = (TextView) convertView.findViewById(R.id.text);
textView.setText(list.get(position));
return convertView;
}
@Override
public Object getItem(int position) {
return list.get(position);
}
}
标签: 代码
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。