android:ListFragment簡單記錄(12)

//功能:點擊一個按鈕,顯示出一個ListFragment,將ListFragment的內容給右邊的Fragment顯示出來
//MainActivity代碼:
public class MainActivity extends FragmentActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	public void clickButton(View view) {
		FragmentTransaction transaction = getSupportFragmentManager()
				.beginTransaction();
		transaction.add(R.id.center, new ListFragmentDemo(), "ListFragment");
		transaction.commit();

	}
}

//繼承的是ListFragment,,ListFragmentDemo中代碼
public class ListFragmentDemo extends ListFragment {
	private ArrayAdapter<String> adapter;
   
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		List<String> list = new ArrayList<String>();
		for (int i = 0; i < 30; i++) {
			list.add("mike嘿嘿嘿" + i);
		}
		adapter = new ArrayAdapter<String>(getActivity(),
				android.R.layout.simple_list_item_1, list);
		setListAdapter(adapter);
		// ListFragment 的好處是不用創建一個Listview,由於它內含有ListView的佈局,直接綁定
	}

	// 並且也有監聽事件
	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		// 傳值給另一個Fragment
		// 注意這裏不是getSupportFragmentManager()了,這個V4版本纔有
		FragmentManager fragmentManager = getFragmentManager();
		FragmentTransaction transaction = fragmentManager.beginTransaction();
		ShowListFragment showListFragment = new ShowListFragment();
		transaction.add(R.id.right, showListFragment, "ShowListFragment");
		// 從ListFragment獲取數據
		// 這裏須要用到適配器獲取數據,最好定義全局
		String name = adapter.getItem(position);
		Bundle bundle = new Bundle();
		bundle.putString("name", name);
		showListFragment.setArguments(bundle);
		transaction.commit();

	}
}

public class ShowListFragment extends Fragment {
	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		Bundle bundle = getArguments();
		String name = bundle.getString("name");
		TextView textView=new TextView(getActivity());
		textView.setText(name);
		return textView;
	}
}

//主佈局文件
<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" >

    <LinearLayout
        android:id="@+id/left"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#cccccc"
        android:orientation="vertical" >

        <Button
            android:id="@+id/bnt_show"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="clickButton"
            android:text="顯示"
            android:textSize="14sp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/center"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:background="#AFEEEE"
        android:orientation="vertical" >
    </LinearLayout>

    <LinearLayout
        android:id="@+id/right"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:background="#00FFFF"
        android:orientation="vertical" >
    </LinearLayout>

</LinearLayout>
相關文章
相關標籤/搜索