Android自定义多View RemoteViews通知:单条View滑动手势无响应
问题:通知内子项无法检测触摸事件,滑动关闭功能失效
我开发的应用通知数量过多,打算将单个Notification拆分为包含多个子View的RemoteViews,实现每条子View可滑动关闭的效果。但目前尝试通过Inflate View并设置setOnTouchListener的方式来监听触摸事件,所有View都无法检测到触摸动作。相关代码及布局如下:
自定义通知代码
public class CustomNotification { RemoteViews remoteViewsSmall; RemoteViews remoteViewsBig; Context context; public CustomNotification(Context context) { this.context = context; RemoteViews remoteViewsSmall = new RemoteViews(context.getPackageName(), R.layout.notification_main); this.remoteViewsSmall = addLine(remoteViewsSmall); RemoteViews remoteViewsBig = new RemoteViews(context.getPackageName(), R.layout.notification_main); this.remoteViewsBig = addLine(remoteViewsBig); LinearLayout linearLayoutParent = ((LinearLayout) ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.notification_main, null)); linearLayoutParent.setOnTouchListener((v, event) -> detectMove(event)); for (int i = 0; i < linearLayoutParent.getChildCount(); i++) { View childAt = linearLayoutParent.getChildAt(i); childAt.setOnTouchListener((v, event) -> detectMove(event)); } LinearLayout linearLayout1stLine = ((LinearLayout) ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.notification_line, linearLayoutParent)); linearLayout1stLine.setOnTouchListener((v, event) -> detectMove(event)); for (int i = 0; i < linearLayout1stLine.getChildCount(); i++) { View childAt = linearLayout1stLine.getChildAt(i); childAt.setOnTouchListener((v, event) -> detectMove(event)); } View viewBig = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(remoteViewsBig.getLayoutId(), null); View viewSmall = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(remoteViewsSmall.getLayoutId(), null); LinearLayout parentLinearLayoutBig = viewBig.findViewById(R.id.notificationParentLinearLayout); parentLinearLayoutBig.setOnTouchListener((v, event) -> detectMove(event)); for (int i = 0; i < parentLinearLayoutBig.getChildCount(); i++) { View childAt = parentLinearLayoutBig.getChildAt(i); childAt.setOnTouchListener((v, event) -> detectMove(event)); } LinearLayout parentLinearLayoutSmall = viewSmall.findViewById(R.id.notificationParentLinearLayout); parentLinearLayoutSmall.setOnTouchListener((v, event) -> detectMove(event)); for (int i = 0; i < parentLinearLayoutSmall.getChildCount(); i++) { View childAt = parentLinearLayoutSmall.getChildAt(i); childAt.setOnTouchListener((v, event) -> detectMove(event)); } } private boolean detectMove(MotionEvent event) { Log.i("TAG", event.getAction() + " detected"); return true; } private RemoteViews addLine(RemoteViews remoteView) { RemoteViews remoteViewLine = new RemoteViews(context.getPackageName(), R.layout.notification_line); remoteViewLine.setTextViewText(R.id.notification1stLine1stRowTxtView, "some string1"); remoteViewLine.setTextViewText(R.id.notification1stLine2ndRowTxtView, "some string2"); remoteView.addView(R.id.notificationParentLinearLayout, remoteViewLine); return remoteViewLine; } }
通知主父布局(notification_main.xml)
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:id="@+id/notificationParentLinearLayout"> </LinearLayout>
子项布局(notification_line.xml)
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="10dp" android:orientation="horizontal"> <TextView android:id="@+id/notification1stLine1stRowTxtView" style="@style/TextAppearance.Compat.Notification.Title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:maxLines="1" tools:text="Example text" /> <TextView android:id="@+id/notification1stLine2ndRowTxtView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:maxLines="1" tools:text="Example text" /> </LinearLayout>
内容的提问来源于stack exchange,提问作者janusz j
相关产品推荐
相关产品推荐

