在RecyclerView.Adapter中未删除RecyclerView

时间:2016-11-08 13:59:51

标签: android

我有一个Recyclerview适配器,它基于两种不同的卡布局。该适配器使用新卡动态更新,因此适配器中的每个卡也需要更新。对于每张卡片,都有一个显示随机生成数据的进度条。

然而,我面临的问题是,当我删除列表中的一张卡片时,删除的卡片的参考不会被删除(onCreateViewHolder未被调用,因此不会更新视图),而是留下的卡片是只需写下"删除"的布局卡。卡上的文字是正确的,它从列表中删除了正确的项目,但进度条仍然具有旧卡的旧参考,这使得它添加了与删除的卡相对应的值。

为了说明问题,我根据移动名称将随机数据添加到进度条中的每张卡片上。因此无论卡片如何,名为" Open Hand"应始终在0-20之间添加随机数据,"关闭手" 20-60"和"代词" 60-100"

添加三张卡后,这里的一切正常运作: After inserted three cards

但是当我删除两张第一张牌时#34;打开手牌" "关闭手","旋转"卡片左侧显示正在进行中的数据栏实际上对应于"打开手牌"数据范围。那么为什么引用不正确呢? 我试过调用onDataSetChange();和notifyItemRemoved(pos); After deleted two cards

我的适配器代码:

public class ParameterAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    List<Movement> movements;

    public ParameterAdapter() {
        this.movements=movementList;
    }

    class AddCard extends RecyclerView.ViewHolder {
        public AddCard(View inflate) {
            super(inflate);
        }
    }

    class Card extends RecyclerView.ViewHolder {
        TextView movement;
        TextView channel;
        TextView remove;
        ProgressBar strength;
        TextView graph;
        LinearLayout expandedView;
        LineChart lineChart;
        Movement mov;
        public Card(View itemView, Movement m) {
            super(itemView);
            movement = (TextView) itemView.findViewById(R.id.movementText);
            channel = (TextView) itemView.findViewById(R.id.channelNameText);
            remove = (TextView) itemView.findViewById(R.id.removeCard);
            strength = (ProgressBar) itemView.findViewById(R.id.progressBarStrength);
            graph = (TextView) itemView.findViewById(R.id.graphTextButton);
            expandedView = (LinearLayout) itemView.findViewById(R.id.detailsNr2);
            lineChart = (LineChart) itemView.findViewById(R.id.linechartCard);
            mov=m;
            channel.setText(m.getChannel());
            //Setup real time graph
            setupLineChart(mov, lineChart, mov.getId());
            // Start adding random data to the graph
            startAddingRandomData();
        }

        public void startAddingRandomData() {
            new Thread(
                    new Runnable() {
                        public void run() {
                            while (true) {
                                int generatedData;

                                // Depending on what type of movement, add slightly different generated random data
                                // (to be sure that the views are correctly updated when removed for instance)
                                if(mov.getSettingsType().equals(SettingsDbHelper.MOVEMENTS_STRING[0])){
                                    // Add random generated data between the span 0-20
                                    generatedData=generateRandomData(0,20);
                                } else if (mov.getSettingsType().equals(SettingsDbHelper.MOVEMENTS_STRING[1])) {
                                    // Add random generated data between the span 20-60
                                    generatedData=generateRandomData(20, 60);
                                } else {
                                    // Add random generated data between the span 60-100
                                    generatedData=generateRandomData(60, 100);
                                }
                                //Set the progress bar
                                strength.setProgress(generatedData);

                                // Add the random generated data to the graph
                                addEntry(mov, lineChart, generatedData);

                                try {
                                    Thread.sleep(300);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    }).start();

        }
    }

    public int generateRandomData(int n1, int n2){
        Random r = new Random();
        return r.nextInt(n2 - n1) + n1;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View vSetting=null;
        View vCard=null;
        // If its the first position, then inflate the "add movement" card.
        if(viewType==0) {
           vSetting = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_parameter_cardview, parent, false);
           return new AddCard(vSetting);
       }else if(viewType>0) {
            // Otherwise, if it's not the first position, then inflate a "movement card" and create the corresponding movement item
            vCard = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_movement, parent, false);
            Movement m = movementList.get(viewType - 1);
            return new Card(vCard, m);
       }else {
         // For some reason, this occurs
         return null;
       }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder,final int position) {
        // "Add movement" card
        if(position==0){
            ((AddCard)holder).itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(getActivity(), AddMovement.class);
                    startActivityForResult(intent, ADD_MOVEMENT);
                }
            });

        // "Movement" item card
        } else if(position>0) {
            final boolean isExpanded = position == mExpandedPosition;
            ((Card) holder).expandedView.setVisibility(isExpanded ? View.VISIBLE : View.GONE);
            ((Card) holder).graph.setActivated(isExpanded);
            ((Card) holder).graph.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mExpandedPosition = isExpanded ? -1 : position;
                    notifyDataSetChanged();
                }
            });

            // Get the type of movement
            Movement m = movementList.get(position - 1);

            // This lets the real-time graph view be created before actual adding any data.
            m.setChartCreated(1);

            //Update the textviews inside the card
            ((Card)holder).movement.setText(m.getMovement());
            ((Card)holder).channel.setText(m.getChannel());
            ((Card)holder).remove.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Movement m = movementList.get(position - 1);
                    //Remove the item from the list
                    remove(m);
                    // Get ID and settings type in order to update the entry
                    String id=m.getId();
                    String settingsType = m.getSettingsType();
                    //Update the entry
                    settingsdb.updateSetting(settingsType,id,"false");
                }
            });
        }
    }

    // Remove item from the list
    public void remove(Movement data) {
        int position = movementList.indexOf(data);
        movementList.remove(data);
        notifyDataSetChanged();
        // (+ 1 one to compensate for the first card)
        notifyItemRemoved(position + 1);

    }


    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }


    @Override
    public int getItemViewType(int position) {
        //Return the position
     return position;
    }


    @Override
    public int getItemCount() {
        // (+1 to compensate for the first card)
        return movementList.size()+1;
    }

}

0 个答案:

没有答案