private DrawerLayout drawerLayout;
private ListView mLv;
private LinearLayout ll;
private GridView gv;
String[] tyeps = {"手机","电脑","汽车"};
String[] phones = {"小米1","小米2","小米3","小米4","小米5"};
String[] computers = {"联想","dell","mac","小米","华为"};
String[] cars = {"凯迪拉克","宝马x3","奔驰c300","哈佛6","奇骏","宝骏560"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
drawerLayout = (DrawerLayout) findViewById(R.id.activity_main);
mLv = (ListView) findViewById(R.id.lv);
ll = (LinearLayout) findViewById(R.id.ll);
gv = (GridView) findViewById(R.id.gv);
drawerLayout.setDrawerListener(new DrawerLayout.DrawerListener() {
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
}
@Override
public void onDrawerOpened(View drawerView) {
}
@Override
public void onDrawerClosed(View drawerView) {
}
@Override
public void onDrawerStateChanged(int newState) {
}
});
//listview配置数据
ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,tyeps);
mLv.setAdapter(myAdapter);
mLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Toast.makeText(MainActivity.this,tyeps[position],Toast.LENGTH_SHORT).show();
//操作完了后,关闭侧滑
drawerLayout.closeDrawer(ll);
if(position == 0){
ArrayAdapter<String> myAdapterGv = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,phones);
gv.setAdapter(myAdapterGv);
}else if(position == 1){
ArrayAdapter<String> myAdapterGv = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,computers);
gv.setAdapter(myAdapterGv);
}else{
ArrayAdapter<String> myAdapterGv = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,cars);
gv.setAdapter(myAdapterGv);
}
}
});
}
}
<!-- 引入一个布局作为主界面-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是主界面::侧滑对应的类型下的商品"
/>
<GridView
android:id="@+id/gv"
android:numColumns="2"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
<!--引入一个组件作为侧滑菜单 layout_gravity这个属性表示次组件是侧滑组件-->
<LinearLayout
android:id="@+id/ll"
android:layout_width="240dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="start"
android:background="#fff"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@mipmap/mm"
/>
<ListView
android:id="@+id/lv"
android:layout_width="240dp"
android:layout_height="match_parent"
/>
</LinearLayout>