xml <?xml version="1.0" encoding="utf-8"?><android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:too ...
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("狀態改變了......");
// }
// });
}
}