RecyclerView上选定项的错误

3
我已经在RecyclerView数据中实现了一个HorizontalScrollView,问题是在我的Adapter代码中,我实现了一个逻辑,当一个项目被点击时,它会放大。问题显然出现在这个video上,我不知道发生了什么-我用一个带有booleanint的类测试了一切,说这个项目被点击了,然后在onBindViewHolder上询问这个项目,如果它被点击了,那么再次缩放,如果没有,那么缩放。 我知道这很令人困惑,但视频可以帮助解释。 我的list_row.xml是:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    >

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/RLimage"
        android:layout_centerHorizontal="true"
        android:layout_centerInParent="true"
        >

        <ImageView
            android:layout_centerInParent="true"
            android:id="@+id/thumbnail"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@mipmap/ic_launcher"
            />

    </RelativeLayout>
    <RelativeLayout
        android:layout_centerHorizontal="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/RLimage"
        >
        <TextView
            android:layout_marginTop="14dp"
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:textColor="#222"
            android:textSize="12sp"/>
    </RelativeLayout>


</RelativeLayout>

我有一个 RecyclerView 的片段如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<FrameLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:layout_weight="1">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rcyList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="-20dp"
        android:paddingLeft="8dp"
        android:paddingRight="8dp" />

</FrameLayout>

这是我的Fragment中的onCreateView()

@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
    this.mContext = getActivity();
    View rootView = inflater.inflate(R.layout.fragment_carta, container, false);
    rv = (RecyclerView) rootView.findViewById(R.id.rcyList);
    CustomLinearLayoutManager layoutManager = new CustomLinearLayoutManager(mContext);
    layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
    rv.setLayoutManager(layoutManager);
    // Adding code here
    dataModelList = new ArrayList<dataModel>();
    dataModelList.add(new dataModel(ContextCompat.getDrawable(mContext, R.mipmap.ic_launcher),"1234"));
    dataModelList.add(new dataModel(ContextCompat.getDrawable(mContext, R.mipmap.ic_launcher),"1234"));
    dataModelList.add(new dataModel(ContextCompat.getDrawable(mContext, R.mipmap.ic_launcher),"1234"));
    dataModelList.add(new dataModel(ContextCompat.getDrawable(mContext, R.mipmap.ic_launcher),"1234"));
    dataModelList.add(new dataModel(ContextCompat.getDrawable(mContext, R.mipmap.ic_launcher),"1234"));
    dataModelList.add(new dataModel(ContextCompat.getDrawable(mContext, R.mipmap.ic_launcher),"1234"));
    dataModelList.add(new dataModel(ContextCompat.getDrawable(mContext, R.mipmap.ic_launcher),"1234"));
    dataModelList.add(new dataModel(ContextCompat.getDrawable(mContext, R.mipmap.ic_launcher),"1234"));
    dataModelList.add(new dataModel(ContextCompat.getDrawable(mContext, R.mipmap.ic_launcher),"1234"));
    adapter = new MyRecyclerViewAdapter(mContext, dataModelList);
    rv.setAdapter(adapter);

    return rootView;

}

而这个 adapter 就是这样的:

public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.CustomViewHolder> {

private Context mContext;
View animatedView = null;
private List<dataModel> dataModelList;
int animatedIndex = -1; // Initially no view is clicked so -1
//private PopulateListView populateListview;


public MyRecyclerViewAdapter(Context context, List<dataModel> items) {
    this.dataModelList = items;
    this.mContext = context;
    //this.populateListview = populateListview;
}

@Override
public CustomViewHolder onCreateViewHolder(ViewGroup viewGroup, final int i) {
    //View per each row
    final View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_row, null);
    CustomViewHolder viewHolder = new CustomViewHolder(view);
    view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (animatedView == null) {
                animatedView = view;
            } else {
                animatedView.setAnimation(null);
                animatedView = view;
            }
            ScaleAnimation fade_in = new ScaleAnimation(1f, 1.2f, 1f, 1.2f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
            fade_in.setDuration(200);     // animation duration in milliseconds
            fade_in.setFillAfter(true);    // If fillAfter is true, the transformation that this animation performed will persist when it is finished.
            view.startAnimation(fade_in);
        }
    });
    return viewHolder;
}


@Override
public void onBindViewHolder(final CustomViewHolder customViewHolder, final int i) {
    //Setting text view title and drawable
    dataModel dataModel = dataModelList.get(i);
    customViewHolder.imageView.setImageDrawable(dataModel.icon);
    customViewHolder.textView.setText(dataModel.title);

    if(animatedIndex == i){
        ScaleAnimation fade_in = new ScaleAnimation(1f, 1.2f, 1f, 1.2f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        fade_in.setDuration(200);     // animation duration in milliseconds
        fade_in.setFillAfter(true);    // If fillAfter is true, the transformation that this animation performed will persist when it is finished.
        customViewHolder.itemView.startAnimation(fade_in);
    }

    customViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            animatedIndex = i;
            if (animatedView == null) {
                animatedView = customViewHolder.itemView;
            } else {
                animatedView.setAnimation(null);
                animatedView = customViewHolder.itemView;
            }
            //populateListview.PopulateListView(String.valueOf(i));
            ScaleAnimation fade_in = new ScaleAnimation(1f, 1.2f, 1f, 1.2f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
            fade_in.setDuration(200);     // animation duration in milliseconds
            fade_in.setFillAfter(true);    // If fillAfter is true, the transformation that this animation performed will persist when it is finished.
            customViewHolder.itemView.startAnimation(fade_in);
        }
    });
}

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

public class CustomViewHolder extends RecyclerView.ViewHolder {
    protected ImageView imageView;
    protected TextView textView;

    public CustomViewHolder(View view) {
        super(view);
        this.imageView = (ImageView) view.findViewById(R.id.thumbnail);
        this.textView = (TextView) view.findViewById(R.id.title);
    }
}

注意:我还使用this class来避免在RecyclerView上不滚动的情况下进行缩放。

简而言之

主要问题是,使用此代码可以缩放项目有时在滚动(向左或向右)时,该项目会失去缩放,我不知道为什么。视频中显示的错误是我猜想的关键错误...


问题到底是什么? - Blackbelt
我无法使所点击的项目保持“永久”缩放,当我滚动时它总会恢复到正常状态。 - Skizo-ozᴉʞS ツ
1
这是因为当项目在视图上不可见时,它将被刷新。您需要在数据模型上拥有一个isClicked字段来设置状态,一旦您点击该项目,就将该字段设置为true。然后在您的bindviewholder中检查数据模型是否已被点击,如果为真,则放大该项目,否则将其置于正常状态。 - ville101
尝试了为每个项目添加一个ID的方法,但没有结果,可能是我的代码写错了:S - Skizo-ozᴉʞS ツ
@ville101 有什么解决方法吗? - Skizo-ozᴉʞS ツ
显示剩余3条评论
4个回答

4
这是因为您的RecyclerView根据定义正在回收其视图。当视图超出RecyclerView的边界时,它会被丢弃,然后绑定到不同的数据模型上。当发生这种情况时,视图的变换数据(平移、缩放、旋转)将被重置。
为了解决这个问题,您需要向数据模型添加一些指示视图已缩放的指示(例如isSelected)。
在您的onBindViewHolder方法中,您需要检查数据模型是否选择了该视图,如果是,则将视图的比例设置为1.2,否则为1.0。请注意,在此处,您需要设置比例,而不是动画比例,因为我们假设用户触摸视图时动画已经发生。当我们在视图上绑定数据时,我们只是尝试将视图的状态重新创建为上次绑定时的状态。
在您的onCreateViewHolder方法中,您正在为充气视图设置onClickListener。在这个新的onClick方法内部,您应该根据先前的值将新的“isSelected”字段设置为true/false。
在您的onBindViewHolder方法中,您应该删除添加新的onClickListener的代码(因为这是多余的)。在这里,您应该检查dataModel.isSelected值,并相应地设置scaleX/scaleY。
请记住,RecyclerView内的视图应被视为原始模板,并由您在onBindViewHolder方法中绑定它们的数据驱动。您不能依赖它们已经存在的内容(例如它们的动画值等)。

哦,我明白了,但正如我所说,我尝试过“isSelected”的方法……但它不起作用。 - Skizo-ozᴉʞS ツ
@GilMoshayof:我正在面临一个相关的问题...你能否在这里帮助我解决我的问题?(https://dev59.com/2FsW5IYBdhLWcg3w8q1S) - Yash Sampat

2
正如其他答案中所建议的,这里的问题与“视图回收”有关。
我调试了上面的代码发现,当滚动时有时候不会调用onBindViewHolder(),导致视图失去动画效果。因此,在数据模型中使用布尔值并没有帮助。
正如其他答案中建议的那样,使用setIsRecyclable(false)将为您解决问题,所以您可以绝对使用它。
我还注意到,当您在屏幕上任何地方滚动时,视图都会滚动,要克服这一点,您可能需要覆盖LinearLayoutManager,但我认为这会影响每个项目视图的高度。需要深入挖掘一下。
作为替代方案,您可以使用TwoWayView来实现相同的功能,而不是RecyclerView
您可以在此处找到相应的库:TwoWayView 我已经使用TwoWayView测试了您的上述代码,并且其运行良好。我在此发布文件,您可以自行检查。如果您需要更多解释,请提问。 layout_twowayview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

<org.lucasr.twowayview.TwoWayView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/lvItems"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:drawSelectorOnTop="false"
    android:orientation="horizontal"
    tools:context=".MainActivity" />

 </LinearLayout>

TwoWayViewActivity

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import com.savitriya.samples.R;
import com.savitriya.samples.adapter.UsersAdapter;
import com.savitriya.samples.model.DataModel;
import org.lucasr.twowayview.TwoWayView;
import java.util.ArrayList;

/**
* Created by Satyen on 11/7/15.
*/
public class TwoWayViewActivity extends Activity {

ArrayList<DataModel> dataModels;
ScaleAnimation scaleAnimation = null, emptyAnimation = null;
TwoWayView twoWayView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_twowayview);

    twoWayView = (TwoWayView) findViewById(R.id.lvItems);
    scaleAnimation = new ScaleAnimation(1f, 1.3f, 1f, 1.3f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    scaleAnimation.setInterpolator(new AccelerateInterpolator());
    scaleAnimation.setStartTime(0);
    scaleAnimation.setDuration(0);     // animation duration in milliseconds
    scaleAnimation.setFillAfter(true);

    emptyAnimation = new ScaleAnimation(1f, 1f, 1f, 1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    emptyAnimation.setInterpolator(new AccelerateInterpolator());
    emptyAnimation.setStartTime(0);
    emptyAnimation.setDuration(0);     // animation duration in milliseconds
    emptyAnimation.setFillAfter(true);

    dataModels = new ArrayList<>();
    dataModels.add(new DataModel(0, R.mipmap.ic_launcher, "Data 1", 0, emptyAnimation, Color.parseColor("#ffffff")));
    dataModels.add(new DataModel(1, R.mipmap.ic_launcher, "Data 2", 0, emptyAnimation, Color.parseColor("#ffffff")));
    dataModels.add(new DataModel(2, R.mipmap.ic_launcher, "Data 3", 0, emptyAnimation, Color.parseColor("#ffffff")));
    dataModels.add(new DataModel(3, R.mipmap.ic_launcher, "Data 4", 0, emptyAnimation, Color.parseColor("#ffffff")));
    dataModels.add(new DataModel(4, R.mipmap.ic_launcher, "Data 5", 0, emptyAnimation, Color.parseColor("#ffffff")));
    dataModels.add(new DataModel(5, R.mipmap.ic_launcher, "Data 6", 0, emptyAnimation, Color.parseColor("#ffffff")));
    dataModels.add(new DataModel(6, R.mipmap.ic_launcher, "Data 7", 0, emptyAnimation, Color.parseColor("#ffffff")));
    dataModels.add(new DataModel(7, R.mipmap.ic_launcher, "Data 8", 0, emptyAnimation, Color.parseColor("#ffffff")));
    dataModels.add(new DataModel(8, R.mipmap.ic_launcher, "Data 9", 0, emptyAnimation, Color.parseColor("#ffffff")));
    dataModels.add(new DataModel(9, R.mipmap.ic_launcher, "Data 10", 0, emptyAnimation, Color.parseColor("#ffffff")));

    UsersAdapter usersAdapter = new UsersAdapter(TwoWayViewActivity.this, dataModels);
    twoWayView.setAdapter(usersAdapter);
}
}

适配器代码

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.savitriya.samples.R;
import com.savitriya.samples.model.DataModel;
import java.util.ArrayList;

/**
* Created by Satyen on 11/7/15.
*/
public class UsersAdapter extends ArrayAdapter<DataModel> {

ArrayList<DataModel> users;
ScaleAnimation scaleAnimation = null, emptyAnimation = null;

View view;
int animatedIndex = -1;

// View lookup cache
private static class ViewHolder {
    TextView name;
    ImageView icon;
}

public UsersAdapter(Context context, ArrayList<DataModel> users) {
    super(context, R.layout.list_row, users);
    this.users = users;

    scaleAnimation = new ScaleAnimation(1f, 1.3f, 1f, 1.3f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    scaleAnimation.setInterpolator(new AccelerateInterpolator());
    scaleAnimation.setStartTime(0);
    scaleAnimation.setDuration(0);     // animation duration in milliseconds
    scaleAnimation.setFillAfter(true);

    emptyAnimation = new ScaleAnimation(1f, 1f, 1f, 1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    emptyAnimation.setInterpolator(new AccelerateInterpolator());
    emptyAnimation.setStartTime(0);
    emptyAnimation.setDuration(0);     // animation duration in milliseconds
    emptyAnimation.setFillAfter(true);

}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // Get the data item for this position
    final DataModel user = getItem(position);
    // Check if an existing view is being reused, otherwise inflate the view
    ViewHolder viewHolder; // view lookup cache stored in tag
    view = convertView;
    if (view == null) {
        viewHolder = new ViewHolder();
        LayoutInflater inflater = LayoutInflater.from(getContext());
        view = inflater.inflate(R.layout.list_row, parent, false);
        viewHolder.name = (TextView) view.findViewById(R.id.title);
        viewHolder.icon = (ImageView) view.findViewById(R.id.thumbnail);
        view.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) view.getTag();
    }
    // Populate the data into the template view using the data object
    viewHolder.name.setText(user.getTitle());
    viewHolder.icon.setImageResource(R.mipmap.ic_launcher);

    view.startAnimation(user.getAnimation());
    // Return the completed view to render on screen

    view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //view.startAnimation(scaleAnimation);
            if (animatedIndex != -1) {
                DataModel dataModel = getItem(animatedIndex);
                dataModel.setAnimation(emptyAnimation);
            }
            animatedIndex = position;
            user.setAnimation(scaleAnimation);
            notifyDataSetChanged();
        }
    });
    return view;
}

@Override
public int getCount() {
    return users.size();
}
}

适配器行
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="120dp"
android:layout_height="120dp"
android:animateLayoutChanges="true"
android:animationCache="true"
android:gravity="center"
android:orientation="vertical">

<RelativeLayout
    android:id="@+id/targetView"
    android:layout_width="80dp"
    android:layout_height="100dp"
    android:padding="5dp">

    <ImageView
        android:id="@+id/thumbnail"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_centerInParent="true"
        android:background="@drawable/rectangle"
        android:scaleType="centerCrop"
        android:src="@drawable/locate" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/thumbnail"
        android:layout_centerHorizontal="true"
        android:text="dafdafda"
        android:textColor="#222"
        android:textSize="12sp" />

</RelativeLayout>

</LinearLayout>

@SatyenUdeshi:我遇到了一个相关的问题...你能否帮助我回答一下我的问题在这里 - Yash Sampat

1

我认为我解决了它,请尝试将此作为适配器。

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

/**
 * Created by Ma7mo0oed on 11/8/2015.
 */
public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.CustomViewHolder> {

    private Context mContext;
    View animatedView = null;
    private List<dataModel> dataModelList;
    int animatedIndex = -1; // Initially no view is clicked so -1

    public MyRecyclerViewAdapter(Context context, List<dataModel> items) {
        this.dataModelList = items;
        this.mContext = context;
    }

    @Override
    public CustomViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        //View per each row
        final View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_row, null);
        CustomViewHolder viewHolder = new CustomViewHolder(view);
        return viewHolder;
    }


    @Override
    public void onBindViewHolder(CustomViewHolder customViewHolder, final int i) {
        dataModel dataModel = dataModelList.get(i);
        customViewHolder.imageView.setImageDrawable(dataModel.icon);
        customViewHolder.textView.setText(dataModel.title);
        customViewHolder.setIsRecyclable(false);
        if (animatedIndex == i) {
            animat(customViewHolder.itemView);
        }
        customViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                animatedIndex = i;
                animat(v);
            }
        });
    }

    public void animat(View v) {
        if (animatedView == null) {
            animatedView = v;
        } else {
            animatedView.setAnimation(null);
            animatedView = v;
        }
        ScaleAnimation fade_in = new ScaleAnimation(1f, 1.2f, 1f, 1.2f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        fade_in.setDuration(200);     // animation duration in milliseconds
        fade_in.setFillAfter(true);    // If fillAfter is true, the transformation that this animation performed will persist when it is finished.
        v.startAnimation(fade_in);
    }

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

    public class CustomViewHolder extends RecyclerView.ViewHolder {
        protected ImageView imageView;
        protected TextView textView;


        public CustomViewHolder(View view) {
            super(view);
            this.imageView = (ImageView) view.findViewById(R.id.thumbnail);
            this.textView = (TextView) view.findViewById(R.id.title);

        }
    }
}   

假设在这个HorizontalScrollView下面,我会添加一个ListView,如果用户开始水平滚动,它将每次加载ScrollView? - Skizo-ozᴉʞS ツ
1
我不确定你在问什么,但是由于这两个视图是分开的,其中一个的更改不会影响另一个,除非你想要。顺便问一下,适配器工作了吗? - Mahmoud.M
1
customViewHolder.setIsRecyclable(false);这句话是什么意思? - Skizo-ozᴉʞS ツ
你很聪明,注意到了问题。我在想这段代码哪里出错了,于是开始调试,发现在onBindViewHolder中视图的值正在改变,我记得recyclerView 的工作方式是始终回收视图而不像ListView一样创建新的视图,所以我认为一定有一个方法可以阻止回收并使其成为静态的,我找到了这个方法,它起作用了。 - Mahmoud.M

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接