简体   繁体   中英

How to click view in LinearLayout?

There are multiple selectable TextView in the Dialog .

I'm looking for a way to get text values by selecting multiple views in a LinearLayout over and over.

As the first method, I thought of how to set the click event after setting the variables of each view. But this makes the code too long and messy.

As a second method, I thought of using the RecyclerView to place the view and set the click event. It sounds like a good method, but it seems inefficient because the number of items is so small that it makes no sense to use RecyclerView .

How can I set up a click event that gets the values of the views in the LinearLayout ?

XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ll_body_part"
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white">
        <TextView
            android:id="@+id/back"
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:text=" BACK "
            android:textSize="20dp"
            android:background="?attr/selectableItemBackground"
            android:gravity="center"
            android:padding="15dp" />

        <ImageView
            android:id="@+id/selection_state1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_dumbbell"
            android:backgroundTint="@android:color/transparent"
            android:layout_gravity="center_vertical|end"
            android:layout_marginRight="20dp"
            android:visibility="invisible"/>
    </FrameLayout>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white">
        <TextView
            android:id="@+id/chest"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text=" CHEST "
            android:textSize="20dp"
            android:background="?attr/selectableItemBackground"
            android:gravity="center"
            android:padding="15dp" />

        <ImageView
            android:id="@+id/selection_state2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_dumbbell"
            android:backgroundTint="@android:color/transparent"
            android:layout_gravity="center_vertical|end"
            android:layout_marginRight="20dp"
            android:visibility="invisible"/>
    </FrameLayout>
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white">
        <TextView
            android:id="@+id/leg"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text=" LEG "
            android:textSize="20dp"
            android:background="?attr/selectableItemBackground"
            android:gravity="center"
            android:padding="15dp" />

        <ImageView
            android:id="@+id/selection_state3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_dumbbell"
            android:backgroundTint="@android:color/transparent"
            android:layout_gravity="center_vertical|end"
            android:layout_marginRight="20dp"
            android:visibility="invisible"/>
    </FrameLayout>
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white">
        <TextView
            android:id="@+id/shoulder"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text=" SHOULDER "
            android:textSize="20dp"
            android:background="?attr/selectableItemBackground"
            android:gravity="center"
            android:padding="15dp" />

        <ImageView
            android:id="@+id/selection_state4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_dumbbell"
            android:backgroundTint="@android:color/transparent"
            android:layout_gravity="center_vertical|end"
            android:layout_marginRight="20dp"
            android:visibility="invisible"/>
    </FrameLayout>
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white">
        <TextView
            android:id="@+id/bieceps"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text=" BICEPS "
            android:textSize="20dp"
            android:background="?attr/selectableItemBackground"
            android:gravity="center"
            android:padding="15dp" />

        <ImageView
            android:id="@+id/selection_state"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_dumbbell"
            android:backgroundTint="@android:color/transparent"
            android:layout_gravity="center_vertical|end"
            android:layout_marginRight="20dp"
            android:visibility="invisible"/>
    </FrameLayout>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white">
        <TextView
            android:id="@+id/triceps"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text=" TRICEPS "
            android:textSize="20dp"
            android:background="?attr/selectableItemBackground"
            android:gravity="center"
            android:padding="15dp" />

        <ImageView
            android:id="@+id/selection_state6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_dumbbell"
            android:backgroundTint="@android:color/transparent"
            android:layout_gravity="center_vertical|end"
            android:layout_marginRight="20dp"
            android:visibility="invisible"/>
    </FrameLayout>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white">
        <TextView
            android:id="@+id/abs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text=" ABS "
            android:textSize="20dp"
            android:background="?attr/selectableItemBackground"
            android:gravity="center"
            android:padding="15dp" />

        <ImageView
            android:id="@+id/selection_state7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_dumbbell"
            android:backgroundTint="@android:color/transparent"
            android:layout_gravity="center_vertical|end"
            android:layout_marginRight="20dp"
            android:visibility="invisible"/>
    </FrameLayout>

    <Button
        android:id="@+id/check"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="START"
        android:textSize="27dp"
        android:backgroundTint="@color/purple_200"/>
</LinearLayout>

This is the code I tried.

class BodyPartDialogFragment : DialogFragment(), View.OnClickListener{
    private lateinit var ll: LinearLayout
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view: View = inflater.inflate(R.layout.fragment_body_part_dialog, container, false)
        ll = view.findViewById(R.id.ll_body_part)
        ll.setOnClickListener {
            onClick(it)
        }
        return view
    }
    override fun onClick(view: View?) {
        when (view?.id) {
            R.id.back -> Toast.makeText(context, "back", Toast.LENGTH_LONG)
            R.id.chest -> Toast.makeText(context, "chest", Toast.LENGTH_SHORT)
            R.id.leg -> Toast.makeText(context, "leg", Toast.LENGTH_SHORT)
            R.id.shoulder -> Toast.makeText(context, "shoulder", Toast.LENGTH_SHORT)
            R.id.bieceps -> Toast.makeText(context, "biceps", Toast.LENGTH_SHORT)
            R.id.triceps -> Toast.makeText(context, "triceps", Toast.LENGTH_SHORT)
            R.id.abs -> Toast.makeText(context, "abs", Toast.LENGTH_SHORT)
        }
    }
}

It seems that the entire LinearLayout is selected

Please tell me what to do

Should I just use Recycler View?

You are not clicking the views, you are clicking the linear layout

ll.setOnClickListener {
   onClick(it) //it here is view, which in this case is the linear layout
}

So if you want to only select the TextView and is always going to be a TextView the view you need, you can iterate through them:

fun addClickListeners(viewGroup: ViewGroup) {
    for (i in 0 until viewGroup.childCount) {
        val candidate = viewGroup.childAt(i) as? TextView
        if (candidate != null) {
            candidate.setOnClickListener {
                it.text.toString() //do something
            }            
        } else {
          (viewGroup.childAt(i) as? ViewGroup)?.let { 
              addClickListeners(it) //recursive for inner childs    
           }
        }
    }

}

Then call it like this

addClickListeners(ll)

Regarding if you should use or not a RecyclerView that is up to you. If your evaluation is the requirement is low enough for trusting that is always gonna be a TextView then do that. If you think it can increase the scope then use a RecyclerView . If you think there is gonna be a middle point where things grow but not that much, create a custom view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM