android Fragment的簡單使用

來源:互聯網
上載者:User



public class DetailsFragment extends Fragment{private static final String[] WEEKS=new String[]{"a-1","b-1","c-1"};public static DetailsFragment newInstance(int index){DetailsFragment f = new DetailsFragment();Bundle b = new Bundle();b.putInt("index", index);f.setArguments(b);System.out.println("--==");return f;}public int getShowIndex(){int index = getArguments().getInt("index",0);return index;}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {TextView text = new TextView(getActivity());text.setText(WEEKS[getShowIndex()]);return text;}}


public class FragmentAcitivity extends ListFragment{private static final String[] WEEKS=new String[]{"a","b","c"};//設定初始化選擇位置int mCurCheckPosition= 0;int mShownCheckPosition = -1;/** * 填充資料 */@Overridepublic void onActivityCreated(Bundle savedInstanceState) {super.onActivityCreated(savedInstanceState);setListAdapter(new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_activated_1,android.R.id.text1,WEEKS));//建立之時預設選中行的詳細資料showDetals(mCurCheckPosition);}@Overridepublic void onListItemClick(ListView l, View v, int position, long id) {super.onListItemClick(l, v, position, id);//手動選中某一行顯示該行的詳細資料showDetals(position);}/** * 顯示詳細資料 * @param index 選中行 */public void showDetals(int index){//fragment的管理器FragmentManager am = getFragmentManager();DetailsFragment details = (DetailsFragment)am.findFragmentById(R.id.details);if(details == null || details.getShowIndex()!=index){//設定單選模式getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);//指定條目被選中getListView().setItemChecked(index, true);details = DetailsFragment.newInstance(index);//建立新的DetailsFragment執行個體FragmentTransaction ft = am.beginTransaction();//替換FrameLayout為DetailsFragmentft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);//將得到的fragment替換當前ViewGroup內容,add則不替換會一次累加ft.replace(R.id.details, details);//提交ft.commit();}}}


    <frameLayout     android:id="@+id/details"    android:layout_weight="2"    android:layout_width="0dp"    android:layout_height="match_parent"    android:background="?android:attr/detailsElementBackground"/>



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.