Drawerlayout侧拉菜单_移动开发_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > 移动开发 > Drawerlayout侧拉菜单

Drawerlayout侧拉菜单

 2017/9/13 11:13:39  资深程序猿——  程序员俱乐部  我要评论(0)
  • 摘要:xml<?xmlversion="1.0"encoding="utf-8"?><android.support.v4.widget.DrawerLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/mydraw"android
  • 标签:菜单

 

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("状态改变了......");
// }
// });

 

}
}

 

上一篇: 黄牛称iPhone 8订单火爆 预计炒至2万元起步 下一篇: 没有下一篇了!
发表评论
用户名: 匿名