<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.why.moliweek2.MainActivity" > <android.support.v4.widget.DrawerLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/dl"> <!--主内容区域--> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <RadioGroup android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:id="@+id/rg" android:layout_alignParentBottom="true"> <RadioButton android:textColor="@drawable/selector" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="首页" android:button="@null" android:gravity="center" android:id="@+id/bt1" android:padding="3dp" /> <RadioButton android:textColor="@drawable/selector" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="想法" android:button="@null" android:padding="3dp" android:gravity="center" android:id="@+id/bt2" /> <RadioButton android:textColor="@drawable/selector" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="市场" android:button="@null" android:gravity="center" android:padding="3dp" android:id="@+id/bt3" /> <RadioButton android:textColor="@drawable/selector" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="通知" android:padding="3dp" android:button="@null" android:gravity="center" android:id="@+id/bt4" /> </RadioGroup> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/fl"></FrameLayout> </RelativeLayout> <RelativeLayout android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start" android:background="#ffff00" android:id="@+id/rel"> <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/lv" ></ListView> </RelativeLayout> </android.support.v4.widget.DrawerLayout> </RelativeLayout>
public class MainActivity extends AppCompatActivity { private RadioGroup rg; private DrawerLayout dl; private ListView lv; private ArrayList<String> datalist; private RelativeLayout rel; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); int netType = NetStateUtils.getNetType(this); if(netType!=-1){ Toast.makeText(this,"您的APP有网络",Toast.LENGTH_SHORT).show(); }else{ initData(); } //获取控件 rg = findViewById(R.id.rg); dl = (DrawerLayout) findViewById(R.id.dl); rel = (RelativeLayout) findViewById(R.id.rel); lv = (ListView) findViewById(R.id.lv); datalist = new ArrayList<>(); datalist.add("QQ钱包"); datalist.add("个性装扮"); datalist.add("我的相册"); ArrayAdapter<String> stringArrayAdapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1, datalist); lv.setAdapter(stringArrayAdapter); //设置listview的条目监听事件 lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { //设置参数 frag5 f5=new frag5(); frag6 f6=new frag6(); frag7 f7=new frag7(); dl.closeDrawer(rel); //切换fragment switch (i){ case 0: getSupportFragmentManager().beginTransaction().replace(R.id.fl,f5).commit(); break; case 1: getSupportFragmentManager().beginTransaction().replace(R.id.fl,f6).commit(); break; case 2: getSupportFragmentManager().beginTransaction().replace(R.id.fl,f7).commit(); break; } } }); //设置rg的监听事件 rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { private frag4 f4; private frag3 f3; private frag2 f2; private frag1 f1; @Override public void onCheckedChanged(RadioGroup radioGroup, int checkedId) { switch (checkedId){ case R.id.bt1: f1 = new frag1(); addfragment(f1); break; case R.id.bt2: f2 = new frag2(); addfragment(f2); break; case R.id.bt3: f3 = new frag3(); addfragment(f3); break; case R.id.bt4: f4 = new frag4(); addfragment(f4); break; default: break; } } }); } private void initData() { AlertDialog.Builder b=new AlertDialog.Builder(this); b.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) {
} }); b.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { } }); b.show(); } public void addfragment(Fragment f){ FragmentManager manager = getSupportFragmentManager(); FragmentTransaction transaction = manager.beginTransaction(); transaction.replace(R.id.fl,f).commit();}