-1

In my Native Android project, I am making one reader . So, I am using one recyclerview to show the text content in cards and in the adapter class, inside the onBindViewHolder method, I have used parameter position to show the pageNo like below-

public void onBindViewHolder(RecyclerView.ViewHolder holder, int position)

Now, It shows the Page no. correctly when I scroll slowly, but when I scroll fast or scroll in the reverse direction then sometimes it is showing wrong position. I also have one Seekbar in my reader like below image-

enter image description here

When I move from one specific page to a page with a distance using seekbar, then it is also getting the position incorrectly.

Here's the xml part for each row in the recyclerview-

   <android.support.v7.widget.CardView
            xmlns:card_view="http://schemas.android.com/apk/res-auto"
            android:id="@+id/card_view_left"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            card_view:cardCornerRadius="5dp"
            card_view:contentPadding="10dp"
            card_view:cardBackgroundColor="@color/colorTransparent"
            card_view:cardElevation="4dp"
            card_view:cardMaxElevation="5dp"
            android:layout_marginBottom="12dp"
            android:layout_marginRight="10dp"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="-7dp"
            >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                >

            <TextView
                android:id="@+id/tvPageNo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"

                android:layout_gravity="right"
                android:text=""
                android:layout_marginRight="20dp"
                />

            <com.actinarium.aligned.TextView
                android:layout_marginStart="10dp"
                android:layout_marginEnd="10dp"
                android:id="@+id/tv_content"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"

                android:textAppearance="@style/TextAppearance.AppCompat.Body1"
                android:textSize="14sp"
                app:leading="16sp"
                app:firstLineLeading="24sp"
                android:layout_marginBottom="10dp"
                />

            </LinearLayout>

        </android.support.v7.widget.CardView>

And in the RecyclerAdapter class inside onBindViewHolder I have worked like that-

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        ((ViewHolderText) holder).tvContent.setText("");

        if(preparedPage[position]==null){

            ((ViewHolderText) holder).loadingIndicator.smoothToShow();
            PageViewFragment.rvPageView.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    return true;
                }
            });

            //Prepare page item as spannable string
            Flowable.fromCallable(() -> preparePage(position)).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(finalText -> {
                        ((ViewHolderText) holder).loadingIndicator.hide();

                        PageViewFragment.rvPageView.setOnTouchListener(new View.OnTouchListener() {
                            @Override
                            public boolean onTouch(View v, MotionEvent event) {
                                return false;
                            }
                        });

                        preparedPage[position] = finalText;

                        ((ViewHolderText) holder).tvContent.setText(finalText);
                        ((ViewHolderText) holder).tvPageNo.setText(""+holder.getAdapterPosition());

                    }, this::onError);

        }else {

            ((ViewHolderText) holder).loadingIndicator.hide();
            ((ViewHolderText) holder).tvContent.setText(preparedPage[position]);
            Log.d(TAG, "BOOK: "+preparedPage[position].length()+" "/*+preparedPage[position]*/);
        }

}

And I declared one method in my fragment class named updateView here's the implementation of that-

private void updateView() {
    seekBar.setMax(pagesItems.size() - 1);
    seekBar.setOnProgressChangeListener(
            new DiscreteSeekBar.OnProgressChangeListener() {
                @Override
                public void onProgressChanged(DiscreteSeekBar seekBar, int value, boolean fromUser) {

                    tvPageNumber.setText((seekBar.getProgress() + 1) + " of " + pagesItems.size());
                }

                @Override
                public void onStartTrackingTouch(DiscreteSeekBar seekBar) {}

                @Override
                public void onStopTrackingTouch(DiscreteSeekBar seekBar) {

                    rvPageView.scrollToPosition(seekBar.getProgress());
                    CURRENT_PAGE = seekBar.getProgress();
                    saveBookSettings();
                }
            });

    Log.d(TAG, "updateView: " + CURRENT_PAGE + " " + bookSettings.toString());
    tvPageNumber.setText((CURRENT_PAGE + 1) + " of " + pagesItems.size());
    rvPageView.scrollToPosition(CURRENT_PAGE);
    seekBar.setProgress(CURRENT_PAGE);
}

So, how can I track the number of each card in RecyclerView and show them correctly while scrolling too fast or changing with seekbar?

S. M. Asif
  • 3,437
  • 10
  • 34
  • 58

1 Answers1

1

try to add just this line to your Recycleradpater this will solve your problem

    @Override
    public int getItemViewType(int position) {
        return position;
    }
}
Muhammad Haroon
  • 210
  • 2
  • 11