extends Fragment : Fragment « Core Class « Android






extends Fragment

  
package app.test;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.ListFragment;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

class TitlesFragment extends ListFragment {
  private Test myActivity = null;
  int mCurCheckPosition = 0;

  @Override
  public void onInflate(AttributeSet attrs, Bundle icicle) {
    for (int i = 0; i < attrs.getAttributeCount(); i++) {
      Log.v("",attrs.getAttributeName(i) + " = "
              + attrs.getAttributeValue(i));
    }
    super.onInflate(attrs, icicle);
  }

  @Override
  public void onAttach(Activity myActivity) {
    super.onAttach(myActivity);
    this.myActivity = (Test) myActivity;
  }

  @Override
  public void onCreate(Bundle icicle) {
    if (icicle != null) {
      for (String key : icicle.keySet()) {
        Log.v(Test.TAG, "    " + key);
      }
    }super.onCreate(icicle);
    if (icicle != null) {
      mCurCheckPosition = icicle.getInt("curChoice", 0);
    }
  }

  @Override
  public View onCreateView(LayoutInflater myInflater, ViewGroup container,
      Bundle icicle) {
    return super.onCreateView(myInflater, container, icicle);
  }
  @Override
  public void onActivityCreated(Bundle icicle) {
    if (icicle != null) {
      for (String key : icicle.keySet()) {
        Log.v(Test.TAG, "    " + key);
      }
    }
    super.onActivityCreated(icicle);
    setListAdapter(new ArrayAdapter<String>(getActivity(),
        android.R.layout.simple_list_item_1, new String[] { "A", "B",
            "C", "D", }));
    ListView lv = getListView();
    lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    lv.setSelection(mCurCheckPosition);
    myActivity.showDetails(mCurCheckPosition);
  }
  @Override
  public void onStart() {
    super.onStart();
  }
  @Override
  public void onResume() {
    super.onResume();
  }
  @Override
  public void onPause() {
    super.onPause();
  }
  @Override
  public void onSaveInstanceState(Bundle icicle) {
    super.onSaveInstanceState(icicle);
    icicle.putInt("curChoice", mCurCheckPosition);
  }
  @Override
  public void onListItemClick(ListView l, View v, int pos, long id) {
    myActivity.showDetails(pos);
    mCurCheckPosition = pos;
  }
  @Override
  public void onStop() {
    super.onStop();
  }
  @Override
  public void onDestroyView() {
    super.onDestroyView();
  }
  @Override
  public void onDestroy() {
    super.onDestroy();
  }
  @Override
  public void onDetach() {
    super.onDetach();
    myActivity = null;
  }
}
class DetailsActivity extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
      finish();
      return;
    }

    if (getIntent() != null) {
      DetailsFragment details = DetailsFragment.newInstance(getIntent()
          .getExtras());

      getFragmentManager().beginTransaction()
          .add(android.R.id.content, details).commit();
    }
  }
}

class DetailsFragment extends Fragment {
  private int mIndex = 0;
  public static DetailsFragment newInstance(int index) {
    DetailsFragment df = new DetailsFragment();
    Bundle args = new Bundle();
    args.putInt("index", index);
    df.setArguments(args);
    return df;
  }
  public static DetailsFragment newInstance(Bundle bundle) {
    int index = bundle.getInt("index", 0);
    return newInstance(index);
  }

  @Override
  public void onInflate(AttributeSet attrs, Bundle savedInstanceState) {
    for (int i = 0; i < attrs.getAttributeCount(); i++)
      Log.v("",attrs.getAttributeName(i) + " = "
              + attrs.getAttributeValue(i));
    super.onInflate(attrs, savedInstanceState);
  }

  @Override
  public void onAttach(Activity myActivity) {
    super.onAttach(myActivity);
  }
  @Override
  public void onCreate(Bundle myBundle) {
    if (myBundle != null) {
      for (String key : myBundle.keySet()) {
        Log.v(Test.TAG, "    " + key);
      }
    }super.onCreate(myBundle);
    mIndex = getArguments().getInt("index", 0);
  }

  public int getShownIndex() {
    return mIndex;
  }
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.row, container, false);
    TextView text1 = (TextView) v.findViewById(R.id.text1);
    text1.setText("");
    return v;
  }

  @Override
  public void onActivityCreated(Bundle savedState) {
    if (savedState != null) {
      for (String key : savedState.keySet()) {
        Log.v(Test.TAG, "    " + key);
      }
    }
    super.onActivityCreated(savedState);
  }

  @Override
  public void onStart() {
    super.onStart();
  }
  @Override
  public void onResume() {
    super.onResume();
  }
  @Override
  public void onPause() {
    super.onPause();
  }
  @Override
  public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
  }
  @Override
  public void onStop() {
    super.onStop();
  }
  @Override
  public void onDestroyView() {
    super.onDestroyView();
  }
  @Override
  public void onDestroy() {
    super.onDestroy();
  }

  @Override
  public void onDetach() {
    super.onDetach();
  }
}
public class Test extends Activity {
  public static final String TAG = "Test";

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FragmentManager.enableDebugLogging(true);
    setContentView(R.layout.main);
  }
  @Override
  public void onAttachFragment(Fragment fragment) {
    super.onAttachFragment(fragment);
  }
  @Override
  public void onStart() {
    super.onStart();
  }

  @Override
  public void onResume() {
    super.onResume();
  }
  @Override
  public void onPause() {
    super.onPause();
  }
  @Override
  public void onStop() {
    super.onStop();
  }
  @Override
  public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
  }

  @Override
  public void onDestroy() {
    super.onDestroy();
  }

  public boolean isMultiPane() {
    return getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
  }

  public void showDetails(int index) {
    if (isMultiPane()) {
      DetailsFragment details = (DetailsFragment) getFragmentManager()
          .findFragmentById(R.id.details);
      if (details == null || details.getShownIndex() != index) {
        details = DetailsFragment.newInstance(index);
        FragmentTransaction ft = getFragmentManager()
            .beginTransaction();
        ft.setCustomAnimations(R.animator.bounce_in_down,
            R.animator.slide_out_down);
        ft.replace(R.id.details, details);
        ft.commit();
        getFragmentManager().executePendingTransactions();
      }
    } else {
      Intent intent = new Intent();
      intent.setClass(this, DetailsActivity.class);
      intent.putExtra("index", index);
      startActivity(intent);
    }
  }
}

//layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- This file is res/layout/main.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <fragment class="app.test.TitlesFragment"
            android:id="@+id/titles"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
            android:background="#00550033" />

    <FrameLayout
            android:id="@+id/details" android:layout_weight="2"
            android:layout_width="0px"
            android:layout_height="match_parent" />
</LinearLayout>

//layout/row.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- This file is res/layout/details.xml -->
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
  <ScrollView android:id="@+id/scroller" 
      android:layout_width="match_parent"
      android:layout_height="match_parent">
    <TextView android:id="@+id/text1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
  </ScrollView>
</LinearLayout>


//animator/bounce_in_down.xml


<?xml version="1.0" encoding="utf-8" ?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:interpolator/bounce"
    android:valueFrom="-1280"
    android:valueTo="0"
    android:valueType="floatType"
    android:propertyName="Y"
    android:duration="2000" />


//animator/slide_out_down.xml

<?xml version="1.0" encoding="utf-8" ?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<objectAnimator
    android:interpolator="@android:interpolator/accelerate_cubic"
    android:valueFrom="0"
    android:valueTo="1280"
    android:valueType="floatType"
    android:propertyName="Y"
    android:duration="2000" />
<objectAnimator
    android:interpolator="@android:interpolator/accelerate_cubic"
    android:valueFrom="1"
    android:valueTo="0"
    android:valueType="floatType"
    android:propertyName="Alpha"
    android:duration="2000" />
</set>

   
    
  








Related examples in the same category

1.Demonstration of hiding and showing fragments.
2.Demonstration of using ListFragment to show a list of items from a canned array.
3.Fragment Stack
4.Use Fragment to propagate state across activity instances when an activity needs to be restarted due to a configuration change.
5.extends Fragment to display result
6.Demonstration of PreferenceFragment, showing a single fragment in an activity.
7.Get child element, inner element, outer element, parse Fragment