DrawerLayout 侧滑

activity_main.xml
[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:id="@+id/drawerlayout"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     tools:context="com.example.fxf.drawerlayoutdemo2.MainActivity">  
  8.     <FrameLayout  
  9.         android:id="@+id/fragment"  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="match_parent">  
  12.     </FrameLayout>  
  13.     <ListView  
  14.         android:id="@+id/listview"  
  15.         android:layout_width="wrap_content"  
  16.         android:layout_height="match_parent"  
  17.         android:layout_gravity="left"  
  18.         android:background="@color/colorAccent"  
  19.         android:choiceMode="singleChoice"></ListView>  
  20. </android.support.v4.widget.DrawerLayout>  
 
 

MianActivity.java

[java]  view plain  copy
  1. import android.os.Bundle;  
  2. import android.support.v4.app.FragmentTransaction;  
  3. import android.support.v4.widget.DrawerLayout;  
  4. import android.support.v7.app.AppCompatActivity;  
  5. import android.view.View;  
  6. import android.widget.AdapterView;  
  7. import android.widget.ArrayAdapter;  
  8. import android.widget.FrameLayout;  
  9. import android.widget.ListView;  
  10. import java.util.ArrayList;  
  11. import java.util.List;  
  12.   
  13. public class MainActivity extends AppCompatActivity {  
  14.     List<String> list = new ArrayList<>();  
  15.     private DrawerLayout drawerLayout;  
  16.     private FrameLayout frameLayout;  
  17.     private ListView listview;  
  18.   
  19.     @Override  
  20.     protected void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         setContentView(R.layout.activity_main);  
  23.         initView();  
  24.         initData();  
  25.         SetLisente();  
  26.     }  
  27.    //初始化  
  28.     private void initView() {  
  29.         drawerLayout = (DrawerLayout) findViewById(R.id.drawerlayout);  
  30.         frameLayout = (FrameLayout) findViewById(R.id.fragment);  
  31.         listview = (ListView) findViewById(R.id.listview);  
  32.     }  
  33.     //DrawerLayout      listview配完  
  34.     private void initData() {  
  35.         for (int i = 0; i < 9; i++) {  
  36.             list.add("text" + i);  
  37.         }  
  38.         ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this  
  39.                 , android.R.layout.simple_list_item_1, list);  
  40.         listview.setAdapter(adapter);  
  41.   
  42.     }  
  43.    //点击DrawerLayout中 listview赋值到主页面fragment中TextVite中  
  44.     private void SetLisente() {  
  45.         listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {  
  46.             @Override  
  47.             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {  
  48.                 Fragment01 fragment01 = new Fragment01();  
  49.                 Bundle bundle = new Bundle();  
  50.                 bundle.putString("name", list.get(position));  
  51.                 fragment01.setArguments(bundle);  
  52.                 FragmentTransaction fragmentTransaction =  
  53.                         getSupportFragmentManager().beginTransaction();  
  54.                 FragmentTransaction replace = fragmentTransaction.replace(R.id.fragment, fragment01);  
  55.                 replace.commit();  
  56.                // 关闭DrawerLayout  
  57.                drawerLayout.closeDrawer(listview);  
  58.             }  
  59.         });  
  60.     }  
  61. }  
Fragment01.java
[java]  view plain  copy
  1. public class Fragment01 extends Fragment {  
  2.   
  3.     private View view;  
  4.   
  5.     
  6.     public View onCreateView(LayoutInflater inflater,  ViewGroup container,  Bundle savedInstanceState) {  
  7.         view = inflater.inflate(R.layout.fragment01, container, false);  
  8.         return view;  
  9.     }  
  10.     public void onActivityCreated( Bundle savedInstanceState) {  
  11.         super.onActivityCreated(savedInstanceState);  
  12.         TextView textView = (TextView) view.findViewById(R.id.fragment_textView);  
  13.         Bundle arguments = getArguments();  
  14.         String name = arguments.getString("name");  
  15.         textView.setText(name);  
  16.     }  
  17. }  
加   fragment01.xml

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical" android:layout_width="match_parent"  
  4.     android:layout_height="match_parent">  
  5.     <TextView  
  6.         android:id="@+id/fragment_textView"  
  7.         android:layout_width="wrap_content"  
  8.         android:layout_height="wrap_content"  
  9.         android:text="TextView" />  
  10. </LinearLayout>  

猜你喜欢

转载自blog.csdn.net/zhang1223665986/article/details/80601563