Tuesday, November 29, 2016

FragmentStatePagerAdapter


activity_main 
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:gravity="center_horizontal" 
 android:orientation="vertical" 
 android:padding="4dip">

    <android.support.v4.view.ViewPager 
 android:id="@+id/pager" 
 android:layout_width="match_parent" 
 android:layout_height="0px" 
 android:layout_weight="1">
</android.support.v4.view.ViewPager>

    <LinearLayout 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 android:layout_weight="0" 
 android:gravity="center" 
 android:measureWithLargestChild="true" 
 android:orientation="horizontal">

        <Button 
 android:id="@+id/goto_first" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="first">
</Button>

        <Button 
 android:id="@+id/goto_last" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="last"></Button>
    </LinearLayout>
</LinearLayout>
fragment_image
<?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout 
 xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" >

    <TextView 
 android:id="@+id/text" 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 android:gravity="center_vertical|center_horizontal" 
 android:text="hello_world" 
 android:textAppearance="?android:attr/textAppearanceMedium" />

    <ImageView 
 android:id="@+id/imageView1" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:layout_below="@+id/text" 
 android:layout_centerHorizontal="true" 
 android:layout_marginTop="36dp" 
 android:src="@mipmap/ic_launcher" />

</RelativeLayout>
fragment_pager_list
<?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:background="@android:drawable/gallery_thumb" 
 android:orientation="vertical" >

    <TextView 
 android:id="@+id/text" 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 android:gravity="center_vertical|center_horizontal" 
 android:text="hello_world" 
 android:textAppearance="?android:attr/textAppearanceMedium" />

    <FrameLayout 
 android:layout_width="match_parent" 
 android:layout_height="0dip" 
 android:layout_weight="1" >

        <ListView 
 android:id="@android:id/list" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:drawSelectorOnTop="false" />
    </FrameLayout>

</LinearLayout>
public class MainActivity extends FragmentActivity {
    static final int ITEMS = 5;
    MyAdapter mAdapter;
    ViewPager mPager;

    @Override 
 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_pager);
        mAdapter = new MyAdapter(getSupportFragmentManager());
        mPager = (ViewPager) findViewById(R.id.pager);
        mPager.setAdapter(mAdapter);

        Button button = (Button) findViewById(R.id.first);
        button.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                mPager.setCurrentItem(0);
            }
        });
        button = (Button) findViewById(R.id.last);
        button.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                mPager.setCurrentItem(ITEMS - 1);
            }
        });
    }

    public static class MyAdapter extends FragmentStatePagerAdapter {
        public MyAdapter(FragmentManager fragmentManager) {
            super(fragmentManager);
        }

        @Override 
 public int getCount() {
            return ITEMS;
        }

        @Override 
 public Fragment getItem(int position) {
            switch (position) {
                case 0: // Fragment # 0 - This will show image 
 return ImageFragment.init(position);
                case 1: // Fragment # 1 - This will show image 
 return ImageFragment.init(position);
                default:// Fragment # 2-9 - Will show list 
 return ArrayListFragment.init(position);
            }
        }
    }


}
public class ImageFragment extends Fragment {
    int fragVal;

    static ImageFragment init(int val) {
        ImageFragment truitonFrag = new ImageFragment();
        // Supply val input as an argument. 
 Bundle args = new Bundle();
        args.putInt("val", val);
        truitonFrag.setArguments(args);
        return truitonFrag;
    }

    @Override 
 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        fragVal = getArguments() != null ? getArguments().getInt("val") : 1;
    }

    @Override 
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View layoutView = inflater.inflate(R.layout.fragment_image, container,
                false);
        View tv = layoutView.findViewById(R.id.text);
        ((TextView) tv).setText("Truiton Fragment #" + fragVal);
        return layoutView;
    }
}
public class ArrayListFragment extends ListFragment {
    int fragNum;
    String arr[] = {"1", "2", "2", "3", "4", "5"};

    static ArrayListFragment init(int val) {
        ArrayListFragment truitonList = new ArrayListFragment();

        // Supply val input as an argument. 
 Bundle args = new Bundle();
        args.putInt("val", val);
        truitonList.setArguments(args);

        return truitonList;
    }

    /**     * Retrieving this instance's number from its arguments.     */ 
 @Override 
 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        fragNum = getArguments() != null ? getArguments().getInt("val") : 1;
    }

    /**     * The Fragment's UI is a simple text view showing its instance number and 
 * an associated list.     */ 
 @Override 
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View layoutView = inflater.inflate(R.layout.fragment_pager_list,
                container, false);
        View tv = layoutView.findViewById(R.id.text);
        ((TextView) tv).setText("Truiton Fragment #" + fragNum);
        return layoutView;
    }

    @Override 
 public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setListAdapter(new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_1, arr));
    }

    @Override 
 public void onListItemClick(ListView l, View v, int position, long id) {
        Log.i("Truiton FragmentList", "Item clicked: " + id);
    }
} 
 

No comments:

Post a Comment