Last active
February 19, 2017 05:10
-
-
Save hatena-iti/1a79c049de6eb53476e3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.fragmentpageradapterdemo; | |
import android.os.Bundle; | |
import android.support.v4.app.Fragment; | |
import android.support.v4.app.FragmentActivity; | |
import android.support.v4.app.FragmentManager; | |
import android.support.v4.app.FragmentPagerAdapter; | |
import android.support.v4.app.ListFragment; | |
import android.support.v4.view.ViewPager; | |
import android.util.Log; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.View.OnClickListener; | |
import android.view.ViewGroup; | |
import android.widget.ArrayAdapter; | |
import android.widget.Button; | |
import android.widget.ListView; | |
import android.widget.TextView; | |
public class MainActivity extends FragmentActivity { | |
static final int NUM_ITEMS = 10; | |
MyAdapter mAdapter; | |
ViewPager mPager; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
mAdapter = new MyAdapter(getSupportFragmentManager()); | |
mPager = (ViewPager)findViewById(R.id.pager); | |
mPager.setAdapter(mAdapter); | |
// Watch for button clicks. | |
Button button = (Button)findViewById(R.id.goto_first); | |
button.setOnClickListener(new OnClickListener() { | |
public void onClick(View v) { | |
mPager.setCurrentItem(0); | |
} | |
}); | |
button = (Button)findViewById(R.id.goto_last); | |
button.setOnClickListener(new OnClickListener() { | |
public void onClick(View v) { | |
mPager.setCurrentItem(NUM_ITEMS-1); | |
} | |
}); | |
} | |
/* | |
* MyAdapter | |
*/ | |
public static class MyAdapter extends FragmentPagerAdapter { | |
public MyAdapter(FragmentManager fm) { | |
super(fm); | |
} | |
@Override | |
public int getCount() { | |
return NUM_ITEMS; | |
} | |
@Override | |
public Fragment getItem(int position) { | |
Log.i("MyAdapter", "getItem[" + (position+1) + "]"); | |
return ArrayListFragment.newInstance(position+1); | |
} | |
} | |
/* | |
* ArrayListFragment | |
*/ | |
public static class ArrayListFragment extends ListFragment { | |
int mNum; | |
final static String[] sCheeseStrings = {"one", "two", "three", "four", "five"}; | |
/** | |
* Create a new instance of CountingFragment, providing "num" | |
* as an argument. | |
*/ | |
static ArrayListFragment newInstance(int num) { | |
ArrayListFragment f = new ArrayListFragment(); | |
// Supply num input as an argument. | |
Bundle args = new Bundle(); | |
args.putInt("num", num); | |
f.setArguments(args); | |
return f; | |
} | |
void outputLog() { | |
Log.i("FragmentList", String.format("%s:[%d](%x)", Thread.currentThread().getStackTrace()[3].getMethodName(), mNum, this.hashCode())); | |
} | |
public ArrayListFragment() { | |
super(); | |
outputLog(); | |
} | |
/** | |
* When creating, retrieve this instance's number from its arguments. | |
*/ | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
mNum = getArguments() != null ? getArguments().getInt("num") : 1; | |
outputLog(); | |
} | |
/** | |
* The Fragment's UI is just a simple text view showing its | |
* instance number. | |
*/ | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, | |
Bundle savedInstanceState) { | |
outputLog(); | |
View v = inflater.inflate(R.layout.fragment_pager_list, container, false); | |
View tv = v.findViewById(R.id.text); | |
((TextView)tv).setText("Fragment #" + mNum); | |
return v; | |
} | |
@Override | |
public void onDestroyView() { | |
outputLog(); | |
super.onDestroyView(); | |
} | |
@Override | |
public void onResume() { | |
outputLog(); | |
super.onResume(); | |
} | |
@Override | |
public void onActivityCreated(Bundle savedInstanceState) { | |
super.onActivityCreated(savedInstanceState); | |
setListAdapter(new ArrayAdapter<String>(getActivity(), | |
android.R.layout.simple_list_item_1, sCheeseStrings)); | |
} | |
@Override | |
public void onListItemClick(ListView l, View v, int position, long id) { | |
Log.i("FragmentList", "Item clicked: " + id); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment