我有两个标签。第一个是RecyclerView,第二个是ViewPager。在RecyclerView的第一项中是一个简短的描述,在ViewPager的第一页中,只是描述不短但详细的相同内容。对于第二项和第三项等等,它是相同的。
所以我的问题是,我可以点击RecyclerView的项目而不是视图只是切换到ViewPager中的相同项目吗?
ViewPager适配器
public class ViewPagerAdapter extends PagerAdapter {
// Declare Variables
Context context;
String[] rezeptTitel;
String[] kcal;
String[] zubereitung;
int[] rezeptImages;
LayoutInflater inflater;
public ViewPagerAdapter(Context context, String[] rezeptTitel, String[] kcal,zubereitung, int[] rezeptImages) {
this.context = context;
this.rezeptTitel = rezeptTitel;
this.kcal = kcal;
this.zubereitung = zubereitung;
this.rezeptImages = rezeptImages;
}
@Override
public int getCount() {
return rezeptTitel.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((LinearLayout) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
// Declare Variables
TextView txtrezeptTitel;
TextView txtkcal;
TextView txtzubereitung;
ImageView imagerezept;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.rezepte_fragment_swipeview, container,
false);
// Locate the TextViews in viewpager_item.xml
txtrezeptTitel = (TextView) itemView.findViewById(R.id.rezeptTitel);
txtkcal = (TextView) itemView.findViewById(R.id.kcal);
txtzubereitung = (TextView) itemView.findViewById(R.id.zubereitung);
// Capture position and set to the TextViews
txtrezeptTitel.setText(rezeptTitel[position]);
txtkcal.setText(kcal[position]);
txtzubereitung.setText(zubereitung[position]);
// Locate the ImageView in viewpager_item.xml
imagerezept = (ImageView) itemView.findViewById(R.id.image_rezepte_basic);
// Capture position and set to the ImageView
imagerezept.setImageResource(rezeptImages[position]);
// Add viewpager_item.xml to ViewPager
((ViewPager) container).addView(itemView);
return itemView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// Remove viewpager_item.xml from ViewPager
((ViewPager) container).removeView((RelativeLayout) object);
}
和我的RecyclerView适配器
public class RezepteAdapter extends RecyclerView.Adapter<RezepteAdapter.MyViewHolder> {
private LayoutInflater inflater;
List<Information> data = Collections.emptyList();
public RezepteAdapter(Context context, List<Information> data) {
inflater = LayoutInflater.from(context);
this.data = data;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.ernaehrungsplan_custom_row, parent, false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Information current = data.get(position);
holder.title.setText(current.title);
}
@Override
public int getItemCount() {
return data.size();
}
class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView title;
ImageView icon;
public MyViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.listText3);
icon = (ImageView) itemView.findViewById(R.id.listIcon3);
icon.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Context context = itemView.getContext();
Intent intent = new Intent(context, Ernaehrung.class);
context.startActivity(intent);
}
}
答案 0 :(得分:0)
将方法addOnItemTouchListener
调用到RecyclerView。此事件将为您提供所单击项目的位置。然后,在ViewPager上调用setCurrentItem
将寻呼机移动到该位置。