Rendering Views to View Pager - Optimized Way

為{幸葍}努か 提交于 2020-01-11 09:41:28

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!