【问题标题】:highlighting selected item in custom list view using context menu?使用上下文菜单突出显示自定义列表视图中的选定项目?
【发布时间】:2015-03-11 00:48:44
【问题描述】:

我一直在尝试在此列表视图上设置我的背景颜色,并且遵循多个教程它只是没有出现,但没有显示错误。

我在 values 文件夹中有一个名为 colors 的 xml 文件:

<resources>
    <drawable name="red_colour">#6D0E0E</drawable>
</resources>

哪些链接到drawable文件夹中的xml:

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

</selector>

我试过放置以下代码 android:background:@id"colourhighlight.xml" 几乎到处都是基于各种教程但没有运气?

有什么建议吗?请帮忙...

我有一个带有上下文菜单的主类:

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_contacts);

        // The contacts from the contacts content provider is stored in this cursor
        mMatrixCursor = new MatrixCursor(new String[] { "_id","name","photo","details"} );

        // Adapter to set data in the listview
        mAdapter = new SimpleCursorAdapter(getBaseContext(),
                R.layout.lv_layout,
                null,
                new String[] { "name","photo","details"},
                new int[] { R.id.tv_name,R.id.iv_photo,R.id.tv_details}, 0);

        // Getting reference to listview
        final ListView lstContacts = (ListView) findViewById(R.id.lst_contacts);

        // Setting the adapter to listview
        lstContacts.setAdapter(mAdapter);

        // Creating an AsyncTask object to retrieve and load listview with contacts
        ListViewContactsLoader listViewContactsLoader = new ListViewContactsLoader();

        // Starting the AsyncTask process to retrieve and load listview with contacts
        listViewContactsLoader.execute();

        //Selecting and highlighting the elements in the listview
        lstContacts.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                view.setActivated(true);
            }
        });

        //Creating the context menu and the options for it
        listview = (ListView) findViewById(R.id.lst_contacts);
        listview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
        listview.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
            @Override
            public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
                count = count+1;
                mode.setTitle(count + " Contacts Selected");


            }

            @Override
            public boolean onCreateActionMode(ActionMode mode, Menu menu) {

                MenuInflater inflater = mode.getMenuInflater();
                inflater.inflate(R.menu.contact_context_menu, menu);

                return true;
            }

            @Override
            public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                return false;
            }

            @Override
            public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                switch(item.getItemId()){
                    case R.id.delete_id:
                        Toast.makeText(getBaseContext(), count + " Contacts Deselected", Toast.LENGTH_SHORT).show();
                        count = 0;
                        mode.finish();
                        return true;
                        //break;
                    default:
                       return false; //break;
                }

                //return false;
            }

            @Override
            public void onDestroyActionMode(ActionMode mode) {

            }
        });

    }

lv_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
   >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/iv_photo"
        android:maxHeight="80dp"
        android:maxWidth="80dp"
        android:adjustViewBounds="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:id="@+id/tv_name"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/iv_photo"
        android:layout_toEndOf="@+id/iv_photo" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:id="@+id/tv_details"
        android:layout_below="@+id/tv_name"
        android:layout_toRightOf="@+id/iv_photo"
        android:layout_toEndOf="@+id/iv_photo" />
</RelativeLayout>

主布局.xml

<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.example.ankhit.saveme.UserContacts"
    android:background="@drawable/colourhighlight"
    >

    <ListView
        android:id="@+id/lst_contacts"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/instructions"
        android:id="@+id/button"
        android:onClick="showInstructions"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

【问题讨论】:

    标签: android xml android-layout listview contextmenu


    【解决方案1】:
    @Override
    public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) 
    {
        try 
        {
            final int checkedCount = listView.getCheckedItemCount();
            mode.setTitle(""+checkedCount);
    
            if (checked)
            {
                adapter.setNewSelection(position, checked);                    
            }
            else 
            {
                adapter.removeSelection(position);                 
            }
            adapter.notifyDataSetChanged();
        }
        catch (Exception e) 
        {
            e.printStackTrace();
        }
    }
    

    在你的适配器类中添加这些方法

    private HashMap<Integer, Boolean> mSelection = new HashMap<Integer, Boolean>();
    public void setNewSelection(int position, boolean value) 
    {
        mSelection.put(position, value);
    }
    
    public boolean isPositionChecked(int position) 
    {
        Boolean result = mSelection.get(position);
        return result == null ? false : result;
    }
    
    public Set<Integer> getCurrentCheckedPosition() {
        return mSelection.keySet();
    }
    
    public void removeSelection(int position) {
        mSelection.remove(position);
    }
    
    public void clearSelection() {
        mSelection = new HashMap<Integer, Boolean>();
    }
    

    在适配器的getView()方法中调用这个方法它可以正常工作

    convertView.setBackgroundColor(mContext.getResources().getColor(android.R.color.transparent));
    if (mSelection.get(position) != null) 
    {
        convertView.setBackgroundColor(mContext.getResources().getColor(R.color.message_selector_holo_blue));
                    }
    

    【讨论】:

    • @Rakesh ...您能否指定适配器类(在异步类中)?还应该在我的 setOnItemClick 侦听器中调用 convertview?
    • 想访问列表视图行吗??
    • 我找到了解决方法:你能帮我解决这个问题吗:stackoverflow.com/questions/29021329/…
    【解决方案2】:

    如果您想完全突出显示选定的行,请更改转换视图的背景颜色

    lstContacts.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                view.setBackgroundColor(mContext.getResources().getColor(android.R.color.transparent));
    
    
            }
        });
    

    【讨论】:

    • @Rakesh...这不起作用...无法识别 mContext 所以我更改了它 view.getResources...但是,当我长按以获取上下文栏并尝试选择颜色时不留也不出现。
    • 删除 mContext 并编写 getResources() 方法,它才会成功......
    • 所以它有效但不完全。我的上下文菜单在长按上,然后它计算我选择的数量,但颜色没有出现在那里......但是,当我只是正常选择而不长按它时,它会突出显示项目,但每隔几个就会突出显示在列表中我假设这是因为列表正在被回收?无论如何要保持我在长按上按字面意思按下的项目的颜色?
    猜你喜欢
    • 2013-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-22
    • 1970-01-01
    • 1970-01-01
    • 2013-09-04
    相关资源
    最近更新 更多