RecyclerView适配器是在错误的地方重复值 [英] RecyclerView Adapter is repeating values at wrong places

查看:367
本文介绍了RecyclerView适配器是在错误的地方重复值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我展示17 CardViews。

I am displaying 17 CardViews.

我使用RecyclerView来实现相同的。

I am using RecyclerView to achieve the same.

每个CardView显示了一个共同的数据(格式)。

Each CardView shows a common data (format).

根据从JSON文件接收的数据,一卡可能会显示一些额外的行。 。下图显示了2 CardViews(焦点)显示更多的数据,显示公共数据的其余部分。

Depending on data received from a JSON file, a card may display some additional rows. . The following image shows 2 CardViews (in focus) displaying the additional data, the rest displaying the common data.

这正确显示。但后来当我向下滚动视图的底部,最后CardView并不想显示任何行,但它确实复制一个与我们在上面的链接并显示只见2之一。 [可以发布仅2个链接时间]

It appears correctly. But then when I scroll down to bottom of the View, the last CardView is not suppose to display any row, but it does copy one from one of the 2 we saw in the above link and displays. [Can post only 2 links a time]

然后我滚动到顶部,并将其复制从被正确显示和重复分别这里顶2张牌都CardViews行:

Then I scroll to the top and it copies the rows from both CardViews that were displaying correctly and repeats here for top 2 cards respectively:

我试过?


  • 如果,否则

许多类似的问题表现出共同的解决方案。其中一个答案就在这里。事情是我不知道申请条件是什么在哪里。这是我的code:

Many similar issues show a common solution. One of those answers is here. Thing is I don't know what condition to apply and where. This is my code:

@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();
    }

}

这就是我如何努力实现从共享链接的答案:

and this is how I tried implementing the answers from the shared link:

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)的重复行值,根据不同的卡牌之间躺在上额外的细节和卡上的顶部/底部。也试过:

I checked the JSON data, removed one array from the list, and irrespective of the card I remove, it also affects removal of the repeating row values from top/bottom rows (cardviews), depending on the card lying between card with extra details and card on top/bottom. Also tried:

mAdapter.setHasStableIds(true);

但只是跳过所有出现下面的通用标准的附加行数据。

but that simply skipped all the additional row data that appears below the common standard.

请帮助。我提供什么我试过了。

Kindly help. I have provided whatever I tried.

推荐答案

下面是源$ C ​​$ C,你问XML

Here is the source code which you asked for 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;
        }
    }
}

这篇关于RecyclerView适配器是在错误的地方重复值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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