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

}}

相关推荐