当前位置: 代码迷 >> 综合 >> DrawerLayout 侧滑
  详细解决方案

DrawerLayout 侧滑

热度:84   发布时间:2023-11-15 20:10:18.0
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"><FrameLayoutandroid:id="@+id/fragment"android:layout_width="match_parent"android:layout_height="match_parent"></FrameLayout><ListViewandroid: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;@Overrideprotected 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() {@Overridepublic 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();// 关闭DrawerLayoutdrawerLayout.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"><TextViewandroid:id="@+id/fragment_textView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="TextView" />
</LinearLayout>