带有复选框 onListItemClick 的 ListFragment 永远不会被调用

ListFragment with checkbox onListItemClick never gets called

我已经阅读了一些诸如 this and this 之类的问题,但仍然无法弄清楚我的代码有什么问题。 我有一个 ListFragment,每一行都有一个 TextView 和一个 CheckBox。 单击 CheckBox 有效,但单击 TextView 无效,并且 OnListItemClick 不会被调用。我也试过动态添加一个 OnClickListener,这使它工作,但这不是正确的方法,而且它也缺少点击的 GUI 反馈(项目是 "highlighted"秒)。

这是我的 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);
    }

}

我在这里做错了什么? 我想要的只是能够 select "files" 进行删除,使用 CheckBoxes

尝试只对 textview 和复选框使用 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);

添加以下属性:

  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) {

   }
}