【问题标题】:ListFragment with checkbox onListItemClick never gets called带有复选框 onListItemClick 的 ListFragment 永远不会被调用
【发布时间】:2015-03-25 08:33:50
【问题描述】:

我已经阅读了一些 SO 问题,例如 thisthis,但仍然无法弄清楚我的代码有什么问题。 我有一个ListFragment,每一行都有一个TextView 和一个CheckBox。 单击CheckBox 可以正常工作,但单击TextView 没有任何作用,并且OnListItemClick 不会被调用。我还尝试动态添加一个OnClickListener,使其工作,但这不是正确的方法,而且它也缺乏点击的GUI反馈(项目被“突出显示”一秒钟)。

这是我用于每个项目的 textview_with_checkbox.XML:

<?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"
    android:clickable="true"
    android:focusable="true"
    android:gravity="left"
    android:descendantFocusability="blocksDescendants"
    android:orientation="horizontal" >
<TextView
    android:id="@+id/textview_event_name"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:paddingLeft="3dp"
    android:paddingTop="5dp"
    android:focusable="true"
    android:singleLine="false" />

<CheckBox
    android:id="@+id/checkbox_for_event_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0.05"
    android:focusable="false"
    android:clickable="false" />

</LinearLayout>

现在是代码:

@Override
public void onActivityCreated(Bundle savedInstanceState) 
{
    super.onActivityCreated(savedInstanceState);
    displayEventsLogFiles();
}

@Override
public void onListItemClick(ListView l, View v, int position, long id) 
{
    super.onListItemClick(l, v, position, id);

    String chosenItem = (String) getListAdapter().getItem(position);
    mCallBack.onEventItemSelected(chosenItem);

}

static class myCustomAdapterViewHolder
{
    public TextView eventName;
    public CheckBox eventCheckBox;
}

private class myCustomAdapter extends ArrayAdapter<String>
{   
    private ArrayList<String> sortedList;
    private Context m_oContext;
    List<String> m_oValues = null;

    public myCustomAdapter(Context cont, int viewResId, List<String> objects) 
    {
        super(cont, viewResId, objects);
        m_oContext = cont;
        m_oValues = objects;
        sortedList = new ArrayList<String>();
        for (String str : objects)
            sortedList.add(str);
        java.util.Collections.sort(sortedList);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View l_oRowView = convertView;
        myCustomAdapterViewHolder l_oInitializedViewHolder = null;
        final int l_nPosition = position;
        // Use convertView if possible, otherwise inflate a view:
        if (l_oRowView == null)
        {
            LayoutInflater inflater = (LayoutInflater) m_oContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            l_oRowView = inflater.inflate(R.layout.textview_with_checkbox, parent, false);

            // PlaceHolder pattern:
            final myCustomAdapterViewHolder l_oViewHolder = new myCustomAdapterViewHolder();
            l_oViewHolder.eventName = (TextView) l_oRowView.findViewById(R.id.textview_event_name);
            l_oViewHolder.eventCheckBox = (CheckBox) l_oRowView.findViewById(R.id.checkbox_for_event_name);

            l_oViewHolder.eventCheckBox.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    CheckBox cb = (CheckBox)v;
                    //cb.setChecked(!cb.isChecked());

                    String l_sFilename = l_oViewHolder.eventName.getText().toString();
                    if (cb.isChecked())
                    {
                        if (!m_lstSelectedFilenames.contains(l_sFilename))
                            m_lstSelectedFilenames.add(l_sFilename);
                    }
                    else
                    {
                        if (m_lstSelectedFilenames.contains(l_sFilename))
                            m_lstSelectedFilenames.remove(l_sFilename);
                    }
                }
            });
            l_oViewHolder.eventCheckBox.setFocusable(false);
            //l_oViewHolder.eventCheckBox.setClickable(false);
            // "Add" the viewHolder as a tag:
            l_oRowView.setTag(l_oViewHolder);
            l_oInitializedViewHolder = l_oViewHolder;
        }
        else
            l_oInitializedViewHolder = (myCustomAdapterViewHolder) l_oRowView.getTag();

        // By now, the rowView is initialized, just get the viewHolder and then get the views from it, to update:
        //myCustomAdapterViewHolder l_oViewHolder = (myCustomAdapterViewHolder) l_oRowView.getTag();
        /*l_oInitializedViewHolder.eventName.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                mCallBack.onEventItemSelected((String)getListAdapter().getItem(l_nPosition));

            }
        });*/
        l_oInitializedViewHolder.eventName.setText(m_oValues.get(position));
        return l_oRowView;
        //return super.getView();
    }

    public myCustomAdapter(Context cont, int viewResId, String[] strings) 
    {
        this(cont, viewResId, Arrays.asList(strings));
    }

    @Override
    public String getItem(int position) 
    {
        return sortedList.get(position);
    }

    @Override
    public int getPosition(String item) 
    {
        return sortedList.indexOf(item);
    }

}

我在这里做错了什么? 我想要的只是能够选择“文件”进行删除,使用CheckBoxes

【问题讨论】:

  • 据我所知,ListView 中的 CheckBox 不能正常工作,所以我有一个更好的 MultichoiceItem 解决方案。如果你愿意,我可以发布这个..
  • 试试这是最好的答案stackoverflow.com/questions/18162931/…

标签: android checkbox textview android-listfragment


【解决方案1】:

尝试只对 textview 和 checkbox 使用 onclick,而不是 onListItemClick - 你也可以删除它,所以你也应该更改线性布局的一些属性。

<?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"
    android:clickable="false"
    android:focusable="false"
    android:gravity="left"
    android:orientation="horizontal" >
<TextView
    android:id="@+id/textview_event_name"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:paddingLeft="3dp"
    android:paddingTop="5dp"
    android:focusable="true"
    android:singleLine="false" />

<CheckBox
    android:id="@+id/checkbox_for_event_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0.05"
    android:focusable="false"
    android:clickable="false" />

</LinearLayout>

在适配器中实现

    // PlaceHolder pattern:
        final myCustomAdapterViewHolder l_oViewHolder = new myCustomAdapterViewHolder();
        l_oViewHolder.eventName = (TextView) l_oRowView.findViewById(R.id.textview_event_name);


         l_oViewHolder.eventCheckBox = (CheckBox) l_oRowView.findViewById(R.id.checkbox_for_event_name);

  l_oViewHolder.eventName.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                     // your code for textview click
               }
           }
            l_oViewHolder.eventCheckBox.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    CheckBox cb = (CheckBox)v;
                    //cb.setChecked(!cb.isChecked());

                    String l_sFilename = l_oViewHolder.eventName.getText().toString();
                    if (cb.isChecked())
                    {
                        if (!m_lstSelectedFilenames.contains(l_sFilename))
                            m_lstSelectedFilenames.add(l_sFilename);
                    }
                    else
                    {
                        if (m_lstSelectedFilenames.contains(l_sFilename))
                            m_lstSelectedFilenames.remove(l_sFilename);
                    }
                }
            });
            l_oViewHolder.eventCheckBox.setFocusable(false);

【讨论】:

  • 好吧,似乎更改 XML 就足够了,布局应该是不可聚焦和不可点击的,现在 onListItemClick 被调用,我得到了正确的 GUI 反馈。所以它解决了我的问题,无需使用l_oViewHolder.eventName.setOnClickListener,这在我看来有点奇怪(也许这是正确的方法)。谢谢
  • 非常有用!这只是在 textview 上使用 setOnClickListener 的另一种方法。如果没有这个也能正常工作,那就太好了。就我个人而言,我也更喜欢在列表视图项目上使用 onClick,但只是想给你一个替代方案:)。
  • 正如您在我之前尝试使用它的代码中看到的(已注释),但单击项目时没有 GUI 反馈。可能是因为 XML 设置不符合我的需求。
【解决方案2】:

添加以下属性:

  android:focusable="false"
  android:clickable="false"

例如在我的 xml 文件中:

<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeightSmall"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:button="?android:attr/listChoiceIndicatorSingle"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:buttonTint="@color/gray"
android:focusable="false"
android:clickable="false"
/>

在我的代码中:

public class MyTestFragment extends ListFragment {
   .....
   @Override
   public void onListItemClick(ListView l, View v, int position, long id) {

   }
}

【讨论】:

    猜你喜欢
    • 2011-11-08
    • 1970-01-01
    • 2018-08-08
    • 2012-10-27
    • 2013-10-12
    • 2012-03-27
    • 2013-08-29
    • 2013-11-03
    • 1970-01-01
    相关资源
    最近更新 更多