我正在开发一个项目,我在一个屏幕上使用 View Pager,如下所示:
但是你可以在那里看到,有一个上面的布局,其中列出了根据显示的页面选择的年份。当我们滑动视图寻呼机时,年份也会根据显示页面的索引更改为下一个年份。我必须对将显示相应 View Pager Page 的年份的 TextView 执行 Click 操作。但如何实施呢?意味着当我们点击另一个视图时,我们如何在 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;
}
提前谢谢。