草庐IT

android - 自定义列表项不响应选择器中的 state_checked

coder 2023-11-19 原文

首先我要说我已经详细阅读了关于 SO 的几乎所有问题,我可以找到与自定义可检查列表项和选择器相关的问题。他们中的许多人都有类似的问题,但没有一个答案能解决我的问题。

在我的应用中,我展示了一个自定义列表 Activity 。创建后,它会从调用它的 Intent 中检索一组静态数据,并将该数据传递给它的自定义数组适配器。每个列表项都是一个简单的 RelativeLayout 实现了 Checkable界面。默认情况下,如果您单击其中一项,则会显示一个新 Activity ,其中显示有关所选联系人的详细信息。但是,如果长按列表中的项目,则会启动 ActionMode。此时单击列表中的项目不会显示详细信息 Activity ,它只是将项目设置为已选中。然后,如果用户选择了其中一项操作模式项,它将对选中的项执行操作。

需要了解的重要一点是,在两种选择“模式”中,单击列表项会将其设置为选中状态。

我上面描述的所有内容都完美无缺。我的唯一问题与列表项的背景有关,当它们被设置为选中时,即使使用默认选择器也没有突出显示。

我想做的是有两个选择器:每个选择器一个。在第一种情况下,选中项目时背景不会改变,而在第二种情况下会改变。我试过实现自定义选择器,但即使在那些 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>

双线阵列适配器:

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>

最佳答案

我在 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 };

问题似乎在于,在单击时,您从未处理过切换 View 的选中状态。

关于android - 自定义列表项不响应选择器中的 state_checked,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13627000/

有关android - 自定义列表项不响应选择器中的 state_checked的更多相关文章

  1. ruby - Facter::Util::Uptime:Module 的未定义方法 get_uptime (NoMethodError) - 2

    我正在尝试设置一个puppet节点,但ruby​​gems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由ruby​​gems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby

  2. ruby-on-rails - Rails 3.2.1 中 ActionMailer 中的未定义方法 'default_content_type=' - 2

    我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer

  3. ruby-on-rails - form_for 中不在模型中的自定义字段 - 2

    我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢

  4. ruby - 主要 :Object when running build from sublime 的未定义方法 `require_relative' - 2

    我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby​​1.9+ 关于ruby-主要:Objectwhenrun

  5. ruby - RVM 使用列表[0] - 2

    是否有类似“RVMuse1”或“RVMuselist[0]”之类的内容而不是键入整个版本号。在任何时候,我们都会看到一个可能包含5个或更多ruby的列表,我们可以轻松地键入一个数字而不是X.X.X。这也有助于rvmgemset。 最佳答案 这在RVM2.0中是可能的=>https://docs.google.com/document/d/1xW9GeEpLOWPcddDg_hOPvK4oeLxJmU3Q5FiCNT7nTAc/edit?usp=sharing-知道链接的任何人都可以发表评论

  6. ruby - 在 Ruby 中有条件地定义函数 - 2

    我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin

  7. ruby - 定义方法参数的条件 - 2

    我有一个只接受一个参数的方法:defmy_method(number)end如果使用number调用方法,我该如何引发错误??通常,我如何定义方法参数的条件?比如我想在调用的时候报错:my_method(1) 最佳答案 您可以添加guard在函数的开头,如果参数无效则引发异常。例如:defmy_method(number)failArgumentError,"Inputshouldbegreaterthanorequalto2"ifnumbereputse.messageend#=>Inputshouldbegreaterthano

  8. ruby-on-rails - 每次我尝试部署时,我都会得到 - (gcloud.preview.app.deploy) 错误响应 : [4] DEADLINE_EXCEEDED - 2

    我是Google云的新手,我正在尝试对其进行首次部署。我的第一个部署是RubyonRails项目。我基本上是在关注thisguideinthegoogleclouddocumentation.唯一的区别是我使用的是我自己的项目,而不是他们提供的“helloworld”项目。这是我的app.yaml文件runtime:customvm:trueentrypoint:bundleexecrackup-p8080-Eproductionconfig.ruresources:cpu:0.5memory_gb:1.3disk_size_gb:10当我转到我的项目目录并运行gcloudprevie

  9. ruby - 如何在 Grape 中定义哈希数组? - 2

    我使用Ember作为我的前端和GrapeAPI来为我的API提供服务。前端发送类似:{"service"=>{"name"=>"Name","duration"=>"30","user"=>nil,"organization"=>"org","category"=>nil,"description"=>"description","disabled"=>true,"color"=>nil,"availabilities"=>[{"day"=>"Saturday","enabled"=>false,"timeSlots"=>[{"startAt"=>"09:00AM","endAt"=>

  10. ruby - 获取模块中定义的所有常量的值 - 2

    我想获取模块中定义的所有常量的值:moduleLettersA='apple'.freezeB='boy'.freezeendconstants给了我常量的名字:Letters.constants(false)#=>[:A,:B]如何获取它们的值的数组,即["apple","boy"]? 最佳答案 为了做到这一点,请使用mapLetters.constants(false).map&Letters.method(:const_get)这将返回["a","b"]第二种方式:Letters.constants(false).map{|c

随机推荐