在Android中,如何检测ScrollView中的Click和LongClick

时间:2016-01-08 20:33:37

标签: android click scrollview

我的活动非常简单:只是ScrollView中的TextView。

我想在整个ScrollView上处理用户点击和longclicks以启动一些操作。我可以在TextView上轻松处理单击事件,但不能在ScrollView上处理。

当文本内容足够长时,它不是问题,但是当它只是一行文本而非用户需要点击特定行(屏幕顶部)时,这不是很有用。

如何在ScrollView中检测点击事件? (或任何其他解决方案)

示例活动:

public class TestActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);
}

public void testOnClick(View view) {
    Log.d("TestActivity", "Hello? Anybody there?");
    finish();
}}

布局:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff0000"
    android:clickable="true"
    android:onClick="testOnClick">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="test text\n test text\n test text" />
</ScrollView>

2 个答案:

答案 0 :(得分:0)

正如本文所述,您应将clickable false设置为滚动视图中的其他元素,以使其正常工作OnClickListener on scrollView

干杯

答案 1 :(得分:0)

您想要的是为TextView设置更大的点击目标。

只需在TextView上设置点击/长按一下监听器即可。您可以添加填充以增加视图的大小,如下所示:

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="16dp"
    android:paddingBottom="16dp"
    android:text="test text\n test text\n test text" />

这确保了点击目标至少为40dp,具体取决于您的字体大小,这很容易点击。