天天看點

listFragment

轉自: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>
           
@Override
	public 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,,監聽事件

@Override
	public 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);
	}
	@Override
	public 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;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_activity);
		FragmentManager manager=getSupportFragmentManager();
		rightFragment=(RightFragement) manager.findFragmentById(R.id.right_fragment);
	}
	@Override
	public 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;
	@Override
	public 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;
	}
	@Override
	public void showContent(String title) {
		textView.setText(title);
	}
}
           

當然,也可以通過 fragment的getActivity方法,實作fragment向activity傳遞資料