gpt4 book ai didi

android - 滚动时 RecyclerView 项目位置更改或删除

转载 作者:行者123 更新时间:2023-11-30 01:55:31 25 4
gpt4 key购买 nike

我有一个问题,我已经尝试解决太久了,但我不知道是什么原因导致了这个问题。

我有一个标记列表,其中包含我用作标记之间部分的测试对象和主题字符串,我使用 getClass() 方法来确定对象是字符串还是测试,如果是字符串,我隐藏测试的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 方法,但它对我不起作用

提前致谢!

最佳答案

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

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

关于android - 滚动时 RecyclerView 项目位置更改或删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32312739/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com