您的位置:首页 > 其它

两个Fragment之间传递数据之一

2016-02-19 20:27 253 查看
在此实例中, 两个Fragment在同一个Activity,需要了解Fragment的两种加载方式(静态和动态):

关于Fragment的一些知识:

一,如何创建Fragment

1,定义一个类,继承Fragment

2,重写父类的生命周期方法:onCreateView()

二,Fragment的显示方式

1,静态显示

布局页面中:

必须要指定id或者tag属性,来标示Fragment的唯一

android:name 指定要显示的Fragment类

2,动态显示

[code]2.1 在Activity的布局中,通过布局容器占位
    2.2 在Activity中:
    //1, 得到Fragment管理器对象
    FragmentManager manager = getFragmentManager();

    //2,开始Fragment的事务处理
    FragmentTransaction transaction = manager.beginTransaction();

    //3,实例化Fragment
    MyFragment fragment = new MyFragment();

    //4,动态显示Fragment
    //containerViewId  布局中给Fragment占位的布局容器ID, fragment  这个位置上显示的Fragment
    transaction.add(R.id.frgment_id, fragment);

    //5,提交事务,而且只能提交一次
    transaction.commit();


简要说明:左右两个Fragment,左侧显示文件名, 点击文件名,右侧根据左侧传来的文件名查找文件,并将文件内容读出,显示在右侧Fragment中,左侧静态显示,右侧动态显示,文件放在assets目录下

具体代码:

MainActivity,在这里没用,怕忘记,先贴出来:

[code]package com.example.zjday12_home_work;

import android.os.Bundle;
import android.app.Activity;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

}


左侧Fragment:

[code]package com.example.zjday12_home_work.fragment;

import java.util.ArrayList;
import java.util.List;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.example.zjday12_home_work.R;

public class Left_Fragment extends Fragment {
    private ListView lv;
    private List<String> data = new ArrayList<String>();
    private ArrayAdapter<String>adapter;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View view =inflater.inflate(R.layout.fragment_left, null);
        lv=(ListView) view.findViewById(R.id.listView_id);
        data.add("day02.txt");
        data.add("day03.txt");
        data.add("day04.txt");
        data.add("day05.txt");
        data.add("day06.txt");
        data.add("day07.txt");

        adapter =new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, data);

        lv.setAdapter(adapter);
        //为listView设置监听,向ContentFragment中传入文件名
        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                //获取点击的Item的对应的文件名
                String fileName=data.get(position);
                //通过bundle传值
                Bundle args=new Bundle();
                args.putString("fileName",fileName);
                //实例化一个ContentFragment对象,用于传值
                ContentFragment fragment =new ContentFragment();
                fragment.setArguments(args);

                //动态加载右侧的Fragment
                getFragmentManager()
                .beginTransaction()
                .replace(R.id.frameLayout_id, fragment)
                .addToBackStack(null)
                .commit();
            }
        });

        return view;
    }
}


右侧Fragment(ContentFragment):

[code]package com.example.zjday12_home_work.fragment;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import com.example.zjday12_home_work.R;

import android.app.Fragment;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class ContentFragment extends Fragment {
    private TextView tv;
    private String contentText;
    private String fileName;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view =inflater.inflate(R.layout.fragment_content, null);
        tv=(TextView) view.findViewById(R.id.tv);

        //接受从left_Fragment 传入的数据(文件名)
        fileName =getArguments().getString("fileName");
        //根据传入的文件名获取assets目录下的对应文件的内容
        AssetManager manager =getResources().getAssets();
        //打开对应文件
        try {
            InputStream inStream =manager.open(fileName);
            StringBuffer sBuffer =new StringBuffer();
            String line =null;
            BufferedReader buffReader=new BufferedReader(new InputStreamReader(inStream));

            while((line=buffReader.readLine())!=null){
                sBuffer.append(line);
                sBuffer.append("\n");
            }
            contentText= sBuffer.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        tv.setText(contentText);
        return view;
    }
}


activity_main.xml布局文件:

[code]<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:padding="5dp"
>

    <fragment 
        android:id="@+id/fragment_id"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:name="com.example.zjday12_home_work.fragment.Left_Fragment"/>

    <FrameLayout 
        android:id="@+id/frameLayout_id"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2"
        />

</LinearLayout>


fragment_layout.xml布局文件:

[code]<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView 
        android:id="@+id/listView_id"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="#00ff00"
        android:dividerHeight="2dp"
        />
</LinearLayout>


fragment_content.xml:

[code]<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ccc" />

</ScrollView>


不要忘记assets下的文件!!!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: