RecyclerView滚动进行更改 [英] RecyclerView scrolling makes changes

查看:84
本文介绍了RecyclerView滚动进行更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个RecyclerView,它具有一个自定义适配器,可以使rowLayout膨胀.每行包含一个CardView,其中包含一些文本和图像作为按钮.特别是我有一个喜欢"按钮(ImageView),到目前为止,该按钮仅应在单击时更改imageResource.

I have a RecyclerView which has a custom adapter that inflates a rowLayout. Each Row contains a CardView and in it some text and images as buttons. In particular i have a "like" button(ImageView) that as for now only is supposed to change imageResource on click.

我能够在onBindViewHolder()方法中设置一个onClicklistener,并且当我单击按钮时它确实会注册,但是它不仅会更改该按钮的图像,还会更改其他一些按钮,例如按钮我向下滚动,然后再次向上滚动,第一个赞"按钮有时会重置.

I was able to set an onClicklistener in the onBindViewHolder() method and it does indeed register when i click the button, however it not only changes that buttons image but it also changes some of the other like buttons and also when i scroll down and then up again the first like button sometimes get reset.

这是我的适配器代码.

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> implements View.OnClickListener {

    private ArrayList<WallPost> wallPosts;
    @Override
    public void onClick(View v) {

    }
    public static class ViewHolder extends RecyclerView.ViewHolder {
        public View view;
        public ViewHolder(View v) {
            super(v);
            view = v;
        }
    }
    public MyAdapter(ArrayList<WallPost> wallPosts) {
        this.wallPosts = wallPosts;
    }
    @Override
    public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                   int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_layout, parent, false);
        ViewHolder vh = new ViewHolder(v);
        return vh;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        TextView name = (TextView) holder.view.findViewById(R.id.person_name);
        TextView age = (TextView) holder.view.findViewById(R.id.person_age);
        ImageView profile = (ImageView) holder.view.findViewById(R.id.person_photo);

        TextView description = (TextView) holder.view.findViewById(R.id.description_text);
        AppCompatImageView mainImage = (AppCompatImageView) holder.view.findViewById(R.id.main_image);

        final ImageView likeButton = (ImageView) holder.view.findViewById(R.id.like_button);

        name.setText(wallPosts.get(position).getName());
        age.setText(wallPosts.get(position).getAge());
        profile.setImageResource(wallPosts.get(position).getPhotoId());

        description.setText(wallPosts.get(position).getDescription());
        mainImage.setImageResource(wallPosts.get(position).getMainPhotoId());

        likeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                likeButton.setImageResource(R.drawable.favourite_red);
            }
        });
    }

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

这是项目布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/cv"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginBottom="10dp"
    card_view:cardCornerRadius="5dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="8dp"
        >

        <de.hdodenhof.circleimageview.CircleImageView
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:id="@+id/person_photo"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginRight="16dp"
            android:src="@drawable/placeholderprofilepic"
            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/person_name"
            android:text="Name"
            android:layout_toRightOf="@+id/person_photo"
            android:layout_alignParentTop="true"
            android:textSize="30sp"
            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/person_age"
            android:text="19"
            android:layout_toRightOf="@+id/person_photo"
            android:layout_below="@+id/person_name"
            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/description_text"
            android:text="This is the description of the image"
            android:layout_below="@id/person_age"/>


        <android.support.v7.widget.AppCompatImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/main_image"
            android:src="@drawable/placeholderfoodimage"
            android:layout_below="@+id/description_text"
            android:elevation="4dp"/>


        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/like_button"
            android:src="@drawable/favoruite_hollow"
            android:layout_marginTop="10dp"
            android:layout_below="@+id/main_image"
            />

    </RelativeLayout>

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

推荐答案

这是由于回收视图所致.您需要修改您的getView()方法并执行以下两项操作-

This is due to recycling of views . You need to modify your getView() method and do these 2 things -

  1. 修改您的WallPost类,使其具有用于设置like标志的标志.在onClickListener中设置此标志并更新wallPosts数据集.

  1. Modify your WallPost class to have a flag to set the like flag. Set this flag in onClickListener and update your wallPosts dataset .

wallPosts.get(position).setLike(true);

每次根据您在列表对象中拥有的like标志设置喜欢/不喜欢"图像资源-

Set the like/not like image resource everytime based on the like flag you own in your list objects -

if(wallPosts.get(position).isLiked()){ likeButton.setImageResource(R.drawable.favourite_red); } else{ likeButton.setImageResource(R.drawable.favoruite_hollow); }

if(wallPosts.get(position).isLiked()){ likeButton.setImageResource(R.drawable.favourite_red); } else{ likeButton.setImageResource(R.drawable.favoruite_hollow); }

这篇关于RecyclerView滚动进行更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆