Fragment getting started DEMO

Source: Internet
Author: User

<?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="horizontal" >        <fragment         android:id="@+id/title"        class="com.njupt.fragment1.TitleFragment"        android:layout_width="0dp"        android:layout_height="fill_parent"        android:layout_weight="1"        />        <FrameLayout         android:id="@+id/detail"        android:layout_width="0dp"        android:layout_height="fill_parent"        android:layout_weight="1"        />    </LinearLayout>


Package com. njupt. fragment1; import android. app. fragmentManager; import android. app. fragmentTransaction; import android. app. listFragment; import android. OS. bundle; import android. view. view; import android. widget. arrayAdapter; import android. widget. listView; public class TitleFragment extends ListFragment {private String [] data = new String [] {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday ", "Saturday", "Sunday"}; private int currentPos Ition = 0; @ Overridepublic void onActivityCreated (Bundle savedInstanceState) {super. onActivityCreated (savedInstanceState); setListAdapter (new ArrayAdapter <String> (getActivity (), android. r. layout. simple_list_item_activated_1, android. r. id. text1, data); showDetail (currentPosition);} public void showDetail (int index) {FragmentManager fm = getFragmentManager (); DetailFragment detail = (DetailFragment) fm. find FragmentById (R. id. detail); if (detail = null | index! = Detail. getShowIndex () {getListView (). setChoiceMode (ListView. CHOICE_MODE_SINGLE); getListView (). setItemChecked (index, true); detail = DetailFragment. getInstance (index); FragmentTransaction ft = fm. beginTransaction (); ft. replace (R. id. detail, detail); ft. commit () ;}@ Overridepublic void onListItemClick (ListView l, View v, int position, long id) {super. onListItemClick (l, v, position, id); showDetail (position );}}


Package com. njupt. fragment1; import android. app. fragment; import android. OS. bundle; import android. view. layoutInflater; import android. view. view; import android. view. viewGroup; import android. widget. textView; public class DetailFragment extends Fragment {private String [] data = new String [] {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday ", "Saturday", "Sunday",}; public static DetailFragment getInstance (int index) {DetailFragment df = new DetailFragment (); Bundle args = new Bundle (); args. putInt ("index", index); df. setArguments (args); return df;} public int getShowIndex () {return getArguments (). getInt ("index") ;}@ Overridepublic View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {TextView TV = new TextView (getActivity (); TV. setText (data [getShowIndex ()]); return TV ;}}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.