buttons within listview

主宰稳场 提交于 2020-01-06 03:00:28

问题


I have listview with row layout like

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" 
    android:weightSum="5"
    >

        <TextView 
        android:layout_width="0dp"
        android:layout_height="wrap_content" 
        android:id="@+id/ArriveTime"
        android:gravity="center" 
        android:layout_weight="1" />


        <TextView 
        android:layout_width="0dp"
        android:layout_height="wrap_content" 
        android:id="@+id/Name"
        android:gravity="center" 
        android:layout_weight="1" />

        <Button
        android:focusable="false"

        android:layout_width="0dp"
        android:layout_height="wrap_content" 
        android:id="@+id/Arrive"
        android:gravity="center" 
        android:layout_weight="1"           
        android:text="Arrive" />

        <Button
        android:focusable="false"

        android:layout_width="0dp"
        android:layout_height="wrap_content" 
        android:id="@+id/Encounter"
        android:gravity="center" 
        android:layout_weight="1"           
        android:text="Encounter" />

        <Button
         android:focusable="false"

        android:layout_width="0dp"
        android:layout_height="wrap_content" 
        android:id="@+id/Exit"
        android:gravity="center" 
        android:layout_weight="1"           
        android:text="Exit" />




</LinearLayout>

I want when click on row be able to click on buttons so I set android:focusable="false" and in on click I say

    MainGrid.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                  int position, long id) {

             Log.d("zzzzzzzzzzzzzzzzzzzzzzzz",""+ oldPosition);

            if(oldPosition != -1)
            {
                MainGrid.getChildAt(oldPosition).setFocusable(false);
                //MainGrid.findViewById(oldPosition).setFocusable(false);
            } 

            // Set the whole list view unfocusable 
             MainGrid.setFocusable(false);


            // focus only on this 
            view.setFocusable(true);

            oldPosition = position -1;

            SetMenueOnClick(position-1) ;
 }});

the problem is that after the first click on the row, the buttons be active and I cannot click on it again any idea to fix that , I need to activate the buttons on the row I click then transfer the focus to the row for further click


回答1:


If you want both the line and the buttons to be clickable (all the time), you don't need to go through all those gyrations. Simply add these two lines to your button callouts in the row xml:

    android:focusable="false"
    android:focusableInTouchMode="false"

Then you just set button click listeners in your adapter for each button and item click listeners in the activity where you call the list as normal.

If you want the buttons disabled until you click on the row (I wasn't clear if that was the goal or just an after-effect of the way you were trying to go about getting both clickable), the set up in the xml is the same as far as the focusable callouts, set the buttons to disabled and then in the onListItemClick set up an if statement using a flag to toggle them clickable and not clickable by using button.setEnabled(false); and button.setEnabled(true);.




回答2:


You can try using:

android:descendantFocusability="blocksDescendants"

In your layout XML, instead of modifying each element separately.

You can read more on button in a listview issues Here

Especially comments #27 & #31

Hope it helps




回答3:


Other than setFocusable(true) you can also use setChecked(boolean b). But you can use this method on a CompoundButton.

From java:

CompoundButton cb = (CompoundButton) findViewById(R.id.yourButton); 
cb.setChecked(true);

In XML

<CompoundButton
    android:id="@+id/yourButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/button_stats"
    android:text="@string/stat_hard">
</CompoundButton>

Where button_stats.xml is a file located in res/drawable which contains the following lines:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/generic_button_normal" android:state_checked="false"/>
<item android:drawable="@drawable/generic_button_checked" android:state_checked="true"/>
</selector>


来源:https://stackoverflow.com/questions/10998383/buttons-within-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!