如何在android中点击按钮时显示视图寻呼机的特定页面

时间:2012-08-03 19:48:23

标签: android android-viewpager

我正在开发一个项目,我在一个屏幕上使用View Pager,如下所示:

enter image description here

但是你可以看到,有一个上层布局,其中列出了根据显示的页面选择的年份。当我们滑动视图寻呼机时,年份也将根据显示的页面索引更改为下一个。我必须在几年的TextViews上实现Click操作,在其上将显示相应的View Pager Page。但它怎么能实施呢?意味着当我们调用另一个View时,我们如何在View Pager中显示特定的页面索引。意味着当我们选择1998年时,页面将显示对应于1998年。我已经编写了代码,但这对我不起作用。

代码:

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提前。

1 个答案:

答案 0 :(得分:17)

您应该能够在OnClickListener中使用setCurrentItem方法(下面的语法和文档),并且您的viewpager会自动滚动(如果smoothScroll未停用)新职位。

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

在这种情况下,您将索引替换为您想要的那个。因此,您可能还需要一个存储年份和页面之间关系的数组,或者您可以在年份中设置TAG(比如说位置),然后在OnClickListener上可以检索TAG转到该位置。

请参阅ViewPager docs了解详情