我有一个为recycleview视图项充气的框架布局。我希望背景视图中的按钮/图像视图在滑动前景视图时能够响应onClick或onTouch事件。此刻,当不滑动前景时,按钮单击将起作用。看到:
<RelativeLayout android:id="@+id/backGround"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/color_pms_7578">
<ImageView
android:id="@+id/view_icon"
android:layout_width="@dimen/ic_eye"
android:layout_height="@dimen/ic_eye"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginEnd="@dimen/padd_10"
android:src="@drawable/ic_eye"
android:contentDescription="@string/view" />
<TextView
style="@style/TextViewStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toStartOf="@+id/view_icon"
android:layout_centerVertical="true"
android:layout_marginEnd="@dimen/padd_10"
android:text="@string/view"
android:textSize="13sp" />
</RelativeLayout>
<RelativeLayout android:id="@+id/foreGround"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="@dimen/padd_10"
android:outlineSpotShadowColor="@color/color_pms_cool_gray_9"
android:background="@color/color_pms_778">
<LinearLayout android:id="@+id/top"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/color_pms_7462"
android:orientation="horizontal">
<TextView
android:id="@+id/textFacilityName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:layout_marginBottom="@dimen/activity_vertical_margin"
android:layout_marginEnd="@dimen/activity_horizontal_margin"
android:layout_marginStart="@dimen/activity_horizontal_margin"
style="@style/TextViewStyle"
android:textSize="@dimen/section_header"/>
<TextView
android:id="@+id/textStatus"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/activity_horizontal_margin"
android:gravity="end"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:layout_marginBottom="@dimen/activity_vertical_margin"
style="@style/TextViewStyle"/>
</LinearLayout>
<LinearLayout android:id="@+id/bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/color_pms_7462"
android:layout_below="@+id/top"
android:orientation="horizontal">
<TextView android:id="@+id/textFacilityAddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/activity_vertical_margin"
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:textSize="12sp"
android:maxLines="1"
android:ellipsize="end"
android:scrollHorizontally="true"
android:ems="15"
style="@style/TextViewStyle"/>
<TextView android:id="@+id/textDateCompleted"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/activity_vertical_margin"
android:textSize="12sp"
android:gravity="end"
android:layout_marginEnd="@dimen/activity_horizontal_margin"
style="@style/TextViewStyle"/>
</LinearLayout>
</RelativeLayout>
请参见RecylerView ItemTouchHelper.SimpleCallback
public class SwipeToPerformActionCallBack extends ItemTouchHelper.SimpleCallback{
private RecyclerItemTouchHelperListener listener;
public SwipeToPerformActionCallBack() {
super(0, ItemTouchHelper.LEFT);
//this.listener = listener;
}
@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder viewHolder1) {
return false;
}
@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
//listener.onSwiped(viewHolder, i, viewHolder.getAdapterPosition());
Log.d(TAG, " i have been swiped..");
}
@Override
public int convertToAbsoluteDirection(int flags, int layoutDirection) {
return super.convertToAbsoluteDirection(flags, layoutDirection);
}
@Override
public void onChildDraw(Canvas c, @NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive){
final View foreGroundView = ((AssessmentListAdapter.ViewHolder)viewHolder).foreGround;
getDefaultUIUtil().onDraw(c, recyclerView, foreGroundView, dX/4, dY, actionState, isCurrentlyActive);
}
@Override
public void onChildDrawOver(Canvas c, @NonNull RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive){
final View foreGroundView = ((AssessmentListAdapter.ViewHolder)viewHolder).foreGround;
getDefaultUIUtil().onDraw(c, recyclerView, foreGroundView, dX/4, dY, actionState, isCurrentlyActive);
}
@Override
public void onSelectedChanged(@NonNull RecyclerView.ViewHolder viewHolder, int actionState) {
if(viewHolder != null){
final View foreGroundView = ((AssessmentListAdapter.ViewHolder)viewHolder).foreGround;
getDefaultUIUtil().onSelected(foreGroundView);
}
}
}
和viewHolder:
public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView facilityName, assessmentStatus, address, dateCompleted;
ImageView btnView;
public RelativeLayout foreGround, backGround;
String TAG = "AssessmentViewHolder";
Context context;
public ViewHolder(@NonNull View itemView, Context context) {
super(itemView);
this.context = context;
facilityName = itemView.findViewById(R.id.textFacilityName);
assessmentStatus = itemView.findViewById(R.id.textStatus);
address = itemView.findViewById(R.id.textFacilityAddress);
dateCompleted = itemView.findViewById(R.id.textDateCompleted);
foreGround = itemView.findViewById(R.id.foreGround);
backGround = itemView.findViewById(R.id.backGround);
btnView = itemView.findViewById(R.id.view_icon);
btnView.setOnClickListener(this);
btnView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.d(TAG, "somebody has touched me oooo! ");
return false;
}
});
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
//continue assessment..
int position = getAdapterPosition();
if(v != null){
Log.d(TAG, "A view of type : " + v.toString() +" was clicked");
if(v instanceof ImageView) {
long assessmentId = (long) v.getTag(R.id.assessmentId);
Log.d(TAG, "assessment id :" + assessmentId);
Intent intent = new Intent(AppConstants.ACTION_LOAD_ASSESSMENT_DETAILS);
intent.putExtra("AssessmentId", assessmentId);
intent.addCategory(Intent.CATEGORY_DEFAULT);
Log.i(TAG, "Intent sent with action :" + intent.getAction());
PackageManager pm = context.getPackageManager();
ComponentName cn = intent.resolveActivity(pm);
if (cn != null) {
context.startActivity(intent);
} else {
Log.w(TAG, "No Activity Found to Handle Action : " + intent.getAction());
}
}else {
Log.d(TAG, v.toString());
long assessmentId = (long) v.getTag(R.id.assessmentId);
Log.d(TAG, "assessment id :" + assessmentId);
Intent intent = new Intent(AppConstants.ACTION_LOAD_ASSESSMENT_HOME);
intent.putExtra("AssessmentId", assessmentId);
intent.addCategory(Intent.CATEGORY_DEFAULT);
Log.i(TAG, "Intent sent with action :" + intent.getAction());
PackageManager pm = context.getPackageManager();
ComponentName cn = intent.resolveActivity(pm);
if (cn != null) {
context.startActivity(intent);
} else {
Log.w(TAG, "No Activity Found to Handle Action : " + intent.getAction());
}
}
}
if(position != RecyclerView.NO_POSITION){
Log.d(TAG, "item in position :" + position + " clicked");
}
}
}