我是初学者,需要你的帮助才能解决这个问题。我正面临着RecyclerView
中视图的复制。每5个视图后,视图会自动重复。我在这里经历了几个线程并且reddit androiddev也没有人可以提供帮助。
基本上我正在创建一个反馈表来获取学生的意见,这就是我的方式,我无法解决它。
我附上截图,这样你就可以得到问题的要点。
请在下方和下方找到截图比较的代码。
public class GenericViewholder extends CarViewholder {
TextView ques;
TextView tv1, tv2, tv3, tv4, tv5;
LinearLayout counter;
CardView cardview1;
// Firebase mref;
// Button bottomb;
int count = 0;
String database = new String();
public GenericViewholder(View itemView) {
super(itemView);
// cardview1 = (CardView) itemView.findViewById(R.id.card_view1) ;
counter = (LinearLayout) itemView.findViewById(R.id.tv_counter);
ques = (TextView) itemView.findViewById(R.id.setQuestion);
tv1 = (TextView) itemView.findViewById(R.id.one);
tv2 = (TextView) itemView.findViewById(R.id.two);
tv3 = (TextView) itemView.findViewById(R.id.three);
tv4 = (TextView) itemView.findViewById(R.id.four);
tv5 = (TextView) itemView.findViewById(R.id.five);
// bottomb = (Button) itemView.findViewById(R.id.button);
View.OnClickListener clicklistener = new View.OnClickListener() {
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.one:
if (tv4.getBackground() != null || tv2.getBackground() != null || tv3.getBackground() != null
|| tv5.getBackground() != null) {
tv4.setBackgroundResource(R.color.standardwhite);
tv2.setBackgroundResource(R.color.standardwhite);
tv3.setBackgroundResource(R.color.standardwhite);
tv5.setBackgroundResource(R.color.standardwhite);
}
tv1.setBackgroundResource(R.drawable.fill);
count = 1;
database = "1";
break;
case R.id.two:
if (tv4.getBackground() != null || tv1.getBackground() != null || tv3.getBackground() != null
|| tv5.getBackground() != null) {
tv4.setBackgroundResource(R.color.standardwhite);
tv1.setBackgroundResource(R.color.standardwhite);
tv3.setBackgroundResource(R.color.standardwhite);
tv5.setBackgroundResource(R.color.standardwhite);
}
tv2.setBackgroundResource(R.drawable.fill);
count = 1;
database ="2";
break;
case R.id.three:
if (tv1.getBackground() != null || tv2.getBackground() != null || tv4.getBackground() != null
|| tv5.getBackground() != null) {
tv1.setBackgroundResource(R.color.standardwhite);
tv2.setBackgroundResource(R.color.standardwhite);
tv4.setBackgroundResource(R.color.standardwhite);
tv5.setBackgroundResource(R.color.standardwhite);
}
tv3.setBackgroundResource(R.drawable.fill);
count= 1;
database = "3";
break;
case R.id.four:
if (tv1.getBackground() != null || tv2.getBackground() != null || tv3.getBackground() != null
|| tv5.getBackground() != null) {
tv1.setBackgroundResource(R.color.standardwhite);
tv2.setBackgroundResource(R.color.standardwhite);
tv3.setBackgroundResource(R.color.standardwhite);
tv5.setBackgroundResource(R.color.standardwhite);
}
tv4.setBackgroundResource(R.drawable.fill);
count =1;
database = "4";
break;
case R.id.five:
if (tv4.getBackground() != null || tv2.getBackground() != null || tv3.getBackground() != null
|| tv1.getBackground() != null) {
tv1.setBackgroundResource(R.color.standardwhite);
tv2.setBackgroundResource(R.color.standardwhite);
tv3.setBackgroundResource(R.color.standardwhite);
tv4.setBackgroundResource(R.color.standardwhite);
}
tv5.setBackgroundResource(R.drawable.fill);
count = 1;
database ="5" ;
// b5.setBackgroundColor(Color.BLUE);
break;
default:
count = 0;
database ="0" ;
}
//Toast.makeText(view.getContext(), "Rating set : " + value.getRating(), Toast.LENGTH_SHORT).show();
}
};
if(tv1.getBackground() != null ||tv2.getBackground() != null ||tv3.getBackground() != null ||tv4.getBackground() != null ||tv5.getBackground() != null )
{
count = 1;
}
else
count = 0;
tv1.setOnClickListener(clicklistener);
tv2.setOnClickListener(clicklistener);
tv3.setOnClickListener(clicklistener);
tv4.setOnClickListener(clicklistener);
tv5.setOnClickListener(clicklistener);
}
}
public class CardAdapter extends RecyclerView.Adapter<CarViewholder> {
/*
Important thing to notice here is that the multiple viewholders are needed to be checked and aligned
with respect to the position and viewtype of the layout
Viewtype: returns default value 0 for similar layout so we overrided it with two values
one for the cardview and one for the Master submit section
Pl note :::: Always create different child classes and extend it to the master class to get better result and
better judge of the execution taking place.
OncreateViewholder creates and inflates the view so here we need to distinctly create different objects of
child classes to get different results
*/
List<String> dbObjList = new ArrayList<>();
public static int CARD_VAL = 1;
public static int BUTTON_VAL = 2;
int countercount = 0;
String[] ques = {"How would you grade the Teachers",
"How Did You Find The Study Material",
"How Was The Coaching Infrastructure",
"How Innovative Were The Teachers",
"Ease Of Travel To The Class",
"Previous Years' Results",
"Extra-Curricular Activities",
"Counselling During Admission",
"Clear Your Doubts Sessions",
"How Were The Tests Conducted",
"How Were The Seminars",
"How Were The Powerpoint Presentations",
"Quality of tests in regular time intervals",
"Syllabus coverage in tests",
"Test results/feedback on time",
"Doubt clearance after tests",
"Remedial classes after tests"};
Context context;
LayoutInflater inflater;
// View iview;
public CardAdapter(Context context) {
this.context = context;
inflater = LayoutInflater.from(context);
}
@Override
public CarViewholder onCreateViewHolder(ViewGroup parent, int viewType) {
CarViewholder viewholder ;
View iview;
if (viewType == CARD_VAL) {
iview = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview, parent, false);
viewholder = new GenericViewholder(iview);
iview.setTag(viewholder);
return viewholder;
} else {
iview = LayoutInflater.from(parent.getContext()).inflate(R.layout.button, parent, false);
viewholder = new ButtonViewHolder(iview);
return viewholder;
}
}
@Override
public void onBindViewHolder(CarViewholder holder, int position) {
if (getItemViewType(position) == BUTTON_VAL || position == getItemCount() + 1) {
Log.i("DEBUG", "hun" + getItemViewType(position));
ButtonViewHolder buttonViewHolder = (ButtonViewHolder) holder;
Log.i("DEBUG", "" + getItemViewType(position));
Log.i("DEBUG", "hun" + getItemViewType(position));
buttonViewHolder.bottombutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int countt = 0;
Log.i("Checking the database", dbObjList.toString());
for (int i = 0; i< dbObjList.size();i++)
{
if(dbObjList.get(i).equals("") || dbObjList.get(i).equals("0"))
countt++;
}
Log.i("Checking the database", countt + "");
if (countt == 0) {
new AlertDialog.Builder(view.getContext())
.setTitle("Title")
.setMessage("Do you really want to submit?")
.setIcon(android.R.drawable.ic_dialog_alert)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Toast.makeText(context, "Submitted your response", Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton(android.R.string.no, null).show()
} else {
Toast.makeText(view.getContext(), "Still left to submit", Toast.LENGTH_SHORT).show();
}
}
});
}
if (getItemViewType(position) == CARD_VAL && position <= getItemCount()) {
Log.i("DEBUG", "" + getItemViewType(position));
GenericViewholder genericViewholder = (GenericViewholder) holder;
dbObjList.add(position,genericViewholder.database);
if(genericViewholder != null)
if(genericViewholder.count == 1){
countercount++;
Log.i("THE value of count :",countercount+"");
}
genericViewholder.ques.setText(ques[position]);
}
}
@Override
public int getItemCount() {
return ques.length + 1;
}
@Override
public long getItemId(int position) {
return super.getItemId(position);
}
@Override
public int getItemViewType(int position) {
//check for the termination point
//for the last point insert button
return (position < ques.length) ? CARD_VAL : BUTTON_VAL;
}
}
答案 0 :(得分:0)
尝试在onBindViewHolder之外声明你的buttonview持有者 因为每次视图绑定时onBindViewHolder都会运行 。所以这可能会导致您的观点重复
答案 1 :(得分:0)
尝试更改classCardAdapter
类
public classCardAdapter extends RecyclerView.Adapter<classCardAdapter.RecyclerViewHolder>
并更改方法:
@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//all stuff
}
创建一个包含所有视图数据的RecyclerViewHolder
类