RecyclerView适配器在错误的位置重复值

时间:2015-12-28 08:43:35

标签: android scroll android-recyclerview android-cardview

我正在显示17张CardView。

我正在使用RecyclerView来实现同样的目标。

每个CardView都显示一个通用数据(格式)。

根据从JSON文件收到的数据,卡可能会显示一些额外的行。 enter image description here。下图显示了2个显示附加数据的CardView(焦点对准),其余显示公共数据。

看起来很正确。但是当我向下滚动到View的底部时,最后一个CardView不会显示任何行,但它会从我们在上面链接中看到的2个中复制一个并显示。 [一次只能发布2个链接]

然后我滚动到顶部,它会复制正确显示的两个CardView中的行,并分别在这里重复前2个卡片: enter image description here

我尝试了什么?

  • 如果,否则

许多类似问题都显示了一个共同的解决方案其中一个答案就在这里。事情是我不知道适用的条件和地点。这是我的代码:

@Override
    public void onBindViewHolder(DataObjectHolder holder, final int position) {


        HashMap<Integer, ShoppingCartSummaryObject> dataItem = new HashMap<Integer, ShoppingCartSummaryObject>(); 
        dataItem = mDataset.get(position);

        TextView textViewComment = holder.textViewComment;

        ShoppingCartSummaryObject obj = new ShoppingCartSummaryObject();
        obj = (ShoppingCartSummaryObject) dataItem.get(position);

        holder.textViewPackageType.setText(obj.getPackageType());
        holder.textViewPackageCode.setText(obj.getPackageCode());
        holder.textViewPackageDesc.setText(obj.getPackageDesc());

        params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        try {
            for (int i = 0; i < obj.getArrayListPart().size(); i++) {

                View view2 = LayoutInflater.from(mContext).inflate(R.layout.detailrow, null);
                holder.advPartDesc = (TextView) view2.findViewById(R.id.advPartDesc);
                holder.advPartnum = (TextView) view2.findViewById(R.id.advPartNum);

                cv = holder.cv;
                advisoryLayout = holder.advisoryLayout;
                empty = holder.empty;
                layoutPackageSummary = holder.layoutPackageSummary;

                holder.detailRow = (RelativeLayout) view2.findViewById(R.id.detailRow);

                rallyLayout = new RelativeLayout(mContext);

                rallyLayout.addView(holder.detailRow);

                cnt+=1;
                rallyLayout.setId(cnt);

                holder.layoutpartList.addView(rallyLayout);

                if (i == 0) { //for adding the first row below standard data

                    params.addRule(RelativeLayout.BELOW, empty.getId() );



       holder.advPartnum.setText(obj.getArrayListPart().get(i).partCode);
                holder.advPartDesc.setText(obj.getArrayListPart().get(i).partDesc);

                rallyLayout.setLayoutParams(params);

            } else { //for more than one rows, 2nd row appearing below 1st and 3rd below 2nd and so on

                params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                params.addRule(RelativeLayout.BELOW, cnt - 1);

                holder.advPartnum.setText(obj.getArrayListPart().get(i).partCode);
                holder.advPartDesc.setText(obj.getArrayListPart().get(i).partDesc);

                rallyLayout.setLayoutParams(params);
            }

        }
    } catch (Exception e) {
        e.printStackTrace();
    }

}

这就是我尝试从共享链接实现答案的方式:

if(obj.getArrayListPart().size()>0) { /*added the if() condition*/
                    if (i == 0) {

                        params.addRule(RelativeLayout.BELOW, empty.getId());

                        holder.advPartnum.setText(obj.getArrayListPart().get(i).partCode);


          holder.advPartDesc.setText(obj.getArrayListPart().get(i).partDesc);

                    rallyLayout.setLayoutParams(params);

                } else {

                    params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    params.addRule(RelativeLayout.BELOW, cnt - 1);

                    holder.advPartnum.setText(obj.getArrayListPart().get(i).partCode);
                    holder.advPartDesc.setText(obj.getArrayListPart().get(i).partDesc);

                    rallyLayout.setLayoutParams(params);
                }
            }
            else
            {
                holder.advPartnum.setText("abc");
                holder.advPartDesc.setText("abc");
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

我还补充说:

@Override
    public long getItemId(int position) {
        return super.getItemId(position);
    }

没有任何效果。

我检查了JSON数据,从列表中删除了一个数组,无论我删除了哪个卡,它还会影响从顶部/底部行(cardviews)中删除重复行值,具体取决于卡片之间的卡片位置顶部/底部的额外细节和卡片。还尝试过:

mAdapter.setHasStableIds(true);

但是它只是跳过了公共标准下面出现的所有其他行数据。

请帮助。我提供了我尝试的任何东西。

1 个答案:

答案 0 :(得分:0)

以下是您要求xml

的源代码
 <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:stackFromEnd="true" />

活动

 import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import in.ashish29agre.calendar.R;

public class RecyclerViewActivity extends AppCompatActivity {

    private List<Message> messages;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_view);
        MessagesAdapter adapter = initDefaultsAndGetADapter();
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);
    }

    private MessagesAdapter initDefaultsAndGetADapter() {

        GradientDrawable fromDrawable = new GradientDrawable();
        fromDrawable.setColor(Color.parseColor("#EC407A"));
        fromDrawable.setCornerRadius(16f);

        GradientDrawable toDrawable =  new GradientDrawable();
        toDrawable.setCornerRadius(16f);
        toDrawable.setColor(Color.parseColor("#64B5F6"));
//        toDrawable.getPaint().setStyle(Paint.Style.FILL_AND_STROKE);
//        toDrawable.getPaint().setStrokeWidth(1);


        messages = new ArrayList<>();
        Random random = new Random();
        Message message;
        for (int i = 0; i < 20; i++) {
            message = new Message();
            int value = random.nextInt(2);
            if (value == Message.TYPE_FROM) {
                message.setMsgType(value);
                message.setContent("Other user says #" + i);
                message.setDrawable(fromDrawable);
            } else if (value == Message.TYPE_TO) {
                message.setMsgType(value);
                message.setContent("You are saying #" + i);
                message.setDrawable(toDrawable);
            } else {
                Log.e("TAG", "Oops");
            }
            messages.add(message);
        }
        return new MessagesAdapter(messages);
    }


    private class MessagesAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

        private List<Message> messages;

        public MessagesAdapter(List<Message> messages) {
            this.messages = messages;
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

            if (viewType == Message.TYPE_FROM) {
                View fromLayout = LayoutInflater.from(parent.getContext()).inflate(R.layout.from_layout_item, parent, false);
                return new OtherUserMessageHolder(fromLayout);
            } else if (viewType == Message.TYPE_TO) {
                View toLayout = LayoutInflater.from(parent.getContext()).inflate(R.layout.to_layout_item, parent, false);
                return new CurrentUserMessageHolder(toLayout);
            } else {
                throw new IllegalArgumentException("Invalid View type");
            }
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

            if (holder instanceof OtherUserMessageHolder) {
                configureOtherUserMessageHolder(holder, position);
            } else {
                configureCurrentUserMessageHolder(holder, position);
            }
        }


        private void configureCurrentUserMessageHolder(RecyclerView.ViewHolder holder, int position) {

            if (holder != null && holder instanceof CurrentUserMessageHolder) {
                CurrentUserMessageHolder currentUserMessageHolder = (CurrentUserMessageHolder) holder;
                currentUserMessageHolder.getToContentTv().setText(messages.get(position).getContent());
                currentUserMessageHolder.getToContentTv().setBackground(messages.get(position).getDrawable());
            }
        }

        private void configureOtherUserMessageHolder(RecyclerView.ViewHolder holder, int position) {
            if (holder != null && holder instanceof OtherUserMessageHolder) {
                OtherUserMessageHolder otherUserMessageHolder = (OtherUserMessageHolder) holder;
                otherUserMessageHolder.getFromContentTv().setText(messages.get(position).getContent());
                otherUserMessageHolder.getFromContentTv().setBackground(messages.get(position).getDrawable());
            }
        }

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

        @Override
        public int getItemViewType(int position) {
            return messages.get(position).getMsgType();
        }

        private final class CurrentUserMessageHolder extends RecyclerView.ViewHolder {

            private TextView toContentTv;

            public CurrentUserMessageHolder(View itemView) {
                super(itemView);
                toContentTv = (TextView) itemView.findViewById(R.id.to_item_tv);
            }

            public TextView getToContentTv() {
                return toContentTv;
            }

        }

        private final class OtherUserMessageHolder extends RecyclerView.ViewHolder {

            private TextView fromContentTv;

            public OtherUserMessageHolder(View itemView) {
                super(itemView);
                fromContentTv = (TextView) itemView.findViewById(R.id.from_item_tv);
            }

            public TextView getFromContentTv() {
                return fromContentTv;
            }

        }
    }

    private class Message {
        private static final int TYPE_FROM = 0;
        private static final int TYPE_TO = 1;

        private int msgType;
        private CharSequence content;
        private Drawable drawable;

        public int getMsgType() {
            return msgType;
        }

        public void setMsgType(int msgType) {
            this.msgType = msgType;
        }

        public CharSequence getContent() {
            return content;
        }

        public void setContent(CharSequence content) {
            this.content = content;
        }

        public Drawable getDrawable() {
            return drawable;
        }

        public void setDrawable(Drawable drawable) {
            this.drawable = drawable;
        }
    }
}