Simple use of android Fragment

Source: Internet
Author: 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 "}; // set the initialization selection position int mCurCheckPosition = 0; int mShownCheckPosition =-1;/*** fill data */@ 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); // The showDetals (mCurCheckPosition) of the selected row by default at creation;} @ Overridepublic void onListItemClick (ListView l, View v, int position, long id) {super. onListItemClick (l, v, position, id); // manually select a row to display the details of the row showDetals (position );} /*** display details ** @ param index selected row */public void showDetals (int index) {// fragment manager FragmentManager am = GetFragmentManager (); DetailsFragment details = (DetailsFragment) am. findFragmentById (R. id. details); if (details = null | details. getShowIndex ()! = Index) {// set the single-choice mode getListView (). setChoiceMode (ListView. CHOICE_MODE_SINGLE); // getListView () is selected for the specified entry (). setItemChecked (index, true); details = DetailsFragment. newInstance (index); // The New DetailsFragment instance FragmentTransaction ft = am. beginTransaction (); // replace FrameLayout with DetailsFragmentft. setTransition (FragmentTransaction. TRANSIT_FRAGMENT_FADE); // Replace the obtained fragment with the content of the current ViewGroup. If you do not replace the value of add, the value of ft is accumulated at one time. replace (R. id. details, details); // submit 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"/>
 



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.