自定义列表项在选择器中不响应state_checked状态

13
我要先说一下,我已经详细阅读了几乎所有与自定义可选列表项和选择器相关的SO问题。其中许多问题存在类似的问题,但没有一个答案能解决我的问题。
在我的应用程序中的某个点上,我呈现一个自定义列表活动。创建时,它从调用它的意图中检索一组静态数据,并将该数据传递给其自定义数组适配器。每个列表项都是一个简单的RelativeLayout,实现了Checkable接口。默认情况下,如果您单击其中一个项目,则会显示一个新的活动,其中显示所选联系人的详细信息。但是,如果长时间单击列表中的项目,则会启动ActionMode。此时,单击列表中的项目不会显示详细活动,而只是将项目设置为已选中。然后,如果用户选择其中一个操作模式项,则对选中的项目执行该操作。
重要的是要理解,在两种选择“模式”中,单击列表项都会将其设置为已选中。
我上述描述的所有内容都完美运行。 我唯一的问题是,当将列表项设置为已选中时,它们的背景不会高亮显示,即使使用默认选择器也是如此。
我想做的是有两个选择器:每种选择模式一个。 在第一个选择器中,当选中项目时,背景不会改变,在第二个选择器中,背景会改变。 我尝试实现自定义选择器,但即使在这些选择器中,state_checked也被忽略! 选择器的其他部分工作正常,但state_checked不起作用。
我的CheckableListItem实现结合了许多不同示例的想法,因此如果我做错了什么,或者有更好的方法,请告诉我!
注意:有趣的一点是,如果我将results_list_item.xml中列表项的背景设置为我的选择器,而不是ListView的listSelector属性,则当选中项目时,背景会更改。 但是,这样做会导致我的选择器中的长按转换不起作用。

ResultsActivity.java:

public class ResultsActivity extends ListActivity implements OnItemLongClickListener {

    private ListView listView;          // Reference to the list belonging to this activity
    private ActionMode mActionMode;     // Reference to the action mode that can be started
    private boolean selectionMode;      // Detail mode or check mode

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_results);

        // When the home icon is pressed, go back
        ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);

        // Get a reference to the list
        listView = getListView();

        // Initially in detail mode
        selectionMode = true;

        // Get the contacts from the intent data and pass them to the contact adapter
        @SuppressWarnings("unchecked")
        ArrayList<Contact> results = ((ArrayList<Contact>)getIntent().getSerializableExtra("results"));
        Contact[] contacts = new Contact[results.size()];
        ContactArrayAdapter adapter = new ContactArrayAdapter(this, results.toArray(contacts));
        setListAdapter(adapter);

        // We will decide what happens when an item is long-clicked
        listView.setOnItemLongClickListener(this);
    }

    /**
     * If we are in detail mode, when an item in the list is clicked
     * create an instance of the detail activity and pass it the
     * chosen contact
     */
    public void onListItemClick(ListView l, View v, int position, long id) {
        if (selectionMode) {
            Intent displayContact = new Intent(this, ContactActivity.class);
            displayContact.putExtra("contact", (Contact)l.getAdapter().getItem(position));
            startActivity(displayContact);
        }
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        return super.onCreateOptionsMenu(menu);
    }

    /**
     * If the home button is pressed, go back to the
     * search activity
     */
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                Intent intent = new Intent(this, SearchActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    /**
     * When an item is long-pressed, switch selection modes 
     * and start the action mode 
     */
    public boolean onItemLongClick(AdapterView<?> adapter, View view, int position, long i) {
        if (mActionMode != null)
            return false;

        if (selectionMode) {
            toggleSelectionMode();
            listView.startActionMode(new ListActionMode(this, getListView()));
            return true;
        }
        return false;
    }

    /**
     * Clear the list's checked items and switch selection modes
     */
    public void toggleSelectionMode() {
        listView.clearChoices();
        ((ContactArrayAdapter)listView.getAdapter()).notifyDataSetChanged();
        if (selectionMode) {
            selectionMode = false;
        } else {
            selectionMode = true;
        }
    }
}

activity_results.xml:

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:choiceMode="multipleChoice" 
    android:listSelector="@drawable/list_selector" />

list_selector.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true" android:drawable="@drawable/blue_transition" />
    <item android:state_checked="true" android:drawable="@drawable/blue" />
</selector>

TwoLineArrayAdapter:


public abstract class TwoLineArrayAdapter extends ArrayAdapter<Contact> {

    private int mListItemLayoutResId;

    public TwoLineArrayAdapter(Context context, Contact[] results) {
        this(context, R.layout.results_list_item, results);
    }

    public TwoLineArrayAdapter(Context context, int listItemLayoutResourceId, Contact[] results) {
        super(context, listItemLayoutResourceId, results);
        mListItemLayoutResId = listItemLayoutResourceId;
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View listItemView = convertView;
        if (convertView == null) {
            listItemView = inflater.inflate(mListItemLayoutResId, parent, false);
        }

        // Get the text views within the layout
        TextView lineOneView = (TextView)listItemView.findViewById(R.id.results_list_item_textview1);
        TextView lineTwoView = (TextView)listItemView.findViewById(R.id.results_list_item_textview2);

        Contact c = (Contact)getItem(position);

        lineOneView.setText(lineOneText(c));
        lineTwoView.setText(lineTwoText(c));

        return listItemView;
    }

    public abstract String lineOneText(Contact c);

    public abstract String lineTwoText(Contact c);

}

ContactArrayAdapter

public class ContactArrayAdapter extends TwoLineArrayAdapter {

    public ContactArrayAdapter(Context context, Contact[] contacts) {
        super(context, contacts);
    }

    public String lineOneText(Contact c) {
        return (c.getLastName() + ", " + c.getFirstName());
    }

    public String lineTwoText(Contact c) {
        return c.getDepartment();
    }

}

CheckableListItem.java:

public class CheckableListItem extends RelativeLayout implements Checkable {

    private boolean isChecked;
    private List<Checkable> checkableViews;

    public CheckableListItem(Context context, AttributeSet attrs,
            int defStyle) {
        super(context, attrs, defStyle);
        initialise(attrs);
    }

    public CheckableListItem(Context context, AttributeSet attrs) {
        super(context, attrs);
        initialise(attrs);
    }

    public CheckableListItem(Context context, int checkableId) {
        super(context);
        initialise(null);
    }

    private void initialise(AttributeSet attrs) {
        this.isChecked = false;
        this.checkableViews = new ArrayList<Checkable>(5);
    }

    public boolean isChecked() {
        return isChecked;
    }

    public void setChecked(boolean check) {
        isChecked = check;
        for (Checkable c : checkableViews) {
            c.setChecked(check);
        }
        refreshDrawableState();
    }

    public void toggle() {
        isChecked = !isChecked;
        for (Checkable c : checkableViews) {
            c.toggle();
        }
    }

    private static final int[] CheckedStateSet = {
        android.R.attr.state_checked
    };

    protected int[] onCreateDrawableState(int extraSpace) {
        final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
        if (isChecked()) {
            mergeDrawableStates(drawableState, CheckedStateSet);
        }
        return drawableState;
    }

    protected void onFinishInflate() {
        super.onFinishInflate();
        final int childCount = this.getChildCount();
        for (int i = 0; i < childCount; i++) {
            findCheckableChildren(this.getChildAt(i));
        }
    }

    private void findCheckableChildren(View v) {
        if (v instanceof Checkable) {
            this.checkableViews.add((Checkable) v);
        }
        if (v instanceof ViewGroup) {
            final ViewGroup vg = (ViewGroup) v;
            final int childCount = vg.getChildCount();
            for (int i = 0; i < childCount; i++) {
                findCheckableChildren(vg.getChildAt(i));
            }
        }
    }
}

results_list_item.xml:

<com.test.mycompany.Widgets.CheckableListItem
    android:id="@+id/results_list_item"
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="5dp"
    android:paddingBottom="5dp" >

    <TextView android:id="@+id/results_list_item_textview1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:textSize="20sp"
        android:textColor="#000000"
        android:focusable="false" />

    <TextView android:id="@+id/results_list_item_textview2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@id/results_list_item_textview1"
        android:textSize="16sp"
        android:textColor="@android:color/darker_gray" 
        android:focusable="false" />

</com.test.mycompany.Widgets.CheckableListItem>

你在哪里勾选和取消勾选你的项目?你也可以尝试使用android:state_activited。 - DroidBender
当您直接设置背景时,请在CheckableListItem上尝试使用android:longClickable=true(如您的注释中所指定的)。 - Ron
'R.attr.state_checked' 是引用你自己的 'R' 类(即生成的类),还是引用 Android API 中的类? - ben75
OP,你能告诉我标签“css-selectors”在这里的相关性吗?据我所知,Android与CSS无关,因为它只是基于Java的,我看不出这个标签在哪里有意义。 - Clemens Himmer
@Tarekis 那一定是个打字错误。 - Groppe
显示剩余3条评论
2个回答

0

我在CheckedListItem中进行了修改和添加这些方法,并且对我来说它是有效的:

@Override
public boolean onTouchEvent( MotionEvent event ) {

    int action = event.getAction() & MotionEvent.ACTION_MASK;
    if ( action == MotionEvent.ACTION_UP ) {
        toggle();
    }

    return true;
}

public void toggle() {

    setChecked( !isChecked() );
}

private static final int[] CheckedStateSet = { android.R.attr.state_checked };

似乎问题在于您从未处理切换视图的选中状态。

现在对我来说不起作用了...当我点击一个项目时什么也不会发生。这些是你改变的唯一的东西吗?你删除了其他方法中的任何内容吗? - Groppe
不,我没有,但我也没有把它放在ListView中。你在ListView上有一个监听器来接收所有的点击事件,所以你的视图将永远不会接收到触摸事件。我的建议是将选择模式设置为CHOICE_MODE_SINGLECHOICE_MODE_MULTIPLE(根据你的需要),这将使列表项可选。然后你可以在你的CheckedListItem类中使用state_selected而不是state_checked - Jason Robinson

0
如果问题还没有解决,可以给你的“可选择布局”添加一个带有状态和颜色定义的选择器背景drawable。否则,因为mBackground为空,drawableStateChanged将不会起作用。(比如:android:background="@drawable/list_selector")
然后确保使用listview.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE)来允许选择多个项目。
你不必实现setOnItemClickListener来检查项目,因为设置选择模式已经自动完成了。(也不要将可选择布局设置为可点击)
好吧,至少这是我解决我的问题的方法。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接