码迷,mamicode.com
首页 > 移动开发 > 详细

Android抽屉菜单SlidingDrawer 简单使用

时间:2015-04-22 11:38:22      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:android开发

<?xml version="1.0" encoding="utf-8"?>
<!-- 需要设置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);
}
}

Android抽屉菜单SlidingDrawer 简单使用

标签:android开发

原文地址:http://blog.csdn.net/true100/article/details/45192271

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!