片段中的按钮不会在滚动视图中第一次触发?

时间:2015-10-27 09:53:09

标签: android android-fragments scrollview onclicklistener

在我的应用中,我在主页上有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听众从来没有第一次开火,我必须点击两次..

我认为第三个片段没有得到集中。我可以在布局中绑定片段吗?谁有想法解决这个问题?

2 个答案:

答案 0 :(得分:0)

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

这个问题很可能会在下一个版本中修复。直到那时你可以看到这篇文章的解决方法: onClick method not working properly after NestedScrollView scrolled

答案 1 :(得分:0)

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

相关问题