【问题标题】:ListView - differentiate between a checkbox selection and a text selectionListView - 区分复选框选择和文本选择
【发布时间】:2014-11-05 08:21:49
【问题描述】:

鉴于下面的 ListView,我想根据用户是选择文本(创建新活动)还是单击关联的复选框(将其添加到收藏夹列表)来执行两种不同的操作。是否可以通过此设置实现这一点,还是我必须使用自定义适配器甚至不同的布局?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    </ListView>
</LinearLayout>


setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, teams));
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String team_name = adapterView.getItemAtPosition(i).toString().trim();
                Intent intent = new Intent("blah.blah.blah");
                intent.putExtra("team", team_name);
                startActivity(intent);
            }
});

一个

【问题讨论】:

    标签: android android-listview


    【解决方案1】:

    如果我的理解正确,您的 ListView 应该包含 TextBox 和 CheckBox 并且 TextBox 和 CheckBox 是可点击的,而不是 ListView 本身。

    为此,您必须制作自定义适配器,您将在其中为 TextBox 和 CheckBox 创建侦听器。

    public class CustomAdapter extends BaseAdapter {
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // your_costum_view should contain textbox and checkboc
            view = inflater.inflate(R.layout.your_costum_view, null);
    
            // Get your checkbox and textbox and add listeners
            TextView textView = (TextView) view.findById(R.id.textView);
            textView.setOnClickListener...
    
            Checkbox checkbox=(CheckBox)view.findById(R.id.checkBox);
            checkBox.setOnClickListener...
    
    
            return view;
        }
    
    }
    

    your_costum_view 布局示例

    <?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="wrap_content" />
    
        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    
        <CheckBox
            android:id="@+id/checkBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    
    </LinearLayout>
    

    【讨论】:

      【解决方案2】:

      使用自定义适配器将帮助您跟踪视图,这比使用您无法控制的默认适配器更容易。供参考:http://www.vogella.com/tutorials/AndroidListView/article.html#adapterown_custom

      【讨论】:

        【解决方案3】:

        使用自定义适配器肯定会让您的生活更轻松。在适配器中,您可以同时引用复选框和 textView,并为每个添加一个 onClick 侦听器 - 从那里您还可以添加代码来处理每个事件。我还建议使用 recyclerView 而不是 ListView。它是 Android 5.0 中的新事物,它确实比普通的 ListView 更容易使用。希望这可以帮助: RecyclerView Help

        【讨论】:

          【解决方案4】:

          在 XML 布局中将 CheckBox 设置为 focusable="false"。

          android:focusable="false"
          

          如果不运行,请转到 to this link 并查看示例,因为您需要在列表视图中创建自定义行并进行设置:

          Checkbox checkbox=(CheckBox)view.findById(R.id.checkboxID);
          checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
          
                  @Override
                  public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                      //do stuff
          
                  }
              });
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-07-26
            • 1970-01-01
            • 2014-02-09
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多