0

我的回收站视图有问题。当我向下滚动时一切正常,但是当我滚动回顶部时,我设置为图像的可绘制对象正在更改。这是我的适配器代码:

    @Override
    public FeedsHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.feed_item, parent, false);

        final FeedsHolder holder = new FeedsHolder(view);

        //Query

        QueryOptions options = new QueryOptions();

        options.setRelated(Arrays.asList("usersThatLike"));

        BackendlessDataQuery query = new BackendlessDataQuery();

        query.setQueryOptions(options);

        Backendless.Data.of(Feeds.class).find(query, new AsyncCallback<BackendlessCollection<Feeds>>() {

            @Override

            public void handleResponse(BackendlessCollection<Feeds> response) {
                String userId = Backendless.UserService.CurrentUser().getObjectId();
                for (Feeds feed : response.getData()) {
                    feed = list.get(holder.getAdapterPosition());

                    List<BackendlessUser> likedUsers = feed.getUsersThatLike();
                    for (BackendlessUser user : likedUsers)
                        if (user.getObjectId().equals(userId)) {
    //** GOOGLE MATERIAL ICON WITH FILLED HEART
                            setBackground(holder.like, ContextCompat.getDrawable(context, R.drawable.ic_favorite_black_24dp));
                        } else if (!user.getObjectId().equals(userId)) {
    //** GOOGLE MATERIAL ICON WITH EMPTY HEART
                            setBackground(holder.like, ContextCompat.getDrawable(context, R.drawable.ic_favorite_border_black_24dp));
                        }
                }

            }

            @Override

            public void handleFault(BackendlessFault backendlessFault) {

            }

        });
        return holder;
    }

而不是我在 onBindViewHolder 中设置了 holder.like imageview 的默认图像:

    @Override
    public void onBindViewHolder(final FeedsHolder holder, final int position) {
        holder.like.setBackground(ContextCompat.getDrawable(context, R.drawable.ic_favorite_border_black_24dp));
    }

我认为这就是问题所在......

但我不知道如何解决这个问题,因为没有这行代码,我的其他行 recyclerview 将有不同的图像:有些会充满心,有些会空。

所以请帮我找到解决方案。

编辑:发布我的适配器代码

    public class FeedAdapter extends RecyclerView.Adapter<FeedAdapter.FeedsHolder> {

    private List<Feeds> list;
    private Context context;
    private static List<Long> selectedIds;
    List<BackendlessUser> backendlessUserList;
    private List<Comments> cake = new ArrayList<>();
    private static String TAG = "MainActivity";

    public FeedAdapter(Context context, List<Feeds> list) {
        this.context = context;
        this.list = list;

    }

    @Override
    public FeedsHolder onCreateViewHolder(ViewGroup parent, final int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.feed_item, parent, false);

        final FeedsHolder holder = new FeedsHolder(view);

        //Query

        QueryOptions options = new QueryOptions();

        options.setRelated(Arrays.asList("usersThatLike"));

        BackendlessDataQuery query = new BackendlessDataQuery();

        query.setQueryOptions(options);

        Backendless.Data.of(Feeds.class).find(query, new AsyncCallback<BackendlessCollection<Feeds>>() {

            @Override

            public void handleResponse(BackendlessCollection<Feeds> response) {
                myFeeds = response;
                String userId = Backendless.UserService.CurrentUser().getObjectId();
                for (Feeds feed : response.getData()) {
                    feed = list.get(holder.getAdapterPosition());

                    List<BackendlessUser> likedUsers = feed.getUsersThatLike();
                    backendlessUserList = likedUsers;
                    for (BackendlessUser user : likedUsers)
                        if (user.getObjectId().equals(userId)) {
                            setBackground(holder.like, ContextCompat.getDrawable(context, R.drawable.ic_favorite_black_24dp));
                        } else if (!user.getObjectId().equals(userId)) {
                            setBackground(holder.like, ContextCompat.getDrawable(context, R.drawable.ic_favorite_border_black_24dp));
                        }
                }

            }

            @Override

            public void handleFault(BackendlessFault backendlessFault) {

            }

        });
        return holder;
    }

    @Override
    public void onBindViewHolder(final FeedsHolder holder, final int position) {
        //Starting Feeds
        final Feeds feeds = list.get(position);

        holder.like.setBackground(ContextCompat.getDrawable(context, R.drawable.ic_favorite_border_black_24dp));

        //Name
        holder.name.setText(feeds.getOwner());

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class FeedsHolder extends RecyclerView.ViewHolder {
        //Setup Views
        CardView card;
        TextView name;

        public FeedsHolder(View itemView) {
            super(itemView);

            //Assign views by ID
            card = (CardView) itemView.findViewById(R.id.cv);
            name = (TextView) itemView.findViewById(R.id.commenter);
        }
    }
}
4

1 回答 1

0

您需要将单元格的状态(例如需要显示的图像)保存在某处。一种选择是拥有一个与数据源大小相同的列表,并存储一个值,指示该位置的元素是空心、满心还是什么都没有,最后在 中检查该值onBindViewHolder并更新视图正确。

编辑:这是一个例子:

public class BreweryRecyclerViewAdapter
       extends RecyclerView.Adapter<BreweryRecyclerViewAdapter.BreweryRecyclerViewViewHolder>
       implements View.OnClickListener {

    private static List<Long> selectedIds;

    private Context context;
    private List<Brewery> breweries;
    private View.OnClickListener clickListener;

    public BreweryRecyclerViewAdapter(Context context) {
        clearBreweries();
        this.context = context;
    }

    public BreweryRecyclerViewAdapter(List<Brewery> breweries) {
        setBreweries(breweries);
    }

    public List<Brewery> getBreweries() {
        return breweries;
    }

    public void setBreweries(List<Brewery> breweries) {
        this.breweries = breweries;
        selectedIds = new ArrayList<>();
    }

    public void setClickListener(View.OnClickListener clickListener) {
        this.clickListener = clickListener;
    }

    public void clearBreweries() {
        this.breweries = Collections.emptyList();
        selectedIds = new ArrayList<>();
    }

    @Override
    public BreweryRecyclerViewViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        View itemView = layoutInflater.inflate(R.layout.item_brewery, parent, false);
        itemView.setOnClickListener(this);
        return new BreweryRecyclerViewViewHolder(context, itemView);
    }

    @Override
    public void onBindViewHolder(BreweryRecyclerViewViewHolder holder, int position) {
        Brewery brewery = breweries.get(position);
        if (selectedIds.contains(brewery.getId())) {
            holder.openSecondaryLayoutVisibility();
        } else {
            holder.closeSecondaryLayoutVisibility();
        }
        holder.update(brewery);
    }

    @Override
    public int getItemCount() {
        return breweries.size();
    }

    @Override
    public void onClick(View view) {
        if (clickListener != null) {
            clickListener.onClick(view);
        }
    }

    public static class BreweryRecyclerViewViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.brewery_name)
        TextView breweryName;

        @BindView(R.id.brewery_distance)
        TextView breweryDistance;

        @BindView(R.id.brewery_duration)
        TextView breweryDuration;

        @BindView(R.id.brewery_time)
        TextView breweryTime;

        @BindView(R.id.brewery_is_happy_hour)
        ImageView breweryIsHappyHour;

        @BindView(R.id.secondary)
        RelativeLayout secondaryLayout;

        @BindView(R.id.brewery_description)
        TextView breweryDescription;

        private Context context;
        private Brewery brewery;

        public BreweryRecyclerViewViewHolder(Context context, View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            this.context = context;
        }

        public Brewery getBrewery() {
            return brewery;
        }

        public void toggleSecondaryLayoutVisibility(Long id) {
            if (secondaryLayout.getVisibility() != View.VISIBLE) {
                openSecondaryLayoutVisibility();
                selectedIds.add(id);
            } else {
                closeSecondaryLayoutVisibility();
                selectedIds.remove(id);
            }
        }

        public void openSecondaryLayoutVisibility() {
            secondaryLayout.setVisibility(View.VISIBLE);
        }

        public void closeSecondaryLayoutVisibility() {
            secondaryLayout.setVisibility(View.GONE);
        }

        @OnClick(R.id.view_route_button)
        public void viewRouteButtonClicked(Button button) {
            String latitude = brewery.getLocation().getLatitude().toString();
            String longitude = brewery.getLocation().getLongitude().toString();
            Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(
                    String.format("http://maps.google.com/maps?daddr=%s,%s", latitude, longitude)));
            context.startActivity(intent);
        }

        public void update(Brewery brewery) {
            breweryName.setText(brewery.getName());
            if (brewery.getDistanceHumanReadable() == null) {
                breweryDistance.setVisibility(View.GONE);
            } else {
                breweryDistance.setText(String.format("%s | ", brewery.getDistanceHumanReadable()));
                breweryDistance.setVisibility(View.VISIBLE);
            }
            if (brewery.getDurationHumanReadable() == null) {
                breweryDuration.setVisibility(View.GONE);
            } else {
                breweryDuration.setText(String.format("%s | ", brewery.getDurationHumanReadable()));
                breweryDuration.setVisibility(View.VISIBLE);
            }
            breweryTime.setText(String.format("%shs a %shs", brewery.getOpen(), brewery.getClose()));
            breweryIsHappyHour.setVisibility(brewery.isHappyHour() ? View.VISIBLE : View.GONE);
            breweryDescription.setText(brewery.getDescription());

            this.brewery = brewery;
        }

    }

}

把注意力放在onBindViewHolder和 上toggleSecondaryLayoutVisibility,看看使用selectedIds来保持单元格状态。

于 2016-04-25T17:09:12.470 回答