码迷,mamicode.com
首页 > 移动开发 > 详细

Android之Fragment学习总结(1)

时间:2015-10-14 23:32:12      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:

对于Fragment的学习:

写的是简易版的Fragment应用,在一个主layout(activty_main.layout)中添加一个fragment。因为

需要继承 android.support.v4.app.Fragment ,Fragment类 需要实现的方法:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)

通过下列语句实现创建一个View实例并返回,

View v = (View) inflater.inflate(R.layout.crime_fragment,container,false);

参数分别是:布局文件,父组件,是否关联

技术分享
 1 package com.example.fragmentpractise;
 2 
 3 import java.util.UUID;
 4 
 5 import android.os.Bundle;
 6 import android.support.v4.app.Fragment;
 7 import android.util.Log;
 8 import android.view.LayoutInflater;
 9 import android.view.View;
10 import android.view.ViewGroup;
11 import android.widget.Button;
12 
13 public class CrimeFragment extends Fragment{
14    
15    private Crime mCrime;
16    
17    public static CrimeFragment newInstance(int id){
18        CrimeFragment c = new CrimeFragment();
19        return c;
20    }
21    
22    @Override
23    public void onCreate(Bundle savedInstanceState){
24        super.onCreate(savedInstanceState);
25        mCrime = new Crime();
26    }
27    
28    @Override
29    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
30        
31        View v = (View) inflater.inflate(R.layout.crime_fragment,container,false);
32        Button date = (Button) v.findViewById(R.id.date);
33        date.setText(mCrime.getDate().toString());
34        return v;
35    
36    }
37     
38 }
Crime_fragment
技术分享
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical" >
 6     <TextView 
 7         android:layout_width="match_parent"
 8         android:layout_height="wrap_content"
 9         android:textSize="20dp"
10         android:text="@string/title_label"/>
11     <EditText
12         android:id="@+id/title"
13         android:layout_width="match_parent"
14         android:layout_height="wrap_content"
15         android:textSize="16dp"
16         />
17     <TextView 
18         android:layout_width="match_parent"
19         android:layout_height="wrap_content"
20         android:textSize="20dp"
21         android:text="@string/detail_label"/>
22     <Button
23         android:id="@+id/date"
24         android:layout_width="match_parent"
25         android:layout_height="wrap_content"
26         />
27     <CheckBox
28         android:id="@+id/isSolved"
29         android:layout_width="match_parent"
30         android:layout_height="wrap_content"
31         android:text="@string/isSolved"/>
32 
33 </LinearLayout>
Fragment布局

 

在启动类中实现通过FragmentManager调用 findFragmentById(id) 查找是否存在,最后通过事务提交Fragment

技术分享
 1 @Override
 2     protected void onCreate(Bundle savedInstanceState) {
 3         super.onCreate(savedInstanceState);
 4         setContentView(R.layout.activity_crime);
 5         FragmentManager fm = getSupportFragmentManager() ;
 6         Fragment fragment = fm.findFragmentById(R.id.fragment_container); 
 7         if(fragment == null){
 8             FragmentTransaction tran = fm.beginTransaction ();
 9             tran.add(R.id.fragment_container,new CrimeFragment());
10             tran.commit();
11         }
检测是否存在该Fragment并加入

 

 

附加:对于findFragmentById的探索

public abstract Fragment findFragmentById (int id)

 

Finds a fragment that was identified by the given id either when inflated from XML or as the container ID when added in a transaction. This first searches through fragments that are currently added to the manager‘s activity; if no such fragment is found, then all fragments currently on the back stack associated with this ID are searched.

Returns

The fragment if found or null otherwise.

这个方法实现了从FragmentManager实例中去寻找一个符合条件的Fragment,这里的条件受id制约,但API提到id究竟是什么呢?尝试使用了传入提交事务时的id,或者是生成布局时的给设置的id,最后在生成视图去检测都依旧不行;

为了验证这一方法,以下代码分别验证了通过Fragmentd XML的id来搜索:

技术分享
 1 @Override
 2    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
 3        
 4        View v = (View) inflater.inflate(R.layout.crime_fragment,container,false);
 5        Button date = (Button) v.findViewById(R.id.date);
 6        date.setText(mCrime.getDate().toString());
 7        v.setId(R.id.test);
 8        return v;
 9    
10    }
为布局加入Id
技术分享
1 fragment = fm.findFragmentById(R.id.test);
2         if(fragment != null){
3             Log.i("information", "true");
4         } else{
5             Log.i("information","false");
6         }
查询是否存在

结果是:10-13 23:10:16.267: I/information(2997): false

直到后来求助了高手和查阅资料后,终于发现了问题所在:这个提交的动作并不是同步的,由事务提交请求,再由系统自己去处理,为了验证这个问题,使用了以下代码来验证:

技术分享
1 FragmentManager fm = getSupportFragmentManager() ;
2         Fragment fragment = fm.findFragmentById(R.id.fragment_container); 
3         if(fragment == null){
4             FragmentTransaction tran = fm.beginTransaction ();
5             tran.add(R.id.fragment_container,new CrimeFragment());
6             tran.commit();
7         }
先提交给事务
技术分享
 1 Timer timer= new Timer();
 2         timer.schedule(new TimerTask() {
 3             
 4             @Override
 5             public void run() {
 6                 // TODO Auto-generated method stub
 7                 FragmentManager fm = getSupportFragmentManager() ;
 8                 Fragment fragment = fm.findFragmentById(R.id.fragment_container); 
 9                 if(fragment != null){
10                     Log.i("information", "true");
11                 } else{
12                     Log.i("information","false");
13                 }
14             }
15         }, 2000);
通过一个线程去延迟查找动作

结果是:10-14 22:18:47.436: I/information(1672): true

思路是使用线程,延迟2秒去检测。

 

 

Android之Fragment学习总结(1)

标签:

原文地址:http://www.cnblogs.com/lhppom/p/4876220.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!