Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Previous data is showing on ViewPager Android

In my app, I have a ViewPager with CirclePageIndicator and it's working. It's show two items in One pager. When I swipe to another pager it's show previous one item. I dont want it. I want to be swipe two item in a time. doesn't show the previous one that are already shown on pager.

Here is my previous question, it's related to this. Show two item in ViewPager android.

Currently working like
ItemA, ItemB | ItemB, ItemC | ItemC, ItemD |

I want to be like
ItemA, ItemB | ItemC, ItemD | ItemE, ItemF |

This is the code i used to show two items in one 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);

    }


}

Activity

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

Log Files

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

Thanks

like image 948
y7876768 Avatar asked Jul 14 '15 05:07

y7876768


Video Answer


1 Answers

I've looked into ViewPager implementation. Method ViewPager.determineTargetPage(int, float, int, int) determine target page when scroll ended and as I see it is not possible to change it.

To make this work I would suggest you a workaround and creating one page, that have 2 horizontal views inside. In that case you should remove

@Override
public float getPageWidth(int position) {
    return(0.5f);
}

EDIT

To make this work, add new layout 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>

Then change your ViewPagerAdapter.instatiateItem(ViewGroup, int) method:

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;

in configureItemView(View, int) you should set itemView views data and in calculateFirstItemDataPosition(int) calculateSecondItemDataPosition(int) you should calculate data position for current adapter position. Also remember to change getCount() method - now it would be 2x smaller

like image 175
pjanecze Avatar answered Oct 29 '22 03:10

pjanecze