您的位置:首页 > 其它

使用回调函数实现Fragment之间的传值

2015-09-24 20:51 337 查看
问题描述:

在MainActivity的布局文件中有两个Fragment,其中左边的Fragment的布局是一个ListView,右边的Fragment是一个TextView

我们想要实现的需求是:通过点击左边ListView中的条目,在右边显示与之对应的内容。

这里我们需要用到回调函数。对于回调函数我现在的思路还不是很清晰,还不能够掌握并灵活运用,但是还是得把老师讲的东西记下来。

不然明天又要讲新的东西,到之后就更没有时间来整理了。所以就先整理好,等之后有时间再系统学一下回调函数,因为它在Android的

开发当中显得非常重要。

这里我们先定义一个接口:

package org.mobiletrain.fragment_demo09;

/**
* 定义一个接口用来获取内容
* @author Administrator
*
*/
public interface OnGetContentListener {

public void getContent(String content);
}
MainActivity:

package org.mobiletrain.fragment_demo09;

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

/**
* 这里MainActivity需要实现接口OnGetContentListener并重写其
* getContent()方法
* @author Administrator
*
*/
public class MainActivity extends Activity implements OnGetContentListener{

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

/**
* 这里默认已经得到content值之后的操作
* 把点击左边条目得到的数据通过bundle传值传递给右边的Fragment
* @Override
*/
public void getContent(String content) {
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
MovieContentFragment fragment = new MovieContentFragment();
Bundle bundle = new Bundle();
bundle.putString("content", content);
//在主Activity中要通过被实例化的Fragment对象的setArguments(Bundle args)方法
//向Fragment传值
fragment.setArguments(bundle);
transaction.replace(R.id.ll_right, fragment);
transaction.commit();
}

}
左边的Fragment,这里我们需要把要填充右边的.txt文件复制到assets文件夹下:

package org.mobiletrain.fragment_demo09;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
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.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class MovieNameFragment extends Fragment{
//声明一个接口对象
private OnGetContentListener myListener;
@Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
//在onAttach方法中实例化myListener
myListener = (OnGetContentListener) activity;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.left, null);
ListView listView = (ListView)view.findViewById(R.id.movieList);
listView.setOnItemClickListener(new OnItemClickListener() {

/**
* 通过点击左边不同的条目来获取与条目相对应的assets文件夹下的content内容
* @Override
*/
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//这里是获取assets文件夹下的txt数据用于填充右边的Fragment
AssetManager asset = getActivity().getResources().getAssets();
try {
//获取读取流
InputStream is = asset.open("movie"+position+".txt");
//获取字节输出流
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int len = 0;
byte[]buffer = new byte[1024];
while ((len = is.read(buffer))!=-1) {
baos.write(buffer, 0, len);
baos.flush();

}
String content = new String (baos.toByteArray());
//使接口myListener得到content的值
myListener.getContent(content);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
});

return view;
}
}
右边的Fragment:

package org.mobiletrain.fragment_demo09;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MovieContentFragment extends Fragment{

/**
* 获取主Activity传来的Content并显示在TextView中
* @Override
*/
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.right, null);
TextView tv = (TextView)view.findViewById(R.id.content);
tv.setText(getArguments().getString("content"));
return view;
}
}
activity_main.xml文件:

<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="horizontal" >
<fragment
android:id="@+id/frag_left"
android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
class="org.mobiletrain.fragment_demo09.MovieNameFragment"/>

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

</LinearLayout>
</LinearLayout>
left.xml文件:

<?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/movieList"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:entries="@array/movies">

</ListView>

</LinearLayout>
right.xml文件:

这里有一个新的知识点就是使用ScrollView来嵌套LinearLayout布局:这样在右边得到的内容中就可以通过滚动查看所有内容,不然会有一些内容看不到,这里

做一下解释,算是一个新的知识点吧。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scroll"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="18sp" />
</LinearLayout>
</ScrollView>
好了,发一下截图吧:



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