在ViewPager Android上显示以前的数据

6
在我的应用程序中,我有一个带有CirclePageIndicator的ViewPager,并且它起作用了。它在一个Pager中显示两个项。当我划到另一个Pager时,它会显示上一个已经显示在Pager上的项。但我不想要这样。我希望一次滑动两个项,不要显示已经在Pager上显示过的前一个项。
这是我的先前的问题,与此相关。 Show two item in ViewPager android
当前的工作方式为: ItemA,ItemB | ItemB,ItemC | ItemC,ItemD | 我希望像这样: ItemA,ItemB | ItemC,ItemD | ItemE,ItemF |
这是我用来显示一个Pager中的两个项的代码。
@Override
public float getPageWidth(int position) {
    return(0.5f);
}

Adapter.class

public class ViewPagerAdapter extends PagerAdapter {
    // Declare Variables
    Context context;
    String[] domain;
    String[] title;
    int[] flag;
    LayoutInflater inflater;

    public ViewPagerAdapter(Context context, String[] domain,
                            String[] title, int[] flag) {
        this.context = context;
        this.domain = domain;
        this.title = title;
        this.flag = flag;
    }

 @Override
public int getCount() {
    return (int) Math.ceil((double)title.length/2);
}

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == ((RelativeLayout) object);
    }



@Override
public Object instantiateItem(ViewGroup container, int position) {

    inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View pageView = inflater.inflate(R.layout.related_article_item_page, container,
            false);

    // Locate the TextViews in viewpager_item.xml
    View itemView1 = pageView.findViewById(R.id.article_1);
    View itemView2 = pageView.findViewById(R.id.article_2);

    configureItemView(itemView1, calculateFirstItemDataPosition(position));
    configureItemView(itemView2, calculateSecondItemDataPosition(position));

    return pageView;
}

public int calculateFirstItemDataPosition(int position){
    return position * 2;
}

public int calculateSecondItemDataPosition(int position){
    return position * 2 + 1;
}

public void configureItemView(View view,int position){
    inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View ConfigItemView = inflater.inflate(R.layout.related_article_item, null);

    // Locate the TextViews in viewpager_item.xml
    final Button btnSave = (Button) ConfigItemView.findViewById(R.id.btn_save);
    final Button btnLike = (Button) ConfigItemView.findViewById(R.id.btn_like);
    final TextView tv_domain = (TextView) ConfigItemView.findViewById(R.id.domain_text);
    TextView tv_title = (TextView) ConfigItemView.findViewById(R.id.title_text);
    ResizableImageView imageView = (ResizableImageView) ConfigItemView.findViewById(R.id.imageViewDynamic);
    final ProgressBar progressBar = (ProgressBar) ConfigItemView.findViewById(R.id.loading);

    // Capture position and set to the TextViews
    tv_domain.setText(domain[position]);
    tv_title.setText(title[position]);

    // Locate the ImageView in viewpager_item.xml
    imageView.setImageResource(flag[position]);

    // Add viewpager_item.xml to ViewPager
}



    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        // Remove viewpager_item.xml from ViewPager
        //((ViewPager) container).removeView((RelativeLayout) object);

    }


}

活动

    public class TestingActivity extends ActionBarActivity {
        // Declare Variables
        ViewPager viewPager;
        PagerAdapter adapter;
        String[] title;
        String[] domain;
        int[] flag;
        CirclePageIndicator mIndicator;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            // Get the view from viewpager_main.xml
            setContentView(R.layout.viewpager_main);

            title = new String[]{"Canada", "Thailand", "United States United StatesUnited StatesUnited States United States United States United States United States United States United States United States United States United States United States United States United States ",
                "Indonesia", "Brazil", "Thailand", "Nigeria", "Singapore",
                "RussiaRussiaRussiaRussiaRussiaRussiaRussiaRussia", "Japan"};

            domain = new String[]{"text1", "text1",
                "text1", "text1", "text1", "text1",
                "text1", "text1", "text1", "text1"};

            flag = new int[]{R.drawable.arsenal, R.drawable.aston_villa,
                R.drawable.manchester_city, R.drawable.liverpool,
                R.drawable.chelsea, R.drawable.manchester_united, R.drawable.swansea,
                R.drawable.liverpool, R.drawable.west_brom, R.drawable.west_ham};

            // Locate the ViewPager in viewpager_main.xml
            viewPagerRelated = (ViewPager) findViewById(R.id.pager2);
            // Pass results to ViewPagerAdapter Class
            adapter = new ViewPagerAdapter(ArticleViewActivityV2.this, domain,
                title, flag);
            // Binds the Adapter to the ViewPager
            viewPagerRelated.setAdapter(adapter);
            // viewPagerRelated.setPageMargin(3);
            // ViewPager Indicator
            mIndicator = (CirclePageIndicator) findViewById(R.id.indicator_pager);
            mIndicator.setViewPager(viewPagerRelated);    
        }
    }

日志文件

java.lang.ArrayIndexOutOfBoundsException: length=9; index=9
        at com.sample.news.adapters.ViewPagerAdapter.configureItemView(ViewPagerAdapter.java:94)
        at com.sample.news.adapters.ViewPagerAdapter.instantiateItem(ViewPagerAdapter.java:64)
        at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:869)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:1085)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:951)
        at android.support.v4.view.ViewPager.onTouchEvent(ViewPager.java:2041)
        at android.view.View.dispatchTouchEvent(View.java:7717)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2244)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1979)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2250)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1951)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2250)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1951)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2250)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1951)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2250)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1951)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2250)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1951)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2250)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1951)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2250)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1951)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2250)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1951)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2087)
        at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1532)
        at android.app.Activity.dispatchTouchEvent(Activity.java:2467)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2035)
        at android.view.View.dispatchPointerEvent(View.java:7897)
        at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4005)
        at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3884)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3445)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3495)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3464)
        at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3571)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3472)
        at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3628)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3445)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3495)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3464)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3472)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3445)
        at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5653)
        at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5633)
        at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5604)
        at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5733)
        at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
        at android.os.MessageQueue.nativePollOnce(Native Method)
        at android.os.MessageQueue.next(MessageQueue.java:138)
        at android.os.Looper.loop(Looper.java:123)
        at android.app.ActivityThread.main(ActivityThread.java:5050)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect

谢谢


请展示您的适配器和活动,不需要 XML。 - Sheychan
额,我还需要一件事……就是添加项目的循环,但是在你的代码中我找不到它。 - Sheychan
@Sheychan 已更新完整源代码。 - y7876768
1个回答

1
我已经研究了ViewPager的实现。方法ViewPager.determineTargetPage(int, float, int, int)确定滚动结束时的目标页面,但据我所见无法更改。
为了使其正常工作,我建议您使用一种解决方法,并创建一个包含2个水平视图的页面。在这种情况下,您应该删除
@Override
public float getPageWidth(int position) {
    return(0.5f);
}

编辑

为使此功能正常工作,请添加新的布局xml文件 related_article_item_page.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="horizontal"
          android:layout_width="match_parent"
          android:layout_height="match_parent">

    <include
        layout="@layout/related_article_item"
        android:id="@+id/article_1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"/>

    <include
        layout="@layout/related_article_item"
        android:id="@+id/article_2"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"/>
</LinearLayout>

然后修改你的ViewPagerAdapter.instatiateItem(ViewGroup, int)方法:

inflater = ...
View pageView = infalter.inflate(R.layout.related_article_item_page, container, false);

View itemView1 = pageView.findViewById(R.id.article_1);
View itemView2 = pageView.findViewById(R.id.article_2);

configureItemView(itemView1, calculateFirstItemDataPosition(position));
configureItemView(itemView2, calculateSecondItemDataPosition(position));

((ViewPager) container).addView(pageView);
return pageView;

configureItemView(View,int)中,您应该设置itemView视图数据,在calculateFirstItemDataPosition(int)calculateSecondItemDataPosition(int)中,您应该计算当前适配器位置的数据位置。还要记得更改getCount()方法-现在它会小2倍。

谢谢,你能帮我吗?如何创建一个包含两个水平视图的ViewPager?这对我很有帮助。 - y7876768
configureItemView 的实现应该从之前的 itemView 配置中复制(即绑定视图并从数据设置视图值的位置)。 - pjanecze
第一:位置 * 2 第二:位置 * 2 + 1 getCount():(int) Math.ceil((double)title.length/2) - pjanecze

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接