Android(安卓)之 Fragment
16lz
2023-01-31
一左侧标题列表
1.1 布局 left_fragment.xml
<ListViewxmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/list" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/darker_gray" android:listSelector="@drawable/onitem_selected_bkcolor"/>
1.2 ListSelector onitem_selected_bkcolor.xml
<?xmlversion="1.0"encoding="utf-8"?> <selectorxmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_window_focused="false" android:drawable="@android:color/holo_green_dark"/> <item android:state_window_focused="true" android:drawable="@android:color/holo_green_light"/> </selector>
1.3 自定义 ListItem 布局 代替 android.R.layout.simple_list_item_1
<?xmlversion="1.0"encoding="utf-8"?> <TextViewxmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="30dp" android:gravity="center_vertical" android:paddingLeft="10dp" android:textColor="@android:color/black"/>
1.4 自定义 LeftFragment
packagecom.example.myfragments; importandroid.app.Activity; importandroid.app.ListFragment; importandroid.os.Bundle; importandroid.view.LayoutInflater; importandroid.view.View; importandroid.view.ViewGroup; importandroid.widget.ArrayAdapter; importandroid.widget.ListView; //自定义回调函数 interfaceonItemSeletedListener { publicvoidonItemSeleted(intposition); } publicclassLeftFragmentextendsListFragment{ onItemSeletedListenermCallback; String[]data={"item0","item1","item2","item3","item4","item5","item6","item7","item8","item9","item10","item11","item12","item13","item14","item15","item16"}; @Override publicViewonCreateView(LayoutInflaterinflater,ViewGroupcontainer, BundlesavedInstanceState){ //TODO自动生成的方法存根 returninflater.inflate(R.layout.left_fragment,container,false); } @Override publicvoidonActivityCreated(BundlesavedInstanceState){ //TODO自动生成的方法存根 setListAdapter(newArrayAdapter<String>(getActivity(), R.layout.listitem,data)); super.onActivityCreated(savedInstanceState); } @Override publicvoidonListItemClick(ListViewl,Viewv,intposition,longid){ //TODO自动生成的方法存根 mCallback.onItemSeleted(position); } @Override publicvoidonAttach(Activityactivity){ //TODO自动生成的方法存根 super.onAttach(activity); //Thismakessurethatthecontaineractivityhasimplemented //thecallbackinterface.Ifnot,itthrowsanexception try{ mCallback=(onItemSeletedListener)activity; }catch(ClassCastExceptione){ thrownewClassCastException("必须实现onItemSeletedListener"); } } }
二 右侧内容展示
2.1 布局 right_fragment.xml
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/background_dark" android:orientation="vertical" tools:ignore="HardcodedText,UselessParent"> <ScrollView android:scrollbars="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/textview" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="请选择左侧边栏:)" android:textColor="@android:color/holo_orange_dark" android:textSize="30sp"/> </ScrollView> </LinearLayout>
2.1 自定义 RightFragment
packagecom.example.myfragments; importandroid.app.Fragment; importandroid.os.Bundle; importandroid.view.LayoutInflater; importandroid.view.View; importandroid.view.ViewGroup; importandroid.widget.TextView; publicclassRightFragmentextendsFragment{ @Override publicViewonCreateView(LayoutInflaterinflater,ViewGroupcontainer, BundlesavedInstanceState){ //TODO自动生成的方法存根 returninflater.inflate(R.layout.right_fragment,container,false); } //更新数据 publicvoidupdate(intposition) { TextViewtextview=(TextView)getActivity().findViewById(R.id.textview); textview.setText("您选择了:"+String.valueOf(position)+"\n--------------" +"\n大江东去浪淘尽,\n千古风流人物,\n故垒西边,\n人道是,\n三国周郎赤壁,\n乱石穿空,\n惊涛拍岸,\n卷起千堆雪,\n江山如画,\n一时多少豪杰。" +"\n遥想公瑾当年,\n小乔初嫁了,\n雄姿英发,\n羽扇纶巾,\n谈笑间,\n樯橹灰飞烟灭,\n故国神游,\n多情应笑我,\n早生华发,\n人间如梦,\n一樽还酹江月。"); } }
三 添加到 main_layout.xml 中,附着于 Activity 显示
3.1布局 main_layout.xml
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" tools:ignore="DisableBaselineAlignment"> <fragment android:id="@+id/left_fragment" android:name="com.example.myfragments.LeftFragment" android:layout_width="70dp" android:layout_height="match_parent"/> <View android:layout_width="2dp" android:layout_height="match_parent" android:background="@android:color/background_dark"/> <fragment android:id="@+id/right_fragment" android:name="com.example.myfragments.RightFragment" android:layout_width="wrap_content" android:layout_height="match_parent"/> </LinearLayout>
3.2 MainActivity
packagecom.example.myfragments; importandroid.app.Activity; importandroid.os.Bundle; importandroid.view.Window; importandroid.view.WindowManager; publicclassMainActivityextendsActivityimplementsonItemSeletedListener{ @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.main_layout); ////添加 //FragmentManagerfragmentmanager=getFragmentManager(); //FragmentTransactionfragmenttransaction=fragmentmanager.beginTransaction(); //LeftFragmentleftfragment=newLeftFragment(); //fragmenttransaction.add(R.id.left_fragment,leftfragment); //fragmenttransaction.commit(); ////删除 //FragmentManagerfragmentmanager=getFragmentManager(); //FragmentTransactionfragmenttransaction=fragmentmanager.beginTransaction(); //Fragmentleftfragment=fragmentmanager.findFragmentById(R.id.left_fragment); //fragmenttransaction.remove(leftfragment); //fragmenttransaction.commit(); ////替换 //FragmentManagerfragmentmanager=getFragmentManager(); //FragmentTransactionfragmenttransaction=fragmentmanager.beginTransaction(); //fragmenttransaction.replace(R.id.left_fragment,newLeftFragment()); //fragmenttransaction.commit(); } @Override publicvoidonItemSeleted(intposition){ RightFragmentrightFragment=(RightFragment)getFragmentManager().findFragmentById(R.id.right_fragment); if(rightFragment!=null) rightFragment.update(position); } }
四 结果展示
转载请注明出处 :)更多相关文章
- 浅谈Java中Collections.sort对List排序的两种方法
- Python list sort方法的具体使用
- python list.sort()根据多个关键字排序的方法实现
- android上一些方法的区别和用法的注意事项
- android实现字体闪烁动画的方法
- Android中dispatchDraw分析
- Android四大基本组件介绍与生命周期
- Android(安卓)MediaPlayer 常用方法介绍
- 在Fragment中设置控件点击方法,执行失败。