且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android:UI控件Fragment、FragmentStack

更新时间:2022-09-23 14:44:34


Fragment的实现有两种方法:xml直接添加和通过代码添加。

下面xml中的<fragment和<RelativeLayout分别使用xml添加和代码添加:

XML代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<RelativeLayout xmlns: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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".FragmentActivity" >
    <fragment
        android:id="@+id/fragment1"
        android:name="com.example.aexh32_fragment.MyFragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="52dp" />
    <RelativeLayout
        android:id="@+id/relayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
         >
    </RelativeLayout>
</RelativeLayout>


MyFragment类:(通过xml添加,直接新建一个Fragment类,从ui面板选择Fragment添加)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.TextView;
public class MyFragment extends Fragment
{
    //指定Fragment的layout,设置对应的控件事件
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View layout = inflater.inflate(R.layout.frangment_layout, null);
        TextView textView = (TextView) layout.findViewById(R.id.textView1);
                                                                                                                                                             
        return layout;
    }
}


FragmentActivity类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
public class FragmentActivity extends android.support.v4.app.FragmentActivity //导入的必须是support.v4才能支持2.3
{
    /**
     * 两种方法:
     * 1.xml可视化添加:
     * 先写好Fragment类,添加对应类的Fragment控件
     * 2.通过代码添加Fragment
     */
                                                                                                                                               
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment);
                                                                                                                                                   
        //通过代码添加
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        MyFragment newFragment = new MyFragment();
        ft.add(R.id.relayout, newFragment);//将MyFragment添加到relayout里
        ft.commit();//add、remove、replace后,都需要commit提交一下
    }
}




其他:

1.将fragment放入栈中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void addFragmentToStack()
{
    mStackLevel++;
    // Instantiate a new fragment.
    Fragment newFragment = CountingFragment.newInstance(mStackLevel);
    // Add the fragment to the activity, pushing this transaction
    // on to the back stack.
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.replace(R.id.simple_fragment, newFragment);
                                                                                                      
    //下面两句是关键
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    ft.addToBackStack(null);
    ft.commit();
}


2.管理Fragment:

要在activity中管理fragment,需要使用FragmentManager. 通过调用activity的getFragmentManager()取得它的实例.

可以通过FragmentManager做一些事情, 包括:

1.使用findFragmentById()(用于在activity layout中提供一个UI的fragment)或findFragmentByTag()(适用于有或没有UI的fragment)获取activity中存在的fragment

2.将fragment从后台堆栈中弹出, 使用 popBackStack() (模拟用户按下BACK 命令).

3.使用addOnBackStackChangeListener()注册一个监听后台堆栈变化的listener.

参考资料:http://blog.csdn.net/aomandeshangxiao/article/details/7671533




3.fragment之间传递参数


传递参数的fragment:

1
2
3
4
5
6
7
8
9
10
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
InfoFragment fragment = new InfoFragment();
Bundle bundle = new Bundle();
bundle.putInt(Contant.KEY_BUNDLE_POSITION, position);
fragment.setArguments(bundle);
ft.replace(R.id.list_fraglayout, fragment);
ft.setCustomAnimations(R.anim.slide_right_enter, R.anim.slide_left_out, R.anim.slide_left_in, R.anim.slide_right_exit);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();


接收参数的fragment:

1
int mId = getArguments().getInt(Contant.KEY_BUNDLE_POSITION);



4.找到对应Fragment

1
2
3
4
5
6
7
8
FragmentManager fm = getSupportFragmentManager();
Fragment fragment = (LoadRateTypeFragment) fm.findFragmentByTag(HBContant.FRAGMENT_TAG_LOADRATETYPE);
if (fragment == null)
{
    fragment = new LoadRateTypeFragment();
    fragment.setTargetFragment(fragment, 0);
    fm.beginTransaction().add(fragment, HBContant.FRAGMENT_TAG_LOADRATETYPE).commit();
}



本文转自 glblong 51CTO博客,原文链接:http://blog.51cto.com/glblong/1229559,如需转载请自行联系原作者