Sanat Pandey
Sanat Pandey

Reputation: 4103

How to display particular page of view pager on button click in android

I am developing a Project in which I am using View Pager on one screen, as you can see below:

enter image description here

But you can see there, there is an upper layout in which years are listed which are selected according to the page displayed. When we swipe a view pager then the year will also be changed to the next one according to the index of the page displayed. I have to implement the Click operation on that TextViews of years on which the corresponding View Pager Page will be displayed. But how can it will be implemented? Means How can we display particular index of page in View Pager when we call on click of another View. Means when we select the year 1998 then page will be displayed corresponding to 1998. I have written a code but this is not working for me.

Code:

for (int i = 0; i < (Overview_Years.size()); i++) {
                tvYear[i] = new TextView(Overview_New.this);
                img_Pagination[i] = new ImageView(Overview_New.this);

                //yearValue = Overview_Years.get(i).toString();
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                        LayoutParams.WRAP_CONTENT,
                        LayoutParams.WRAP_CONTENT);
                // params.weight = 0.5f;

                params.gravity = Gravity.CENTER_VERTICAL;
                params.setMargins(40, 4, 0, 0);


                // tvYear[i].setLayoutParams(new
                // LayoutParams(LayoutParams.WRAP_CONTENT,
                // LayoutParams.WRAP_CONTENT));
                tvYear[i].setLayoutParams(params);

                // tvYear[i].setText(Overview_Years.get(i).toString());
                tvYear[i].setText(Overview_Years.get(i).toString());
                tvYear[i].setTag(Overview_Years.get(i).toString());

                tvYear[i].setGravity(Gravity.CENTER);
                tvYear[i].setTypeface(null, Typeface.BOLD);
                tvYear[i].setTextColor(Color.WHITE);
                tvYear[i].setId(i);
                tvYear[i].setPadding(2, 0, 1, 17);
                //tvYear[i].setTag(Overview_Years.get(i-1).toString());
                final TextView tv = tvYear[i];
                **tvYear[i].setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        for(int i=0;i<((ViewGroup)yearHorizontalScrollView.getChildAt(0)).getChildCount();i++){
                            if(((TextView)arg0)== ((ViewGroup)yearHorizontalScrollView.getChildAt(0)).getChildAt(i)){
                                currentposition = i;

                            }
                        }**

                        onYearSelect(((TextView)arg0).getText().toString());
                    }

                });


                layout_Years.addView(tvYear[i]);

                LinearLayout.LayoutParams paramsPagination = new LinearLayout.LayoutParams(
                        LayoutParams.WRAP_CONTENT,
                        LayoutParams.WRAP_CONTENT);
                // params.weight = 0.5f;

                paramsPagination.gravity = Gravity.CENTER_VERTICAL;
                paramsPagination.setMargins(5, 2, 0, 0);
                img_Pagination[i].setLayoutParams(paramsPagination);
                /*if (i == 0) {
                    img_Pagination[0]
                            .setBackgroundResource(R.drawable.pagination_selected);
                } else {*/
                    img_Pagination[i]
                            .setBackgroundResource(R.drawable.pagination_unselected);
                //}
                img_Pagination[i].setId(i);
                layout_Pagination.addView(img_Pagination[i]);

                tvYear[0].setBackgroundResource(R.drawable.selector_x);
                /*tvYear[0]
                        .setBackgroundResource(R.drawable.selector_x);*/
                //tv_Overview_AllYears.setBackgroundResource(R.drawable.selector_bg_big_new);
                count=-1;

            }

Thnx in advance.

Upvotes: 3

Views: 8094

Answers (1)

FoamyGuy
FoamyGuy

Reputation: 46856

you should be able to used the setCurrentItem method (syntax and docs below) inside the OnClickListener, and your viewpager automatically will scroll (if smoothScroll is not deactivated) to the new position.

ViewPager.setCurrentItem(int index);
OR
ViewPager.setCurrentItem(int index, boolean smoothScroll);

In this case you will substitute the index for the one you would like. So you may also need an array where you store the relationship between the year and the page, or you can set a TAG (lets say the position) in the years and the on the OnClickListener you can retrieve that TAG an go to that position.

see the ViewPager docs for more info

Upvotes: 17

Related Questions