I'm building some custom listview then add them to View Pager.
But when I swift screen, View Pager show only 1 Custom ListView.
this is the image of problem:
the SpeakingActivity.java
public class SpeakingActivity extends SherlockFragmentActivity {
static final int NUM_ITEMS = 4;
MyAdapter mAdapter;
ViewPager mPager;
@Override
protected void onCreate(Bundle savedInstanceState){
setTheme(R.style.Theme_Sherlock); // Used for theme switching in samples
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_pager);
mAdapter = new MyAdapter(getSupportFragmentManager());
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
}
public class MyAdapter extends FragmentPagerAdapter {
SpeakingFragment myFrag[];
int numItem = 4;
public MyAdapter(FragmentManager fm) {
super(fm);
myFrag = new SpeakingFragment[numItem];
for(int i = 0; i < numItem; i++) {
myFrag[i] = new SpeakingFragment();
}
}
@Override
public int getCount(){
return numItem;
}
@Override
public Fragment getItem(int position){
return myFrag[position];
}
}
the SpeakingFragment.java
public class SpeakingFragment extends SherlockFragment {
public final String KEY_SONG = "song";
public final String KEY_ID = "id";
public final static String KEY_TITLE = "title";
public final static String KEY_ARTIST = "artist";
public final static String KEY_DURATION = "duration";
public final static String KEY_THUMB_URL = "thumb_url";
ListView list;
LazyAdapter adapter;
ArrayList<HashMap<String, String>> songsList;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.main, container, false);
return v;
}
@Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
list = (ListView) getSherlockActivity().findViewById(R.id.list);
songsList = new ArrayList<HashMap<String, String>>();
for(int i = 0; i < 15; i++) {
HashMap<String, String> map = new HashMap<String, String>();
map.put(KEY_ID, String.valueOf(i));
map.put(KEY_TITLE, "Title" + i);
map.put(KEY_ARTIST, "Dolphin");
map.put(KEY_DURATION, "11:11");
map.put(KEY_THUMB_URL, "Thumb");
songsList.add(map);
}
adapter = new LazyAdapter(getSherlockActivity(), songsList);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
}
});
}
}
LazyAdapter.java
public class LazyAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private LayoutInflater inflater = null;
public ImageLoader imageLoader;
public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data = d;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader = new ImageLoader(activity.getApplicationContext());
}
public int getCount(){
return data.size();
}
public Object getItem(int position){
return position;
}
public long getItemId(int position){
return position;
}
public View getView(int position, View convertView, ViewGroup parent){
View vi = convertView;
if(convertView == null) vi = inflater.inflate(R.layout.list_row, null);
TextView title = (TextView) vi.findViewById(R.id.title); // title
TextView artist = (TextView) vi.findViewById(R.id.artist); // artist name
TextView duration = (TextView) vi.findViewById(R.id.duration); // duration
ImageView thumb_image = (ImageView) vi.findViewById(R.id.list_image); // thumb image
HashMap<String, String> song = new HashMap<String, String>();
song = data.get(position);
title.setText(song.get(SpeakingFragment.KEY_TITLE));
artist.setText(song.get(SpeakingFragment.KEY_ARTIST));
duration.setText(song.get(SpeakingFragment.KEY_DURATION));
imageLoader.DisplayImage(song.get(SpeakingFragment.KEY_THUMB_URL), thumb_image);
return vi;
}
}
You can try full code here
I've found the error my self.
The swift action ocur before onActivityCreated. It makes the next fragment isn't show up but when swift back, onActivityCreated occured so it appear.
to fix this error, change the code from onActivityCreated to onCreateView (because onCreateView occur before swift).