1.MainActivity
import android.os.Bundle;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
public class MainActivity extends Activity implements Callback {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void getMovieResult(String content) {
RightFragment rightFragment = new RightFragment();
Bundle bundle = new Bundle();
bundle.putString("MovieContent", content);
rightFragment.setArguments(bundle);
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.layout_container_rightFragment, rightFragment);
//对于是否要加transaction.addToBackStack(null)
//也就是将Fragment加入到回退栈。官方的说法是取决于你是否要在回退的时候显示上一个Fragment。
transaction.addToBackStack(null);
transaction.commit();
}
}
activity_main布局
<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/leftFragment"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:name="com.qianfeng.fragmentmovie.LeftFragment"/>
<LinearLayout
android:id="@+id/layout_container_rightFragment"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="match_parent"
android:orientation="horizontal">
</LinearLayout>
</LinearLayout>
RightFragment:
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 RightFragment extends Fragment {
private TextView textView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_right, container, false);
textView = (TextView) view.findViewById(R.id.textView);
String str = getArguments().getString("MovieContent");
textView.setText(str);
return view;
}
}
fragment_right布局
<?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" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="展示电影内容的TextView"/>
</ScrollView>
</LinearLayout>
定义一个回调接口
public interface Callback {
public void getMovieResult(String content);
}
LeftFragment代码:
import java.io.ByteArrayOutputStream;
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.ArrayAdapter;
import android.widget.ListView;
public class LeftFragment extends Fragment {
private ListView listView;
private String[] arrTitle = {"扫毒","功夫战斗机","顶楼的大象","狂爱恶徒","兽性窥探","新金瓶梅","绣春刀","变形金刚4","后会无期","3D肉蒲团","色·戒"};
private ArrayAdapter<String> adapter;
private Callback myListener;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
myListener = (Callback) getActivity();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_left,container , false);
listView = (ListView) view.findViewById(R.id.listView);
adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, arrTitle);
listView.setAdapter(adapter);
getMovieContent(0);//默认加载第一条电影内容
//绑定每一项item的单击事件 点击item的时候解析相应的文本内容然后显示在右侧的Fragment中的TextView上
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
getMovieContent(position);
}
});
return view;
}
//自定义获取相应电影内容的方法
public void getMovieContent(int position){
InputStream is = null;
ByteArrayOutputStream baos = null;
try {
AssetManager manager = getActivity().getResources().getAssets();
is= manager.open("movie"+position+".txt");//将资产目录下的文件通过流读取
baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len=is.read(buffer))!=-1) {
baos.write(buffer, 0, len);
baos.flush();
}
//电影内容字符串
byte[] result = baos.toByteArray();
String movieContent = new String(result, "utf-8");
myListener.getMovieResult(movieContent);
} catch (Exception e) {
e.printStackTrace();
}finally{
if (is!=null) {
try {
is.close();
baos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
fragment_left布局
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#5f00"
android:dividerHeight="1dp"
android:listSelector="#50f0"
android:background="@android:color/darker_gray">
</ListView>
</LinearLayout>