lundi 29 juin 2015

How to avoid triggering the fast-scroller when a checkbox in the list-row is being clicked?

Background

Suppose I have a ListView with a checkbox for each row , and I have a lot of items to show so the fast-scroller should be enabled.

The problem

when clicking on a checkbox, if it's too near the right edge of the ListView, it will trigger the fast-scroller and also scroll using it, instead of toggling the checkbox.

Code

Here's a sample code:

MainActivity.java

public class MainActivity extends AppCompatActivity
  {

  @Override
  protected void onCreate(Bundle savedInstanceState)
    {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ListView listView=(ListView)findViewById(android.R.id.list);
    String[] items=new String[200];
    for(int i=0;i<items.length;i++)
      items[i]="item "+i;
    ArrayAdapter<String> itemsAdapter=
            new ArrayAdapter<String>(this,R.layout.list_item,android.R.id.text1,items);

    listView.setAdapter(itemsAdapter);
    }
  }

list_item.xml

<LinearLayout
  xmlns:android="http://ift.tt/nIICcg"
  xmlns:tools="http://ift.tt/LrGmb4"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal"
  android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
  android:paddingLeft="?attr/listPreferredItemPaddingLeft"
  android:paddingRight="?attr/listPreferredItemPaddingRight"
  android:paddingStart="?android:attr/listPreferredItemPaddingStart"
  tools:context=".MainActivity">

  <TextView
    android:id="@android:id/text1"
    android:layout_width="0px"
    android:layout_height="wrap_content"
    android:layout_weight="1"/>

  <CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

</LinearLayout>

activity_main.xml

<LinearLayout
  xmlns:android="http://ift.tt/nIICcg"
  xmlns:tools="http://ift.tt/LrGmb4"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  tools:context=".MainActivity">

  <ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fastScrollEnabled="true"/>
</LinearLayout>

The question

How can I avoid this, and let the checbox being checked instead of triggering the scrolling using the fast-scroller?




Aucun commentaire:

Enregistrer un commentaire