建立两个Fragment其中一个定义回调接口,声明接口,注册接口,另一个Xml中写个TextView控件,显示参数信息:
需要注意Fragment是3.0以后出现的,一定要注意是使用
android.app.Fragment;包还是使用V4包,必须要统一否则会报错
具体代码如下
有接口的Fragment:
Activity中实现的接口方法,在Fragment绑定Activity时实现注册监听onAttach()方法中
采用Bundle对象实现参数封装传递
<span style="font-size:14px;">import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.xhsc.layout.relativelayout.R;
/**
* A simple {@link Fragment} subclass.
*/
public class HeadlinesFragment extends Fragment implements AdapterView.OnItemClickListener {
String[] arraylist={"新闻","科技","健康","军事","佛教"};
public OnFragmentInteractionListener mListener;
ListView mListview;
public static HeadlinesFragment newInstance() {
Bundle args = new Bundle();
HeadlinesFragment headlinesFragment = new HeadlinesFragment();
headlinesFragment.setArguments(args);
return headlinesFragment;
}
public HeadlinesFragment() {
// Required empty public constructor
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
mListener = (OnFragmentInteractionListener) activity; //在绑定Activity的过程中实现接口的注册
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v=inflater.inflate(R.layout.fragment_headlines_layout, container, false);
mListview = (ListView)v.findViewById(R.id.fragment_headlines_listview);
HeadlinesAdapter adapter = new HeadlinesAdapter(getActivity());//Fragment不能直接获取Context
mListview.setAdapter(adapter);
mListview.setOnItemClickListener(this);//this表示当前类
adapter.setArrayData(arraylist);
return v;
}
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
HeadlinesAdapter adapter = (HeadlinesAdapter) adapterView.getAdapter();
String item = (String) adapter.getItem(i);
Log.v("tag",""+mListener);
mListener.onFragmentInteraction(item);
}
private class HeadlinesAdapter extends BaseAdapter{
String[] arrayData=new String[]{};
LayoutInflater layoutInflater;
public HeadlinesAdapter(Context context){
layoutInflater = LayoutInflater.from(context);
}
public void setArrayData(String[] arrayData) {
this.arrayData = arrayData;
notifyDataSetChanged();
}
@Override
public int getCount() {
return arrayData.length;
}
@Override
public Object getItem(int i) {
return arrayData[i];
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
if(view==null){
view = layoutInflater.inflate(android.R.layout.simple_list_item_1,null);
}
TextView textView = (TextView) view;
textView.setText((String) getItem(i));
return view;
}
}
//回调接口,实现数据传给activity
public interface OnFragmentInteractionListener {
public void onFragmentInteraction(String message);
}
}</span>
xml代码
<span style="font-size:14px;"><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:background="@android:color/holo_green_dark"
tools:context="com.xhsc.fragment.HeadlinesFragment">
<ListView
android:id="@+id/fragment_headlines_listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
</LinearLayout></span>
另外一个显示信息的Fragment代码:
<span style="font-size:14px;">import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.xhsc.layout.relativelayout.R;
public class ParamsFragment extends Fragment {
String mMassage;
public ParamsFragment() {
// Required empty public constructor
}
public static ParamsFragment newInstance(String message) {
Bundle args = new Bundle();
args.putString("MESSAGE",message);
ParamsFragment fragment = new ParamsFragment();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);//官方推荐在此生命周期中接收参数
if(getArguments()!=null){
Bundle bundle = getArguments();
mMassage = bundle.getString("MESSAGE");
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view =inflater.inflate(R.layout.fragment_params_layout, container, false);
TextView textView = (TextView) view.findViewById(R.id.fragment_params_show_textview);
textView.setText(mMassage);
return view;
}
}</span>
Xml代码:
<span style="font-size:14px;"><FrameLayout 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"
tools:context="com.xhsc.fragment.fragmentlife.paramters.ParamsFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:id="@+id/fragment_params_show_textview"
android:textColor="@android:color/holo_purple"
android:textSize="20sp"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</FrameLayout></span>
Activity代码
<span style="font-size:14px;">import android.app.Activity;
import android.os.Bundle;
import com.xhsc.fragment.HeadlinesFragment;
import com.xhsc.layout.relativelayout.R;
public class FragmentParamsToFragmentActivity extends Activity implements HeadlinesFragment.OnFragmentInteractionListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragmen_tactivity_params_to_fragment_layout);
HeadlinesFragment headlinesFragment = HeadlinesFragment.newInstance();
getFragmentManager().beginTransaction().add(R.id.fragment_params_listview_framelayout,headlinesFragment ).commit();
/* headlinesFragment.mListener = new HeadlinesFragment.OnFragmentInteractionListener() {
@Override
public void onFragmentInteraction(String message) {
Log.v("tag", "" + message);
getFragmentManager().beginTransaction().add(R.id.fragment_params_show_framelayout, ParamsFragment.newInstance(message)).commit();
}
};*/
}
@Override
public void onFragmentInteraction(String message) {
//这里使用替换方法,否则会不断添加,出覆盖重影的问题,在Activity正常情况是在onCreate()方法中注册接口接听,而Fragment则可以通绑定时实现接口注册监听
getFragmentManager().beginTransaction().replace(R.id.fragment_params_show_framelayout, ParamsFragment.newInstance(message)).commit();
}
}</span>
xml代码:
<span style="font-size:14px;"><?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="horizontal"
>
<!--空布局占位,作用是动态添加移除替换Fragment-->
<FrameLayout
android:background="@android:color/holo_blue_bright"
android:id="@+id/fragment_params_listview_framelayout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
</FrameLayout>
<FrameLayout
android:id="@+id/fragment_params_show_framelayout"
android:layout_width="0dp"
android:background="@android:color/holo_orange_light"
android:layout_height="match_parent"
android:layout_weight="2">
</FrameLayout>
</LinearLayout></span>