DrawerLayout 侧滑

来源:互联网 发布:mysql编程题及答案 编辑:程序博客网 时间:2024/05/22 06:36
activity_main.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/drawerlayout"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.example.fxf.drawerlayoutdemo2.MainActivity">    <FrameLayout        android:id="@+id/fragment"        android:layout_width="match_parent"        android:layout_height="match_parent">    </FrameLayout>    <ListView        android:id="@+id/listview"        android:layout_width="wrap_content"        android:layout_height="match_parent"        android:layout_gravity="left"        android:background="@color/colorAccent"        android:choiceMode="singleChoice"></ListView></android.support.v4.widget.DrawerLayout>

MianActivity.java

import android.os.Bundle;import android.support.v4.app.FragmentTransaction;import android.support.v4.widget.DrawerLayout;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.AdapterView;import android.widget.ArrayAdapter;import android.widget.FrameLayout;import android.widget.ListView;import java.util.ArrayList;import java.util.List;public class MainActivity extends AppCompatActivity {    List<String> list = new ArrayList<>();    private DrawerLayout drawerLayout;    private FrameLayout frameLayout;    private ListView listview;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initView();        initData();        SetLisente();    }   //初始化    private void initView() {        drawerLayout = (DrawerLayout) findViewById(R.id.drawerlayout);        frameLayout = (FrameLayout) findViewById(R.id.fragment);        listview = (ListView) findViewById(R.id.listview);    }    //DrawerLayout      listview配完    private void initData() {        for (int i = 0; i < 9; i++) {            list.add("text" + i);        }        ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this                , android.R.layout.simple_list_item_1, list);        listview.setAdapter(adapter);    }   //点击DrawerLayout中 listview赋值到主页面fragment中TextVite中    private void SetLisente() {        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {            @Override            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {                Fragment01 fragment01 = new Fragment01();                Bundle bundle = new Bundle();                bundle.putString("name", list.get(position));                fragment01.setArguments(bundle);                FragmentTransaction fragmentTransaction =                        getSupportFragmentManager().beginTransaction();                FragmentTransaction replace = fragmentTransaction.replace(R.id.fragment, fragment01);                replace.commit();               // 关闭DrawerLayout               drawerLayout.closeDrawer(listview);            }        });    }}
Fragment01.java
public class Fragment01 extends Fragment {    private View view;      public View onCreateView(LayoutInflater inflater,  ViewGroup container,  Bundle savedInstanceState) {        view = inflater.inflate(R.layout.fragment01, container, false);        return view;    }    public void onActivityCreated( Bundle savedInstanceState) {        super.onActivityCreated(savedInstanceState);        TextView textView = (TextView) view.findViewById(R.id.fragment_textView);        Bundle arguments = getArguments();        String name = arguments.getString("name");        textView.setText(name);    }}
加   fragment01.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent">    <TextView        android:id="@+id/fragment_textView"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="TextView" /></LinearLayout>