listFragment

来源:互联网 发布:javascript php 混合 编辑:程序博客网 时间:2024/05/20 20:21


转自:http://blog.csdn.net/kakaxi1o1/article/details/29368645


1,ListFragment的布局默认包含一个list view。因此,在ListFragment对应的布局文件中,必须指定一个 android:id “@android:id/list” 的ListView控件! 若用户向修改list view的,可以在onCreateView(LayoutInflater, ViewGroup, Bundle)中进行修改。当然,用户也可以在ListFragment的布局中包含其它的控件。

<?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       <strong> android:id="@android:id/list"       android:layout_width="match_parent"       android:layout_height="wrap_content"       >   </ListView> </LinearLayout>

@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {View view=inflater.inflate(R.layout.list_fragment, null);return view;}

2,ListFragment绑定ListView的数据,必须通过ListFragment.setListAdapter()接口来绑定数据,而不是使用ListView.setAdapter() 或其它方法!

    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        this.setListAdapter(new ArrayAdapter<String>(getActivity(),                android.R.layout.simple_list_item_1, cities));    }
3,,监听事件

@Overridepublic void onListItemClick(ListView l, View v, int position, long id) {super.onListItemClick(l, v, position, id); Toast.makeText(getActivity(),                 "You have selected " + cities[position],                Toast.LENGTH_SHORT).show(); showSelectedDetails(cities[position]);}
        private void showSelectedDetails(String city) {translateContentListener.translate(city);}
4,向acitivty传递数据

interface TranslateContentListener{void translate(String city);}@Overridepublic void onAttach(Activity activity) {super.onAttach(activity);translateContentListener=(TranslateContentListener) activity;}private void showSelectedDetails(String city) {translateContentListener.translate(city);}
5,activity只负责控制fragment,实现上面定义的接口

public class FragmentActivity extends BaseActivity implements TranslateContentListener{RightFragement rightFragment;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.fragment_activity);FragmentManager manager=getSupportFragmentManager();rightFragment=(RightFragement) manager.findFragmentById(R.id.right_fragment);}@Overridepublic void translate(String city) {rightFragment.showContent(city);}       //向fragment传递数据         interface ShowContentListener{void showContent(String title);}}
6,接受内容的fragment 负责展示

public class RightFragement extends Fragment implements ShowContentListener{TextView textView;@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {View view=inflater.inflate(R.layout.text_view_itme, null);textView=(TextView) view.findViewById(R.id.text);return view;}@Overridepublic void showContent(String title) {textView.setText(title);}}

当然,也可以通过 fragment的getActivity方法,实现fragment向activity传递数据






1 0