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
Fragment01.javaimport 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.xmlpublic 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);} }
<?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>
详细解决方案
DrawerLayout 侧滑
热度:84 发布时间:2023-11-15 20:10:18.0
相关解决方案
- Android基础入门教程——2.4.15 DrawerLayout(官方侧滑菜单)的简略使用
- Android DrawerLayout 屉子
- Toolbar + DrawerLayout 的左侧菜单有关问题
- drawerlayout 标题栏
- android.support.v4.widget.DrawerLayout无法转换为android.support.v4.widget.DrawerLayout $ DrawerListener
- DrawerLayout 侧滑
- NavigationView+DrawerLayout
- SlidingPaneLayout,DrawerLayout,ActionBar,NavigationView 详解 三 NavigationView,DrawerLayout 结合ActionB
- DrawerLayout 和ActionBar的联动中遇到问题,ActionBar 不能控制DrawerLayout
- SlidingPaneLayout,DrawerLayout,ActionBar,NavigationView 详解 二SlidingPaneLayout 的 简单的使用
- Android NavigationDrawer(侧滑导航栏 DrawerLayout + NavigationView)
- ##报错:DrawerLayout must be measured with MeasureSpec.EXACTLY. - 在使用ToolBar+DrawerLayout可能会报这个错误 - 1,
- DrawerLayout 学习笔记
- Android DrawerLayout 高仿QQ5 2双向侧滑菜单
- DrawerLayout(侧滑栏)详解,仿QQ消息列表(置顶、删除)