- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个回收服务适配器, View 是某种复杂的按钮,它是从一些 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>
我有一个处理其中点击事件的服务适配器,数据集是使用改造从网络服务加载的,这没有问题,但我的问题是我想点击每个项目的背景和一些其他 UI 和它们的位置要更改和存储,当数据集低于 4 时一切正常,但是当数据集大约为 6 或更多时,会出现一些奇怪的行为,例如:当我单击一个项目时,另一个项目是clicked ,我管理点击的方式是通过我的 View Layout "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 监听器?我还阅读了所有其他主题,但没有一个对我有帮助。
最佳答案
我已经查看了您的代码,我想我已经找到了问题所在。您有一个条件,您可以检查 row_index
是否等于 position
。但是,我没有看到此声明的任何其他部分。
当您的数据集很小时,它可以工作,小到您不必滚动它。但是,当您的数据集很大并且您必须滚动它时,它应该有一些随机行为,因为您没有指定语句的 else
部分。每次滚动时 View 都会被回收,并且在实现 onBindViewHolder
函数时,您需要准确地告诉每行要加载的内容。
因此,我建议您即使不满足条件(即 else 部分)也对 holder 项目进行分配。
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实现相同。你也可以考虑去那里看看。
关于java - 单击 recyclerAdapter 在大型数据集上工作很奇怪,但在小型数据集上工作正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49591610/
自从我 faced an issue由于背景图片对于不同分辨率的内容来说太短,我尝试将背景分成 3 部分并自动拉伸(stretch)中间部分以相应地填充顶部和底部图像之间的空间。不幸的是我没能在 CS
我从去年开始就在我的程序中运行这个函数(Linux 和 Windows)。 现在我需要实现一个新功能,我的新构建不再运行。 我还有其他使用 POST 的 CUrl 函数,结果是一样的:没问题,但我的
在评估函数应用方面,Haskell 是只支持普通降阶还是也支持应用降阶?我是否认为正常顺序是 Haskell 惰性的原因? 最佳答案 GHC 运行时不使用术语缩减策略,因为那会非常低效。事实上,GHC
怎么来的multi使用多处理池对多个“进程”上的数据进行分段和处理的函数比仅调用 map 慢(8 秒)。功能(6 秒)? from multiprocessing import Pool import
假设我正在渲染一个 3d GL_TRIANGLE。该对象需要 3 个顶点才能定义:A、B、C。我将此类数据放入缓冲区并通过 glVertexAttribPointer 将其绑定(bind)到着色器。
我有一个字体的三个文件,普通的,粗体的和浅色的。由于 font-weight:light 不存在,我该如何在 font-face 上设置 light 呢? 顺便问一下,font-weight:ligh
我是 C 的新手,我似乎无法弄清楚什么似乎是一个非常简单的指针问题。我的程序将行号添加到文件中。它逐行读入文件,然后在每行的开头添加一个行号。它在每个文件上都可以正常工作,如下所示: soccer@s
我有以下代码,我不确定为什么当它命中 Myclass 的析构函数时我会收到堆损坏检测错误。我相信我正在正确地释放内存?? #include #include using namespace std
有什么方法可以将“正常”数学符号解释为逆波兰符号 (RPN)..? 例如1) 2 + 3*4 - 1 = 234*+1-2) 5 (4-8) = 548- 你可以假设遵循 BODMAS 规则并且必须首
http://www.ergotopia.de/ergonomie-shop/ergonomische-kissen/orthopaedisches-sitzkissen的手机页面应该看起来像右边(检
我正在 Phonegap/Cordova 中构建一个应用程序。应用目前相当简单,但确实需要网络状态和地理定位插件才能工作。 到目前为止,我已经在 Android 上开发了该应用程序(目前它仅由一些基本
我一整天都在做这个,但没有运气 我设法在一行 TfidfVectorizer 中消除了问题 这是我的工作代码 from sklearn.feature_extraction.text import C
也许有人看到一个错误,问题是当我按btn2 (button 2)和btn3 (button 3)应用程序crashes时,但操作仍然有效,即video正在运行并且PDF打开,而button 1正常工作
我正在开发一个应用程序。它的第一页是登录屏幕。成功登录后,我想将用户带到选项卡式 Activity 。我怎样才能在安卓中做到这一点?谢谢 最佳答案 在 Android 中,启动 Activity 是通
我不确定我在这里做错了什么。 :normal! I### 当我对一个单词执行此命令时,我想要的最终结果是: ### word 但是我得到了这个: ###word 最佳答案 Vim 的 :normal是
我必须将 2 个静态矩阵发送到分配动态矩阵的函数,将矩阵 1 乘以矩阵 2,并返回新矩阵的地址。请注意,COMM 很常见。 我尝试删除 free_matrix 行,它工作正常。 void main()
我在我的一个项目中使用 Gnome libglib 并遇到了一个奇怪的错误。我可以输入 GList 的元素数量看起来仅限于 45 个。在第 45 个元素处,它给出了此错误 40 counter 41
我正在尝试获取“顶级”HWND 的尺寸。即,我想要 Firefox/Windows 资源管理器等的主 HWND 的当前尺寸。窗口。如果窗口最小化, GetWindowRect() 将不起作用。 Get
相同的标题:什么是索引 - 正常 - 全文 - 唯一? 最佳答案 普通索引用于通过仅包含行数据的切片或散列来加速操作。 全文索引向数据库的全文搜索 (FTS) 引擎指示它应该将数据存档在给定字段中,以
我正在使用 EnumParser来自 here它在 VC++ 中编译得很好,但是使用 gcc 我有这样的错误: ./Terminator.o: In function `EnumParser::Enu
我是一名优秀的程序员,十分优秀!