Drawerlayout侧拉菜单
Posted 资深程序猿
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Drawerlayout侧拉菜单相关的知识,希望对你有一定的参考价值。
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侧拉菜单的主要内容,如果未能解决你的问题,请参考以下文章
ToolBar和DrawerLayout的使用实现侧拉栏抽屉的开闭