将视图渲染到 View Pager - 优化方式

Rendering Views to View Pager - Optimized Way(将视图渲染到 View Pager - 优化方式)

本文介绍了将视图渲染到 View Pager - 优化方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我使用以下方法将视图呈现/生成到视图寻呼机.是的,它工作正常,符合预期.

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.

注意:- 但在这里我看到这种方法必须在 Android 资源(与设备相关联)方面付出很多努力.我想找出任何优化的方法来做同样的事情.有吗?建议我还是上面的好?

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;
    }

期待更好的方法来做同样的事情

推荐答案

我想,用FragmentPagerAdapter更优化的片段,然后在onCreate方法中生成所有layot.

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;
    }
}

这篇关于将视图渲染到 View Pager - 优化方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:将视图渲染到 View Pager - 优化方式