【问题标题】:Buttons inside fragments not firing first time in scroll view?片段内的按钮没有在滚动视图中第一次触发?
【发布时间】:2026-02-05 05:15:02
【问题描述】:

在我的应用中,主页中有 5 个片段..

我正在使用相对布局来显示五个片段。我厌倦了线性布局,它也不起作用..

这是我的布局代码

 <android.support.v4.widget.NestedScrollView xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:http="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:background="@color/background"
    android:layout_width="match_parent"
   android:id="@+id/scrollView"
    android:layout_height="wrap_content">

    <RelativeLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:focusable="true"
        android:focusableInTouchMode="true"
        >
        <fragment android:name="com.example.Fragment1"
            android:id="@+id/fragment1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"

            android:layout_marginBottom="20dp"
            tools:layout="@layout/fragment_need_card" />

        <fragment android:name="com.example.Fragment2"
            android:id="@+id/fragment2"
            android:layout_width="match_parent"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginBottom="20dp"
            android:layout_below="@+id/fragment1"
            android:layout_height="wrap_content"
            tools:layout="@layout/fragment_discussion" />

        <fragment android:name="com.example.Fragment3"
            android:id="@+id/fragment3"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginBottom="20dp"
            android:layout_below="@+id/fragment2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            tools:layout="@layout/fragment_askapro" />


        <fragment android:name="com.example.Fragment4"
            android:id="@+id/fragment4"
            android:layout_width="match_parent"
            android:focusable="true"
            android:layout_below="@+id/askapro"
            android:layout_height="wrap_content"
            tools:layout="@layout/fragment3" />


        <fragment android:name="com.example.Fragment5"
            android:id="@+id/fragment5"
            android:layout_width="match_parent"
            android:layout_marginLeft="8dp"
            android:layout_below="@+id/fragment4"
            android:layout_marginBottom="20dp"
            android:layout_marginRight="8dp"
            android:layout_height="fill_parent"
            tools:layout="@layout/recent_articles" />

    </RelativeLayout>

</android.support.v4.widget.NestedScrollView>

默认情况下 2 个片段在屏幕上可见,对于接下来的 3 个片段,用户必须垂直滚动..

公共类 Fragment3 扩展 BaseFragment {

public Fragment3() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View v= inflater.inflate(R.layout.fragment3, container, false);
    CardView b= (CardView)v.findViewById(R.id.cardview);
    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent= new Intent(getActivity(),DummyEvent.class);
            startActivity(intent);

        }
    });



    return  v;
}

这个 onclick 监听器从来没有第一次触发,我必须点击两次..

我认为第三个片段没有获得焦点。可能是我在布局中绑定片段吗?有人有解决问题的想法吗?

【问题讨论】:

  • 试试我的答案,让我知道它是否有效
  • 我觉得这行android:layout_below="@+id/askapro" 是一个错字。请检查一下。应该是android:layout_below="@id/fragment3"
  • @Asthme 提供您的 fragment3 xml

标签: android android-fragments scrollview onclicklistener


【解决方案1】:

这个问题是由于 android 支持库 NestedScrollView 中的一个错误造成的。在这里能找到它 : https://code.google.com/p/android/issues/detail?id=178041

这个问题很可能会在下一个版本中得到修复。在那之前,您可以从这篇文章中看到解决方法: onClick method not working properly after NestedScrollView scrolled

【讨论】:

  • @Asthme 我对你的问题做了一些研究,它在 android 支持库中的错误。检查我编辑的解决方法的答案
【解决方案2】:

如果您使用的是androidX,请使用此更新'androidx.appcompat:appcompat:1.1.0-alpha04'. 或更高版本。

【讨论】:

    最近更新 更多