标签:android开发
<?xml version="1.0" encoding="utf-8"?></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开发
原文地址:http://blog.csdn.net/true100/article/details/45192271