2

I have two tabs. The first is a RecyclerView and the second one is a ViewPager. In the first item of the RecyclerView is a short description and in the first page of the ViewPager is the same content only that the description is not short but in detail. With the second item and third item and so on it's the same.

So my Question is, can I click on a item of the RecyclerView and than the view just switches to the same item in the ViewPager?

ViewPager adapter

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

    }

and my RecyclerView adapter

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

        }
    }
SerOsDeveloper
  • 253
  • 1
  • 3
  • 12

1 Answers1

0

Call the method addOnItemTouchListener to your RecyclerView. This event will give you the position of the item that was clicked. Then, call setCurrentItem on the ViewPager to move the pager to that position.

Greg Ennis
  • 14,917
  • 2
  • 69
  • 74
  • Can you please make an example? I can't find what to do with the `addOnItemTouchListener`. – SerOsDeveloper Jan 12 '15 at 14:02
  • I have a similar situation, however my, however my viewPager adapter is on a different fragment than the recylerview adapter. How can I access the viewPager adapter from the recyclerview adapter? http://stackoverflow.com/q/32751281/2990066 – ShahNewazKhan Sep 24 '15 at 00:19