我正在一个需要Dialogs
的项目中使用RecyclerViews
。
在所有类似情况下,每当您滚动滚动时,在侦听器能够捕获另一个事件之前,都需要间隔几秒钟。
每个项目都包含一个ConstraintLayout
,一个可绘制的背景以及一些TextViews
。
我尝试过:
setOnClickListener
和OnClick
(黄油刀)用于
ConstraintLayout
。Views
。setOnTouchListener
/ OnTouch
(黄油刀)。onBindViewHolder
,以免造成延迟,但不会
成功。Dialog 类的核心方面如下所示:
public class MyDialog extends Dialog {
@BindView(R.id.some_rv)
RecyclerView myRV;
@BindView(R.id.nsv)
NestedScrollView nestedScrollView;
public MyDialog(@NonNull Context context) {
super(context, R.style.MyCustomisedDialogs);
setContentView(R.layout.dialog_mydialog);
getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
ButterKnife.bind(this);
setOnShowListener(d -> nestedScrollView.scrollTo(0, 0));
}
public void runDialog(List<MyObject> itemsList) {
myRV.setAdapter(new MyAdapter(itemsList));
myRV.setLayoutManager(new LinearLayoutManager(getContext(),
LinearLayoutManager.VERTICAL, false));
this.show();
}
…
}
适配器:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private List<MyObject> myList;
public MyAdapter(List<MyObject> myList) {
this.myList= myList;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.my_rv_layout, viewGroup, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {
MyObject myObject = myList.get(i);
myViewHolder.myConstraintLayout.setOnClickListener(v -> doSomething());
myViewHolder.headerTV.setText(myObject.getHeader());
myViewHolder.titleTV.setText(myObject .getTitle());
myViewHolder.mainBackgroundIV.setImageResource(myObject.getImage());
myViewHolder.mainBackgroundIV.setColorFilter(ContextCompat.getColor(context, myObject.getTintColor()));
}
@Override
public int getItemCount() {
return myList.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
/* I have a set of BindViews for each required View
f.i.
** @BindView(R.id.cl) **
** ConstraintLayout myConstraintLayout**
*/
MyViewHolder(@NonNull View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}
}
XML 的核心方面如下所示:
<?xml version="1.0" encoding="utf-8"?>
<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/cl"
android:layout_width="match_parent">
<ImageView
android:id="@+id/main_bg_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
/>
<LinearLayout
android:id="@+id/some_ll"
android:layout_width="140dp"
android:layout_height="32dp"
android:layout_gravity="center_horizontal"
android:background="@drawable/my_shape"
android:gravity="center"
android:orientation="horizontal"
android:visibility="gone"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
>
<TextView
android:id="@+id/button_goto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/some_string"
android:textColor="@color/some_custom_colour"
android:textSize="16sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginBottom="5dp"
android:orientation="horizontal"
app:layout_constraintBottom_toTopOf="@+id/title_tv"
app:layout_constraintStart_toStartOf="@+id/title_tv">
<TextView
android:id="@+id/header_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="32sp"/>
<ImageView
android:visibility="gone"
android:id="@+id/some_selection_iv"
android:layout_width="@dimen/dp_24"
android:layout_height="@dimen/dp_24"
android:layout_gravity="bottom"
android:src="@drawable/ic_some_drawable" />
</LinearLayout>
<TextView
android:id="@+id/title_tv"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginBottom="12dp"
app:layout_constraintBottom_toTopOf="@id/info_tv"
app:layout_constraintStart_toStartOf="@id/info_tv"
/>
<TextView
android:id="@+id/info_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="@+id/main_bg_image"
app:layout_constraintStart_toStartOf="@+id/main_bg_image"
/>
</android.support.constraint.ConstraintLayout>
我希望为onClick
和onTouch
的侦听者找到一种方法,至少在滚动后直接触摸屏幕时可以触发它们。即使所有项目都位于RecyclerView
的主屏幕上,并且没有发生实际的滚动,但如果我按并滚动,也会发生相同的行为,这意味着在经过几秒钟之前没有监听器处于活动状态。