天天看点

Fragment和Fragment进行数据传递

要实现的功能就是点击获取按钮的时候获取另外一个Fragmen里Textview的值

我们要知道一件事情,就是同一级的Fragmen都是通过一个FragmentManager来进行管理的,利用这点我们就可以来做文章了.

首先我们要进行承载Fragment的Activity来进行编写,这里很简单就是把Fragment通过事物加到指定布局就可以。

代码:

package com.example.fragmentdemo;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

public class MainActivity extends FragmentActivity {
    private FragmentManager fm;
    private FragmentTransaction ft;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        fm = getSupportFragmentManager();
        ft = fm.beginTransaction();
        Right rigth = new Right();
        ft.add(R.id.right, rigth, "rigth");
        Left left = new Left();
        ft.add(R.id.left, left, "left");

        ft.commit();
    }

}      

布局文件也很简单:

<LinearLayout 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:baselineAligned="false"
    android:orientation="horizontal"
    tools:context=".MainActivity" >

    <LinearLayout
        android:id="@+id/left"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical" >
    </LinearLayout>

    <LinearLayout
        android:id="@+id/right"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical" >
    </LinearLayout>

</LinearLayout>      

然后是获取值的Fragment的编写

package com.example.fragmentdemo;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

/**
 * A simple {@link android.support.v4.app.Fragment} subclass.
 * 
 */
public class Right extends Fragment {
    // 声明FragmentMagager对象
    private FragmentManager fm;

    /**
     * 初始化数据
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        // 实例化
        fm = getFragmentManager();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_blank, container, false);
        Button btn_rigth = (Button) v.findViewById(R.id.btn_right);
        btn_rigth.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                Left left = (Left) fm.findFragmentByTag("left");
                //getview是指获取布局
                String msg = ((TextView)left.getView().findViewById(R.id.tv_left)).getText().toString();
                Toast.makeText(getActivity(), msg, 1).show();
            }
        });
        
        return v;
    }

    @Override
    public void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
    }

}      
<LinearLayout 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:orientation="vertical"
    tools:context=".Right" >

    <!-- TODO: Update blank fragment layout -->

    <TextView
        android:id="@+id/tv_right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我是右侧的" />

    <Button
        android:id="@+id/btn_right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="点击获取左侧值" />

</LinearLayout>      

这样就实现了Fragment间值的传递