Recyclerview Changing Items During Scroll

前端 未结 15 705
我在风中等你
我在风中等你 2020-11-27 10:38

I have a RecyclerView. Each row has a play button, textview and Progressbar. when click on the play button have to play audio from my sdcard and have to progress Progressbar

相关标签:
15条回答
  • 2020-11-27 11:03

    Add setHasStableIds(true); in your adapter constructor and Override these two methods in adapter. It also worked if anyone using a RecyclerView inside a ViewPager which is also inside a NestedScrollView.

    @Override
    public long getItemId(int position) {
                return position;
    }
    
    @Override
    public int getItemViewType(int position) {
           return position;
    }
    
    0 讨论(0)
  • 2020-11-27 11:06

    As the name implies, the views in a RecyclerView are recycled as you scroll down. This means that you need to keep the state of each item in your backing model, which in this case would be a Historyitem, and restore it in your onBindViewHolder.

    1) Create position, max, and whatever other variables you need to save the state of the ProgressBar in your model.

    2) Set the state of your ProgressBar based on the data in your backing model; on click, pass the position of the item to your FileDownload/StartMediaPlayer methods.

    public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, int position) {
        final Historyitem dataItem = stethitems.get(position);
        final MyViewHolder myViewHolder = (MyViewHolder) viewHolder;
        myViewHolder.progressplay.setMax(dataItem.getMax());
        myViewHolder.progressplay.setProgress(dataItem.getPosition());
        ...
        myViewHolder.stethstreamplay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                   FileDownload(dataItem.getmsg(), position);
            }
        });
    

    3) Update the progress bar by updating the backing model and notifying that it was changed.

    stethitems.get(position).setPosition(mPlayer.getCurrentPosition());
    notifyItemChanged(position);
    
    0 讨论(0)
  • 2020-11-27 11:08

    recyclerview.setItemViewCacheSize(YourList.size());

    0 讨论(0)
  • 2020-11-27 11:08

    try this

    @Override public void smoothScrollToPosition(RecyclerView    recyclerView, RecyclerView.State state,
           int position) {    LinearSmoothScroller linearSmoothScroller =
               new LinearSmoothScroller(recyclerView.getContext()) {
                   @Override
                   public PointF computeScrollVectorForPosition(int targetPosition) {
                       return LinearLayoutManager.this
                               .computeScrollVectorForPosition(targetPosition);
                   }
               };    linearSmoothScroller.setTargetPosition(position);    startSmoothScroll(linearSmoothScroller); }
    

    see this also

    0 讨论(0)
  • 2020-11-27 11:09

    Put recylerView in a NestedScroll View in your XML and add the property nestedScrollingEnabled = false.

    And on your adapter onBindViewHolder add this line

    final MyViewHolder viewHolder = (MyViewHolder)holder;
    

    Use this viewHolder object with your views to setText or do any kind of click events.

    e.g viewHolder.txtSubject.setText("Example");

    0 讨论(0)
  • 2020-11-27 11:09

    This is the expected behaviour of recyclerView. Since the view is recycled your items may get into random views. To overcome this you have to specify which item is put into which kind of view by yourself. This information can be kept in a SparseBooleanArray. what you can do is create a SparseBooleanArray in your adapter like this

    SparseBooleanArray selectedItems = new SparseBooleanArray();
    

    whenever your view changes, do:

    selectedItems.put(viewItemIndex,true);
    

    Now in your onBindViewHolder do

     if(selectedItems.get(position, false)){
        //set progress bar of related to the view to desired position
        }
        else {
            //do the default
        }
    

    This is the basic to solve your problem. You can adjust this logic to any kind of similar problem in recyclerView.

    0 讨论(0)
提交回复
热议问题