package com.zxpMobile.day12_1513_listfragment; import android.app.ListFragment; 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.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.ListView; public class Fragment2 extends ListFragment implements OnItemClickListener { private ListView listView; private String[] data; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub View rootview = inflater.inflate(R.layout.fragment2_layout, container, false); listView = (ListView) rootview.findViewById(android.R.id.list); data = new String[] { "選項一", "選項二", "選項三", "選項四" }; return rootview; } @Override public void onActivityCreated(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onActivityCreated(savedInstanceState); this.setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, data)); listView.setOnItemClickListener(this); } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub //Log.i("1513", "1234"); Fragment1 fg1 = (Fragment1) getActivity().getFragmentManager(). findFragmentById(R.id.fg1); switch (position) { case 0: fg1.setTextViewText("選項一被選中"); break; case 1: fg1.setTextViewText("選項二被選中"); break; case 2: fg1.setTextViewText("選項三被選中"); break; case 3: fg1.setTextViewText("選項四被選中"); break; default: break; } } }