码迷,mamicode.com
首页 > 其他好文 > 详细

Drawerlayout侧拉菜单

时间:2017-09-13 09:51:10      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:sch   slide   start   tool   save   book   demo   emc   void   

 

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/mydraw"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.bwie.drawerlayoutdemo.MainActivity">

 

<ListView
android:id="@+id/lv"
android:layout_width="240sp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#f00"
android:dividerHeight="1dp"
android:entries="@array/books"
android:background="#fff"
/>


</android.support.v4.widget.DrawerLayout>

代码

public class MainActivity extends AppCompatActivity {
private DrawerLayout drawerLayout;
private Button openDraw;
private ListView lv;
ActionBarDrawerToggle mDrawerToggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

init();
}

private void init() {
drawerLayout = (DrawerLayout) findViewById(R.id.mydraw);
openDraw = (Button) findViewById(R.id.openDraw);
lv = (ListView) findViewById(R.id.lv);
//点击 按钮 打开侧滑
openDraw.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
drawerLayout.openDrawer(lv);//打开侧滑

}
});
//点击lv的条目,关闭侧滑,在主界面显示相关详细
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
drawerLayout.closeDrawer(lv);//关闭侧滑
//改变主界面
String str = parent.getItemAtPosition(position).toString();
openDraw.setText(str);
}
});
//drawerlayout侧滑的监听
// drawerLayout.setDrawerListener(new DrawerLayout.DrawerListener() {
// @Override
// public void onDrawerSlide(View drawerView, float slideOffset) {
// Log.d("main","onDrawerSlide........");
// openDraw.setText("在滑动......");
// }
//
// @Override
// public void onDrawerOpened(View drawerView) {
// Log.d("main","onDrawerOpened........");
// openDraw.setText("打开了......");
// }
//
// @Override
// public void onDrawerClosed(View drawerView) {
// Log.d("main","onDrawerClosed........");
// openDraw.setText("关闭了......");
// }
//
// @Override
// public void onDrawerStateChanged(int newState) {
// Log.d("main","onDrawerStateChanged........");
// openDraw.setText("状态改变了......");
// }
// });

 

}
}

 

Drawerlayout侧拉菜单

标签:sch   slide   start   tool   save   book   demo   emc   void   

原文地址:http://www.cnblogs.com/zihang814/p/7513401.html

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