滚动时,RecyclerView项目位置会更改或删除

时间:2015-08-31 13:42:48

标签: android android-recyclerview recycler-adapter

我有一个问题,我一直试图解决太长时间,但我不知道导致问题的原因。

我有一个标记列表,其中包含我用作标记之间的部分的Test对象和主题字符串,我使用getClass()方法来确定对象是字符串还是Test,如果是字符串,我隐藏测试& #39;的RelativeLayout,如果是测试,那么我隐藏了主题的RelativeLayout。但滚动时,项目会被移除并随机重新添加。

我的适配器的课程:

    public class MarksAdapter extends RecyclerView.Adapter<MarksAdapter.MyHolder>{

public class MyHolder extends RecyclerView.ViewHolder{
    RelativeLayout card;
    TextView title;
    TextView total;
    View divider;
    int posito;

    RelativeLayout group;
    TextView groupTitle;
    TextView groupMark;

    public MyHolder(View itemView) {
        super(itemView);
        this.card = (RelativeLayout)itemView.findViewById(R.id.card);
        this.title = (TextView)itemView.findViewById(R.id.title);
        this.total = (TextView)itemView.findViewById(R.id.mark);
        this.divider = itemView.findViewById(R.id.item_divider);

        this.group = (RelativeLayout)itemView.findViewById(R.id.group);
        this.groupTitle = (TextView)itemView.findViewById(R.id.group_title);
        this.groupMark = (TextView)itemView.findViewById(R.id.group_mark);

    }
}


final ArrayList marks;
Context ctx;
String selectedSem;
TestDatabase db;

public MarksAdapter(Context c, ArrayList marks,String sem,TestDatabase db,ArrayList<Integer> sections){
    this.marks = marks;
    ctx = c;
    selectedSem = sem;
    this.db = db;
}

@Override
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        return new MyHolder(LayoutInflater.from(ctx).inflate(R.layout.mark_child, parent, false));
}

int lastPosition = -1;
int offset = 0;

@Override
public void onBindViewHolder(final MyHolder holder, final int positioner) {
    holder.posito = positioner;
        if(marks.get(holder.posito).getClass() == Test.class) {
            holder.group.setVisibility(View.GONE);
            //mark
            final Test test = (Test)marks.get(positioner);
            holder.title.setText(test.getName());
            holder.total.setText(String.format(ctx.getString(R.string.new_m_sum), test.getMarkGot(), test.getMarkOver()));

            //add here
            holder.card.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                     Intent i = new Intent(ctx,addTest.class);
                     Test mark = (Test)marks.get(holder.posito);
                     i.putExtra("t_name",mark.getName());
                     i.putExtra("subject",mark.getSubject());
                     i.putExtra("mark_got",mark.getMarkGot());
                     i.putExtra("mark_over",mark.getMarkOver());
                     i.putExtra("mode","edit");
                     i.putExtra("oldId", mark.getId());
                     ctx.startActivity(i);
                }
            });

            holder.card.setLongClickable(true);
            holder.card.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(final View v) {
                    final Dialog dialo = new Dialog(ctx);
                    dialo.requestWindowFeature(Window.FEATURE_NO_TITLE);
                    dialo.setContentView(R.layout.material_dialog);
                    TextView title = (TextView) dialo.findViewById(R.id.title);

                    TextView body = (TextView) dialo.findViewById(R.id.body);

                    Button negative = (Button) dialo.findViewById(R.id.negative);
                    negative.setText(ctx.getString(R.string.cancel));
                    Button positive = (Button) dialo.findViewById(R.id.positive);
                    positive.setText(ctx.getString(R.string.Delete));

                    title.setText(ctx.getString(R.string.d));
                    body.setText(ctx.getString(R.string.q_delete) + " " + test.getName() + " " + ctx.getString(R.string.de_comp));

                    negative.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            dialo.dismiss();
                        }
                    });

                    positive.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View vir) {

                            TestDatabase tb = new TestDatabase(ctx);
                            SemesterDatabase semesterDatabase = new SemesterDatabase(ctx);
                            tb.deleteTest(holder.posito, semesterDatabase.getSelected().getName());
                            MarksAdapter.this.notifyItemRemoved(holder.posito);

                            marks.remove(holder.posito);

                            for (int i = 0; i < marks.size(); i++) {
                                if(!(marks.get(i) instanceof Test)) {
                                    String sub = String.valueOf(marks.get(i));
                                    if(db.isNoValue(sub,selectedSem)){
                                        marks.remove(i);
                                        notifyItemRemoved(i);
                                    }
                                }

                            }


                            dialo.dismiss();
                        }
                    });
                    dialo.show();

                    return true;
                }
            });
        }else {
            holder.group.setVisibility(View.VISIBLE);
            holder.card.setVisibility(View.GONE);
        //group
            if(holder.posito == 0) {
                LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)holder.group.getLayoutParams();
                params.setMargins(0,0,0,0);
                holder.group.setLayoutParams(params);
            }
            String sub = String.valueOf(marks.get(holder.posito));
            holder.groupTitle.setText(sub);
            holder.groupMark.setText(db.getTotalSubjectMarks(sub,selectedSem));
        }


    if(lastPosition < holder.posito) {
        if(holder.card.getVisibility() == View.VISIBLE) {
            Animation slide = AnimationUtils.loadAnimation(ctx, R.anim.marks);
            slide.setInterpolator(new AccelerateInterpolator());
            slide.setDuration(700);
            offset += 100;
            slide.setStartOffset(offset);
            holder.card.startAnimation(slide);
        } else {
            Animation slide = AnimationUtils.loadAnimation(ctx, R.anim.mark_groups);
            slide.setInterpolator(new AccelerateInterpolator());
            slide.setDuration(700);
            offset += 50;
            slide.setStartOffset(offset);
            holder.group.startAnimation(slide);
            }
        lastPosition = holder.posito;
    }
    }


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

注意:我已经尝试过itemViewType方法,但它对我不起作用

提前致谢!

1 个答案:

答案 0 :(得分:1)

我相信你错过了将holder.card设置为可见,就像这样

if(marks.get(holder.posito).getClass() == Test.class) {
        holder.card.setVisibility(View.Visible); //add back this
        holder.group.setVisibility(View.GONE);