我有一个回收服务适配器,视图是某种复杂的按钮,它是从某个xml文件创建的:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/services_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginStart="20dp"
android:layout_marginTop="15dp"
android:gravity="center_horizontal"
android:orientation="vertical">
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/services_place_holder"
android:layout_width="@dimen/viewPlaceHolderWidth_services"
android:layout_height="@dimen/viewPlaceHolderHeight_services"
android:layout_marginTop="10dp"
android:background="@drawable/rounded_view_selector_services"
android:focusable="false"
android:gravity="center_horizontal"
android:orientation="vertical"
app:layout_constraintTop_toTopOf="@id/services_container">
<TextView
android:id="@+id/service_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:background="@drawable/rounded_text_view_for_services"
android:paddingBottom="2dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="2dp"
android:text="۲۳۲۲۲۳۵۶۶"
android:textAlignment="center"
android:textColor="@color/colorWhite"
android:textSize="12sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/service_image"
android:layout_width="38dp"
android:layout_height="38dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="6dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/service_price"
app:srcCompat="@drawable/placeholder_blue" />
<me.grantland.widget.AutofitTextView
android:id="@+id/service_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="11dp"
android:layout_marginStart="11dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:maxLines="1"
android:text="پرستار"
android:textColor="@color/colorElahiyeBlue"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/service_image"
app:minTextSize="6sp" />
</android.support.constraint.ConstraintLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="-120dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/services_gender_image"
android:layout_width="20dp"
android:layout_height="20dp"
android:src="@drawable/available_gender" />
<ImageView
android:id="@+id/services_prescription_image"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginLeft="7dp"
android:layout_marginStart="7dp"
android:src="@drawable/available_picture" />
<ImageView
android:id="@+id/services_info_image"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginLeft="7dp"
android:layout_marginStart="7dp"
android:src="@drawable/info" />
</LinearLayout>
</LinearLayout>
我有一个服务适配器来处理其中的click事件,数据集是使用改造从Web服务加载的,并且没有问题,但我的问题是我想要点击每个项目的背景和一些其他UI和它们的位置要更改和存储,当数据集低于4时一切正常但是当数据集大约为6或更多时,一些奇怪的行为看起来像:当我点击一个项目时,另一个项目是点击,我管理点击的方式是我的视图布局“services_container”或“services_place_holder”的isFocusable,适配器代码在这里:
public class ServicesAdapter extends RecyclerView.Adapter<ServicesAdapter.ViewHolder> {
private List<Integer> selectedServices = new ArrayList<>();
private LayoutInflater mInflater;
private ItemClickListener mClickListener;
private Context mContext;
private int row_index = -1;
// data is passed into the constructor
// public FieldOfEmploymentAdapter(Context context, List<Integer> colors, List<String> servicesDataItemsList) {
ServicesAdapter(Context context, List<ServicesDataItems> servicesDataItemsList) {
mContext = context;
this.mInflater = LayoutInflater.from(context);
// this.mViewColors = colors;
this.servicesDataItemsList = servicesDataItemsList;
}
// inflates the row layout from xml when needed
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.services_items, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
// binds the data to the view and textview in each row
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
// int color = mViewColors.get(position);
final ServicesDataItems servicesDataItems = servicesDataItemsList.get(position);
holder.linLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
row_index = position;
notifyDataSetChanged();
}
});
if (row_index == position) {
Toast.makeText(mContext, "Pos: " + position + " , GetAdapterPos: " + holder.getAdapterPosition(), Toast.LENGTH_SHORT).show();
// Toast.makeText(mContext, "SSSS" + MainActivity.servicesNumber.getText().toString(), Toast.LENGTH_SHORT).show();
int servicesNumber = Integer.parseInt(MainActivity.servicesNumber.getText().toString());
// Toast.makeText(mContext, "is instance of BitmapDrawable= " + holder.constraintLayout.isFocusable() + " ====== ", Toast.LENGTH_SHORT).show();
if (holder.linLayout.isSelected()){
Toast.makeText(mContext, "IsSelected ", Toast.LENGTH_SHORT).show();
}
if (holder.linLayout.isFocusable()) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
holder.constraintLayout.setBackground(ContextCompat.getDrawable(mContext, R.drawable.rounded_view_selector_services));
holder.linLayout.setFocusable(false);
MainActivity.servicesNumber.setText(String.valueOf(--servicesNumber));
removeSelectedService(position);
} else {
holder.constraintLayout.setBackgroundDrawable(ContextCompat.getDrawable(mContext, R.drawable.rounded_view_selector_services));
holder.linLayout.setFocusable(false);
MainActivity.servicesNumber.setText(String.valueOf(--servicesNumber));
removeSelectedService(position);
}
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
holder.constraintLayout.setBackground(ContextCompat.getDrawable(mContext, R.drawable.rounded_view_selector_services_selected));
holder.linLayout.setFocusable(true);
MainActivity.servicesNumber.setText(String.valueOf(++servicesNumber));
selectedServices.add(position);
} else {
holder.constraintLayout.setBackgroundDrawable(ContextCompat.getDrawable(mContext, R.drawable.rounded_view_selector_services_selected));
holder.linLayout.setFocusable(true);
MainActivity.servicesNumber.setText(String.valueOf(++servicesNumber));
selectedServices.add(position);
}
}
if (servicesNumber > 0) {
Drawable selectLocation = ContextCompat.getDrawable(mContext, R.drawable.rbtn_location_active);
ColorStateList colorStateList = ContextCompat.getColorStateList(mContext, R.color.rbtn_text_color_button);
new EnableLocationSelectButton(selectLocation, colorStateList).invoke();
}
if (servicesNumber == 0) {
Drawable selectLocation = ContextCompat.getDrawable(mContext, R.drawable.rbtn_location_dimmed);
ColorStateList colorStateList = ContextCompat.getColorStateList(mContext, R.color.ElahiyeGray);
new DisableLocationSelectButton(selectLocation, colorStateList).invoke();
}
boolean isPrescriptionImageVisible = false;
boolean isGenderImageVisible = false;
for (int selected : selectedServices) {
if (servicesDataItemsList.get(selected).has_prescriptions_image()) {
isPrescriptionImageVisible = true;
if (MainActivity.prescriptionImageHolder.getVisibility() == View.INVISIBLE) {
MainActivity.gender_prescription_selection.setVisibility(View.VISIBLE);
MainActivity.gender_prescription_separator.setVisibility(View.VISIBLE);
MainActivity.doctorPrescription.setVisibility(View.VISIBLE);
}
}
if (servicesDataItemsList.get(selected).hasGender()) {
isGenderImageVisible = true;
MainActivity.gender_prescription_selection.setVisibility(View.VISIBLE);
MainActivity.gender_prescription_separator.setVisibility(View.VISIBLE);
MainActivity.genderSelection.setVisibility(View.VISIBLE);
}
}
if (!isPrescriptionImageVisible) {
MainActivity.doctorPrescription.setVisibility(View.INVISIBLE);
MainActivity.prescriptionImageHolder.setVisibility(View.INVISIBLE);
}
if (!isGenderImageVisible) {
MainActivity.genderSelection.setVisibility(View.INVISIBLE);
}
if (!isPrescriptionImageVisible && !isGenderImageVisible) {
MainActivity.gender_prescription_selection.setVisibility(View.INVISIBLE);
MainActivity.gender_prescription_separator.setVisibility(View.INVISIBLE);
}
}
holder.service_name.setText(servicesDataItems.getServices_name());
holder.service_price.setText(servicesDataItems.getServices_price());
}
private void removeSelectedService(int position) {
int index = 0;
for (int selected : selectedServices) {
if (selected == position) {
selectedServices.remove(index);
return;
}
index++;
}
}
// total number of rows
@Override
public int getItemCount() {
return servicesDataItemsList.size();
}
// stores and recycles views as they are scrolled off screen
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
// ImageView myImageView;
TextView service_name;
TextView service_price;
ConstraintLayout constraintLayout;
LinearLayout linLayout;
ImageView serviceGender;
ImageView servicePrescription;
ViewHolder(View itemView) {
super(itemView);
// myImageView = itemView.findViewById(R.id.service_image);
service_name = itemView.findViewById(R.id.service_name);
service_price = itemView.findViewById(R.id.service_price);
constraintLayout = itemView.findViewById(R.id.services_place_holder);
linLayout = itemView.findViewById(R.id.services_container);
serviceGender = itemView.findViewById(R.id.services_gender_image);
servicePrescription = itemView.findViewById(R.id.services_prescription_image);
itemView.setOnClickListener(this);
}
}
请告诉我如何正确管理每个项目上的OnClick监听器?我还阅读了所有其他主题,但没有一个对我有帮助。
答案 0 :(得分:1)
我已经完成了你的代码,我想我已经发现了问题。您有条件检查row_index
是否等于position
。但是,我没有看到此声明中的任何其他部分。
当您的数据集较小时,它可以正常工作,只需要您无需滚动它即可。但是,当您的数据集很大并且您必须滚动它时,它应该有一些随机行为,因为您没有指定语句的else
部分。每次滚动时都会回收视图,并且在实现onBindViewHolder
函数时需要确切地说明每行中要加载的内容。
因此,即使条件不满足,我也建议您为持有人项目进行分配(即其他部分)。
if (row_index == position) {
// Your current implementation
} else {
// Set the views accordingly when the above condition is not met.
}
<强>更新强>
如果您希望在RecyclerView
中选择多个项目,则可能需要保留一个单独的数组来跟踪所选项目,然后根据您保留的曲目回收项目在你的数组中。例如,让我们考虑一个数组来跟踪点击次数。
public int[] clickTrack = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
现在,当您单击某个项目时,只需将该单击的轨道保持在您的阵列中即可。
@override
public void onClick(View v) {
if(clickTrack[position] == 0) clickTrack[position] = 1;
else clickTrack[position] = 0;
notifyDataSetChanged();
}
现在,当您单击某些项目时,数组将看起来像这样。
{0, 1, 0, 1, 1, 0, 1, 0, 1, 0}
在onBindViewHolder
中,只需根据您在该数组中找到的值来扩展项目,您可以在其中保持跟踪点击。
if(clickTrack[position] == 1) someView.expand();
else someView.collapse();
这是一个示例实现。我希望你明白这个主意。这是一个实现相同的nice answer。你也可以考虑看看那里。