Alaa AbuZarifa
Alaa AbuZarifa

Reputation: 1249

How to move between the elements of recyclerview inside its adapters

I couldn't phrase the question perfectly with my problem, but basically what I want to achieve is this:-

I have recyclerview of items that contains textview for example, when the item is clicked; a custom dialog will show containing the same content of the clicked element.

so far so good, in the same dialog there's two arrow (left & right) at each of its sides, the arrows buttons should navigate between the recyclerview items and update the content according to the new position.

Here's what I did:

  @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
    holder.tvTitle.setText(data.get(position).getTitle());

    holder.itemView.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View view) {

      //Initializing the custom dailog
        final Dialog dialog = new Dialog(context);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.getWindow() .setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        dialog.getWindow().getAttributes().gravity = Gravity.TOP;
        dialog.setContentView(R.layout.dailog);
        dialog.show();

        ImageView RightArrow = (ImageView) dialog.findViewById(R.id.dia_right_arrow);
        ImageView LeftArrow = (ImageView) dialog.findViewById(R.id.dia_left_arrow);
        final TextView textViewTitle = (TextView) dialog.findViewById(R.id.dia_tvTtile);


        textViewTitle.setText(data.get(position).getTitle());

        RightArrow.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {

              int curPosi = 0;  
               if (data.size() != 0) {
                 curPosi = position+1;
              }
         textViewTitle.setText(data.get(position).getTitle());
          }
        });

        LeftArrow.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {

            int curPosi = 0;
            if (data.size() != 0) {
              curPosi = position-1;
            } 
          textViewTitle.setText(data.get(position).getTitle());

          } });
      }});

     }

The Result:

When I click on the right arrow it works 100% , but if I click again nothing happens !!

And If click the left arrow at anytime it gives crash with this exception:

java.lang.ArrayIndexOutOfBoundsException: length=10; index=-1

Is it possible to achieve a loop effect, what I mean is when I reach the last index and the click again it shows the first index and vice versa..!

Any help?

Upvotes: 0

Views: 855

Answers (1)

Inducesmile
Inducesmile

Reputation: 2493

It seems the issue is from how you check your click limits. You can try this

Add curPosi as a class member variable

private int curPosi;

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
holder.tvTitle.setText(data.get(position).getTitle());



holder.itemView.setOnClickListener(new OnClickListener() {
  @Override
  public void onClick(View view) {

  curPosi = position

  //Initializing the custom dailog
    final Dialog dialog = new Dialog(context);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.getWindow() .setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
    dialog.getWindow().getAttributes().gravity = Gravity.TOP;
    dialog.setContentView(R.layout.dailog);
    dialog.show();

    ImageView RightArrow = (ImageView) dialog.findViewById(R.id.dia_right_arrow);
    ImageView LeftArrow = (ImageView) dialog.findViewById(R.id.dia_left_arrow);
    final TextView textViewTitle = (TextView) dialog.findViewById(R.id.dia_tvTtile);


    textViewTitle.setText(data.get(position).getTitle());

    RightArrow.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {

         int lastIndex = data.size()-1;

        if (lastIndex == curPosi ) { // this prevents the crash, so if the next button is clicked while the last index is showing it will reset and show the first one..!
          curPosi = 0;
        } else if(curPosi < data.size()) {
          curPosi++;
        }

     textViewTitle.setText(data.get(curPosi).getTitle());
      }
    });

    LeftArrow.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {

       //same thing here with this "if", when the previous button clicked from the first; it will show the last item..!  
         if (curPosi == 0) { 
          curPosi = data.size();
        }

        if (curPosi > 0) {
          curPosi--;
        }

      textViewTitle.setText(data.get(curPosi).getTitle());

      } });
  }});

 }

Upvotes: 3

Related Questions