我目前正在使用如下所示的UI
蓝色部分是ConstraintLayout,而紫色部分是其中的RecyclerView(它是RecyclerView,因为它的内容基于服务响应是动态的)。
我正在ConstraintLayout上设置onClick
处理程序,该处理程序会将用户带到另一个页面。问题在于RecyclerView正在消耗点击次数,而没有将其转发给其父项。因此onClick
处理程序适用于蓝色区域,不适用于紫色区域。
我尝试在RecyclerView中设置android:clickable="false"
和android:focusable="false"
,但仍不会将点击传播到其父级。
我遇到的一个解决方案是从ConstraintLayout扩展并覆盖onInterceptTouchEvent()
以返回true。但是,我在项目中有一个严格的要求,即不要创建自定义窗口小部件,因此无法使用此解决方案。
有没有办法告诉RecyclerView停止消耗触摸事件?
活动布局:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_margin="16dp"
android:background="#42d7f4"
android:onClick="navigate"
android:padding="16dp">
<TextView
android:id="@+id/headerText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="FRUITS"
android:textSize="36sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/itemsList"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#9f41f2"
android:clickable="false"
android:focusable="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>
项目布局:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/itemText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:clickable="false"
android:focusable="false" />
Activity.kt:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val rcView = findViewById<RecyclerView>(R.id.itemsList)
rcView.layoutManager = LinearLayoutManager(this)
val items = listOf("Apple", "Banana", "Oranges", "Avocado")
rcView.adapter = ItemAdapter(items)
}
fun navigate(view: View) {
Toast.makeText(this, "Navigating to details page", Toast.LENGTH_SHORT)
.show()
}
}
class ItemAdapter(private val data: List<String>) : RecyclerView.Adapter<ItemViewHolder>() {
override fun getItemCount(): Int = data.size
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item, parent, false)
return ItemViewHolder(view)
}
override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
holder.bind(data[position])
}
}
class ItemViewHolder(view: View) : RecyclerView.ViewHolder(view) {
private val itemTv: TextView = view.findViewById(R.id.itemText)
fun bind(item: String) {
itemTv.text = item
}
}
答案 0 :(得分:1)
完全阻止与单个视图内的任何事物进行交互的最简单方法可能是在其上放置一个透明视图,以拦截所有触摸事件。您可以在该视图上设置单击,以通过该视图管理所有功能。
例如这样的
DELETE
现在,您可以通过该视图执行功能,而不是通过<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_margin="16dp"
android:background="#42d7f4"
android:padding="16dp">
<TextView
android:id="@+id/headerText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="FRUITS"
android:textSize="36sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/itemsList"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#9f41f2"
android:clickable="false"
android:focusable="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<View
android:id="@+id/clickView"
android:layout_width="0dp"
android:layout_height="0dp"
android:onClick="navigate"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:clickable="true"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>
来执行。
此外,您还可以查看此answer
答案 1 :(得分:0)
一种解决方法是使用点击侦听器设置RecyclerView
行,并禁用RecyclerView
行的子级的点击和长按,如下所示:
ConstraintLayout: `android:onClick="navigate"`
For the item layout: `android:onClick="navigate"`
TextView: android:clickable="false"
android:longClickable="false"
(etc. for all children of the row)
我认为缺少的是使TextView
不可长时间单击。
答案 2 :(得分:0)
如果在设置适配器后冻结布局,它将不再吞下点击:
recyclerView.isLayoutFrozen = true // kotlin
recyclerView.setLayoutFrozen(true); // java
请记住,如果需要更改适配器中的数据,则必须先取消冻结布局,然后再调用notifyDataSetChanged,然后重新冻结布局。我不喜欢这种解决方案,但这是唯一对我有用的解决方案。
答案 3 :(得分:-1)
您可以将// project-view.jsx
const React = require('react');
const injectIntl = require('react-intl').injectIntl;
const GUI = require('scratch-gui');
const IntlGUI = injectIntl(GUI.default);
class Preview extends React.Component {
constructor (props) {
super(props);
this.state = {
projectId: 0
};
}
render() {
return (
<React.Fragment>
<IntlGUI
projectId={this.state.projectId}
/>
</React.Fragment>
);
}
}
module.exports.View = Preview;
GUI.setAppElement(document.getElementById('app'));
module.exports.initGuiState = guiInitialState => {
return GUI.initPlayer(guiInitialState);
}
module.exports.guiReducers = GUI.guiReducers;
module.exports.guiInitialState = GUI.guiInitialState;
module.exports.guiMiddleware = GUI.guiMiddleware;
module.exports.initLocale = GUI.initLocale;
module.exports.localesInitialState = GUI.localesInitialState;
的焦点设置为false。
import React from 'react';
import ReactDOM from 'react-dom';
const redux = require('redux');
const thunk = require('redux-thunk').default;
const Provider = require('react-redux').Provider;
import * as serviceWorker from './serviceWorker';
const IntlProvider = require('react-intl').IntlProvider;
const ProjectView = require('./views/project/project-view.jsx');
let locale = window._locale || 'en';
const reducer = {
...ProjectView.guiReducers
};
const reducers = redux.combineReducers(reducer);
const initState = {
locales: ProjectView.initLocale(ProjectView.localesInitialState, locale),
scratchGui: ProjectView.initGuiState(ProjectView.guiInitialState)
};
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || redux.compose;
const enhancers = composeEnhancers(
redux.applyMiddleware(thunk),
ProjectView.guiMiddleware
);
const store = redux.createStore(
reducers,
initState,
enhancers
);
const messages = {};
ReactDOM.render(
<Provider store={store}>
<IntlProvider
locale={locale}
messages={messages}
>
<ProjectView.View />
</IntlProvider>
</Provider>, document.getElementById('app'));
和/或设置其行RecyclerView
编辑
如果您想让recyclerView.setFocusable(false);
成为焦点
view.setFocusable(false);
有关更多信息,请参阅Google提供的official documentation
希望这会有所帮助,欢呼