简体   繁体   English

viewpager中的不同布局

[英]Different layouts in a viewpager

I am trying to do something like the android market to show products from different categories. 我正在尝试像Android市场那样展示来自不同类别的产品。 I have implemented that behavior with this library, and it's all running good. 我已经使用这个库实现了这种行为,并且它运行良好。

What I want to do now is to have a list view in the page of the categories, and in each one of the others a grid view. 我现在要做的是在类别的页面中有一个列表视图,在其他每个列表中都有一个网格视图。 Of course, each one of them will have different sources of data. 当然,他们每个人都有不同的数据来源。

How can I achieve that ? 我怎样才能做到这一点? Does it have anything to do with adapters? 它与适配器有什么关系吗?

I have find one Very good Example you can find that on this site 我找到了一个很好的例子,你可以在这个网站上找到它

https://github.com/astuetz/ViewPagerExtensions https://github.com/astuetz/ViewPagerExtensions

Else hope this code will be Helpful to you. 另外希望这段代码对您有所帮助。

@Override
public Object instantiateItem(View collection, int position) {
    View v = new View(PatientView.this.getApplicationContext());
    final LayoutInflater inflater = (LayoutInflater) PatientView.this
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    switch (position) {
    case 0:
        v = inflater.inflate(R.layout.patientp1,
                (ViewGroup) null, false);
        ((Button) v.findViewById(R.id.pp1btnbck)).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                finish();
            }
        });

        break;
    case 1:
        v = inflater.inflate(R.layout.patientp2, null, false
                );
        break;
    default:

        TextView tv = new TextView(PatientView.this.context);
        tv.setText("Page " + position);
        tv.setTextColor(Color.WHITE);
        tv.setTextSize(30);
        v = tv;
        break;
    }
    ((ViewPager) collection).addView(v, 0);

    return v;
}

I think you want extend FragmentPagerAdapter , override getItem , and return the proper Fragment object (or ListFragment) depending on the index/position in the ViewPager. 我想你需要扩展FragmentPagerAdapter ,覆盖getItem ,并根据ViewPager中的索引/位置返回正确的Fragment对象(或ListFragment)。

In the layout xml for your Activity you should include a ViewPager ( <android.support.v4.view.ViewPager> ), and in your Activity onCreate use the setAdapter method of ViewPager to assign your FragmentPagerAdapter . 在你的活动布局XML,你应该包括ViewPager<android.support.v4.view.ViewPager>并在活动onCreate使用setAdapter方法ViewPager分配你的FragmentPagerAdapter

Hello I have explain how to use the fragment in view pager in my tutorial have a look on this link 您好我已经解释了如何在我的教程中查看分页器中的片段看看这个链接

You can also download code from here 您也可以从这里下载代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM