问题
In my application I am using the following means to render/generate the views to a view pager. Yes it works fine and as expected.
Note :- But here I have seen that this method has to put a lot of effort in terms of Android resources ( associated with the device). I want to find out any optimized way to do the same. Is there is any? Suggest me or the above is good ?
class MyActivity extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layoutView);
LinearLayout pageFirst = getPageFisrt(context);
LinearLayout pageSecond = getPageSecond(context);
LinearLayout pageThird = getPageThird(context);
LinearLayout pageFourth = getPageFourth(context);
.........
.........
pageArrayList = new ArrayList<LinearLayout>();
pageArrayList.clear();
pageArrayList.add(pageFirst);
pageArrayList.add(pageSecond);
pageArrayList.add(pageThird);
pageArrayList.add(pageFourth);
...........
..........
viewPager.setAdapter(new MatchDetailsPagerAdapter(
context, pageArrayList));
indicator.setViewPager(viewPagerMatchDetailMain);
}
}
and for each page I inflated the layout from resource, like
private LinearLayout getPageFisrt(Context context) {
// TODO Auto-generated method stub
LayoutInflater inflater = LayoutInflater.from(context);
LinearLayout linearLayoutFirstPage = (LinearLayout) inflater.inflate(
R.layout.pager_first_large_views, null);
// performing action on the page child layout.
return linearLayoutFirstPage;
}
Looking forward for a better approach to do the same
回答1:
I think,to use fragments with FragmentPagerAdapter
more optimized ,then generate all layots in onCreate
method.
public class FragmentAdapter extends FragmentPagerAdapter
implements IconPagerAdapter
{
private int count = 2;
public InstallFragmentAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return Fragment0.newInstance();
case 1:
return Fragment1.newInstance();
case 2:
return Fragment2.newInstance();
default:
break;
}
return null;
}
@Override
public int getCount() {
return count;
}
@Override
public CharSequence getPageTitle(int position) {
return "";
}
@Override
public int getIconResId(int position) {
return 0;
}
}
来源:https://stackoverflow.com/questions/15895554/rendering-views-to-view-pager-optimized-way