onClickListener doesnt work with clickable child views in Fragment (AndroidStudio)

285 views Asked by At

i need to make my whole fragment clickable. But when I implement onClickListener on view element it doesnt "hear" the clicks made on child views.

KOTLIN:

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
 view = inflater.inflate(R.layout.myFragment, container, false)

 val swipeRefreshLayout = view?.findViewById<SwipeRefreshLayout>(R.id.refreshLayout)
        swipeRefreshLayout?.setOnRefreshListener{
            startSynchronize(true)
            swipeRefreshLayout?.isRefreshing = false
        }

view?.setOnClickListener { Log.d("TAG", "onCreateView: view clicked!") }


 return view

}

there is myFragment xml file


<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:id="@+id/fragment_layout"
    android:padding="@dimen/padding"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/color_pink"
    app:layout_constraintTop_toTopOf="parent">

    <TabHost
        android:id="@+id/tabHost"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="@dimen/padding"
            android:background="@color/color_orange"
            android:orientation="vertical">

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="@dimen/padding0" />

            <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
                android:id="@+id/refreshLayout"
                android:background="@color/color_lemon_yellow"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                    <FrameLayout
                        android:id="@+id/data_frame_layout"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:visibility="gone">


                        <FrameLayout
                            android:id="@android:id/tabcontent"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layout_margin="@dimen/margin">


                            <include layout="@layout/first_fragment" />

                            <include layout="@layout/second_fragment" />

                            <include layout="@layout/third_fragment" />

                            <include layout="@layout/fourth_fragment" />

                        </FrameLayout>

                    </FrameLayout>

                </LinearLayout>
            </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
        </LinearLayout>
    </TabHost>

</FrameLayout>


Log.d is printed only when i click on pink or orange colored views. When ever i click, green or yellow nothing happens. I think its becauase those views arleady are clickable/swipeable/touchable. I really need to make this whole view clickable no matter if it contains any clickable child or not, because i want to use this functionality in other activities. Is it possible to create in that way? enter image description here

I have tried:

replacing the view with getView() and container replacing onClickListener with onTouchListener

Adding these attributes to every child element: android:duplicateParentState="true" android:clickable="true"

Adding android:clickable="true" to root element and android:clickable="false" to every child view.

Adding swipeRefreshLayout?.isClickable = false to onCreateView

none of them worked. For now i have no more ideas.

1

There are 1 answers

0
Rob On

but if you decide to not change your view hierarchy , you can also use this to set the same click listener to all your views

val clickListener = View.OnClickListener { view ->                
     println("$view clicked ....")           
}            
(view as ViewGroup).children.forEach {                
    it.setOnClickListener(clickListener)            
}