滚动时如何防止`RecyclerView`复制?

时间:2019-02-15 10:48:55

标签: java android android-recyclerview recycler-adapter

我在我的block console_content .sensor-page(ng-controller="ambientCtrl") script(src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.4.0/Chart.bundle.min.js") script(type='text/javascript', src='https://cdn.jsdelivr.net/jquery/latest/jquery.min.js') script(type='text/javascript', src='https://cdn.jsdelivr.net/momentjs/latest/moment.min.js') script(type='text/javascript', src='https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.min.js') link(rel='stylesheet', type='text/css', href='https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.css') h1 Sensors // Date-Time Widget div(class="container") div(class="row") div(class="col-sm-6") div(class="form-group") div(class='input-group date' id='datetimepicker1') input(type='text' class="form-control") span(class="input-group-addon") span.glyphicon.glyphicon-calendar script(type="text/javascript"). $( document ).ready(function () {$('#datetimepicker1').datetimepicker()}); // Sensor Checkbox Widget p {{selection}} label(ng-repeat='sensorName in sensors') input(type='checkbox', name='selectedSenors[]', value='{{sensorName}}', ng-checked='selection.indexOf(sensorName) > -1', ng-click='toggleSelection(sensorName)') | {{sensorName}} button(ng-click="changeSensors()") Show canvas(id="myChart") h1 Sensor p Info about sensors here上显示出爱意标志。 当用户单击时,爱号的颜色从默认的灰色变为红色,但是当我滚动时,它在其他一些位置显示颜色(未单击)。此外,单击的颜色将变为灰色。

我找到了大量解决方案,但对我没有任何帮助。 请帮忙。 我给你我的密码

特色产品类

RecyclerView

3 个答案:

答案 0 :(得分:0)

在文件中替换以下代码,然后尝试

       if(wishlist!=null)
        {
            if(wishlist.equals("1"))   // change is here
            {
                featuredView.img_wishlist.setColorFilter(Color.argb((255,255,0,0));
                featuredView.txt_count.setText("1");
            }
            else (wishlist.equals("0"))   // change is here
            {
                featuredView.img_wishlist.setColorFilter(Color.argb(255,255,111,111));
                featuredView.txt_count.setText("0");
            }
        }
        else
        {
            featuredView.img_wishlist.setColorFilter(Color.argb(255, 211, 211, 211));
            featuredView.txt_count.setText("0");
        }

也为此

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

                featured_data.get(getAdapterPosition).setWishlist(featured_data.get(getAdapterPosition).getWishlist().equals("0")?"1":"0");
                notifyItemChanged(getAdapterPosition());
                    onclickListener.wishlistOnclick(v,getAdapterPosition());
                }
            });

答案 1 :(得分:0)

您需要使用SparseBooleanArray保持视图状态。这是因为在RecylerView中,该项目将与先前的视图一起显示(如果存在),即该视图将在使用之前首先被回收。因此,RecyclerView的均值。

您可以执行以下操作:

首先,在您的适配器中,添加SparseBooleanArray来保存标志(请阅读代码中的注释):

public class FeaturedlistAdapter extends RecyclerView.Adapter<FeaturedlistAdapter.FeaturedView>
{
    private Context context;
    private List<Featured_data> featuredData;
    private SparseBooleanArray mSelectedFlags;

    public FeaturedlistAdapter(Context context, List<Featured_data> featured_data,MyAdapterListener  myAdapterListener) {
        ...

        // init the flag
        mSparseBooleanArray = new SparseBooleanArray();

    }

    @Override
    public void onBindViewHolder(@NonNull FeaturedView featuredView, int i) {
       int position = featuredView.getAdapterPosition();
       Featured_data data = featuredData.get(position);

       // reset the state of the view each time the 
       // view get recycled by applying the flag.
       if(mSelectedFlags.get(position)) {
          // set the selected state, i.e red
       } else {
          // SparseBooleanArray.get(i) is false if not found
          // set the unselected state, i.e grey
       }
}

然后,在您的ViewHolder中,设置单击/选择成员视图时的状态:

class FeaturedView extends RecyclerView.ViewHolder {

    ...

    FeaturedView(@NonNull View itemView) {
        ...
        img_wishlist.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              ...
              // save the selected state
              mSparseBooleanArray.put(getAdapterPosition(), true);

            }
         ...
    }
}

现在,将使用SparseBooleanArray标志持有者的保存语句重置视图的选定/未选定状态。

答案 2 :(得分:0)

with open(cur_dir+"/data/GeoLite2-City-Locations.csv", "r", encoding="utf-8") as info: