android中fragment的实现,完全取代选项卡

计算机113 郭超
Fragment的基本使用
-如何在屏幕中嵌入两个Fragment
1.   首先创建两个java类,让这个类继承Fragment
public  class ListFragment  extends Fragment{
   ListView lv;
   MainActivity ac;
   String [] items = {"aaa","bbb","ccc"};
   //和activity方法相似
public  void onActivityCreated(Bundle savedInstanceState) {
       super.onActivityCreated(savedInstanceState);
ArrayAdapter<String> adapter =  new ArrayAdapter<String>
(ac,android.R.layout. simple_list_item_1 ,items);
      lv.setAdapter(adapter);
      lv.setOnItemClickListener( new OnItemClickListener() {
    public  void onItemClick(AdapterView<?> parent, View view,
               int position,  long id) {
              ac.sendMsgToTextFragment(position);
        }
      });
   }
//返回一个activity有实例
    public  void onAttach(Activity activity) {
       super.onAttach(activity);
      ac = (MainActivity) activity;
   }
   //创建在此碎片上显示的视图
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
      View v = inflater.inflate(R.layout. list_fragment,  null);
      lv = (ListView) v.findViewById(R.id. listView1);
       return v;
   }
}

public  class TextFragment  extends Fragment{
   TextView tv;
public  void getMessage( int position){
      tv.setText("position"+position);
}
public  void onActivityCreated(Bundle savedInstanceState) {
   }
public  void onAttach(Activity activity) {
   }
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View v = inflater.inflate(R.layout. text_fragment,  null);
tv = (TextView) v.findViewById(R.id. textView1);
       return v;
}
}
 
2.   要想将这两个碎片文件组合在一起,就必须通过一个activity。
做法是在这个activity文件中加入fragment标签。如下所示:
<fragment
       android:id= "@+id/listf"
<!—设置要包含碎片文件的位置-->         android:name= "com.example.android_uplooking_day2.ListFragment"
        android:layout_width= "wrap_content"
        android:layout_height= "fill_parent"
        android:layout_weight= "1"  />
    <fragment
        android:id= "@+id/tvf"
<!--设置要包含碎片文件的位置-->
android:name= "com.example.android_uplooking_day2.TextFragment"
        android:layout_width= "wrap_content"
        android:layout_height= "fill_parent"
        android:layout_weight= "1"  />
这样就可以在activity中显示两个碎片文件了。
- 两个 Fragment 之前如何通信
要实现两个碎片之间的通信,就需要在通过activity来实现他们之间的交互。
例如:有两个碎片文件ListFragment和TextFragment。点击ListFragment上面的列表。要在TextFragment上面显示哪一个列被点击了。这就需要ListFragment给TextFragment发一个消息。TextFragment接收到这个消息,将它显示出来。
要实现这一过程,就需要在碎片里面使用activity对象。调用显示碎片的activity对象中的一个方法。这个方法接受到消息后。通过FragmentManager获得TextFragment碎片对象,然后通过TextFragment对象调用TextFragment中的方法,将传递过来的消息打印出来。
FragmentManager fm = getFragmentManager();
      TextFragment tf = (TextFragment) fm.findFragmentById(R.id. tvf);
      tf.getMessage(position);
- 如何增加 / 删除 / 替换 Fragment
-Fragment 常用的三个方法 :
1.   onActivityCreated类似Activity的onCreate方法
2.   onCreateView中构建界面&获得组件对象  
3.   onAttach获得外部的Activity(上下文)
Tab的替代品 Fragment 在3.0以后才可以使用.

 

转载于:https://www.cnblogs.com/gc58550105/p/3153543.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值