高亮所选项目使用上下文菜单自定义列表视图? [英] highlighting selected item in custom list view using context menu?

查看:175
本文介绍了高亮所选项目使用上下文菜单自定义列表视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图设置该列表视图和多之后我的教程背景颜色,它只是不上来,还没有显示任何错误。

我已经得到了被称为XML文件的色彩中的值文件夹:

 <资源>
    <绘制NAME =red_colour>#6D0E0E< /绘>
< /资源>

其中在绘制文件夹链接到一个xml:

 <选择的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
<项目
    机器人:STATE_ pressed =真
    机器人:state_activated =真
    机器人:可绘制=@绘制/ red_colour>
< /项目>< /选择>

我试过把下面的code的android:背景:@idcolourhighlight.xml
几乎无处不在基于各种教程还没有运气?

有何建议?请帮助...

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

  @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_user_contacts);        //从联系人内容提供商的联系人存储在此光标
        mMatrixCursor =新MatrixCursor(新的String [] {_id,姓名,图片,详细信息});        //适配器ListView中设定的数据
        mAdapter =新SimpleCursorAdapter(getBaseContext()
                R.layout.lv_layout,
                空值,
                新的String [] {姓名,图片,详细信息},
                新的INT [] {R.id.tv_name,R.id.iv_photo,R.id.tv_details},0);        //获取参考列表视图
        最终的ListView lstContacts =(ListView控件)findViewById(R.id.lst_contacts);        //设置适配器列表视图
        lstContacts.setAdapter(mAdapter);        //创建的AsyncTask对象检索和负载的ListView与联系人
        ListViewContactsLoader listViewContactsLoader =新ListViewContactsLoader();        //启动AsyncTask的过程来检索和负载的ListView与联系人
        listViewContactsLoader.execute();        //选择和突出Lis​​tView中的元素
        lstContacts.setOnItemClickListener(新AdapterView.OnItemClickListener(){
            @覆盖
            公共无效onItemClick(适配器视图<>母公司,观景,INT位置,长的id){
                view.setActivated(真);
            }
        });        //创建上下文菜单,它的选项
        列表视图=(ListView控件)findViewById(R.id.lst_contacts);
        listview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
        listview.setMultiChoiceModeListener(新AbsListView.MultiChoiceModeListener(){
            @覆盖
            公共无效onItemCheckedStateChanged(ActionMode模式,INT位置,长的ID,布尔检查){
                数=计+ 1;
                mode.setTitle(数+选择联系人);
            }            @覆盖
            公共布尔onCreateActionMode(ActionMode模式,菜单菜单){                MenuInflater吹气= mode.getMenuInflater();
                inflater.inflate(R.menu.contact_context_menu,菜单);                返回true;
            }            @覆盖
            在prepareActionMode公共布尔(ActionMode模式,菜单菜单){
                返回false;
            }            @覆盖
            公共布尔onActionItemClicked(ActionMode模式,菜单项项){
                开关(item.getItemId()){
                    案例R.id.delete_id:
                        Toast.makeText(getBaseContext(),计数+取消选择联系人,Toast.LENGTH_SHORT).show();
                        计数= 0;
                        mode.finish();
                        返回true;
                        //打破;
                    默认:
                       返回false; //打破;
                }                //返回false;
            }            @覆盖
            公共无效onDestroyActionMode(ActionMode模式){            }
        });    }

lv_layout.xml

 <?XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent机器人:layout_height =match_parent
   >    < ImageView的
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:ID =@ + ID / iv_photo
        安卓了maxHeight =80dp
        安卓了maxWidth =80dp
        机器人:adjustViewBounds =真
        机器人:layout_alignParentTop =真
        机器人:layout_alignParentLeft =真
        机器人:layout_alignParentStart =真/>    <的TextView
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:textAppearance =机器人:ATTR / textAppearanceLarge
        机器人:ID =@ + ID / tv_name
        机器人:layout_alignParentTop =真
        机器人:layout_toRightOf =@ + ID / iv_photo
        机器人:layout_toEndOf =@ + ID / iv_photo/>    <的TextView
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:textAppearance =机器人:ATTR / textAppearanceSmall
        机器人:ID =@ + ID / tv_details
        机器人:layout_below =@ + ID / tv_name
        机器人:layout_toRightOf =@ + ID / iv_photo
        机器人:layout_toEndOf =@ + ID / iv_photo/>
< / RelativeLayout的>

主要layout.xml

 <的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s的android:layout_width =match_parent
    机器人:layout_height =match_parent机器人:paddingLeft =@扪/ activity_horizo​​ntal_margin
    机器人:paddingRight =@扪/ activity_horizo​​ntal_margin
    机器人:paddingTop =@扪/ activity_vertical_margin
    机器人:paddingBottom会=@扪/ activity_vertical_margin
    工具:上下文=com.example.ankhit.saveme.UserContacts
    机器人:背景=@绘制/ colourhighlight
    >    < ListView控件
        机器人:ID =@ + ID / lst_contacts
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_above =@ + ID /按钮
        />    <按钮
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=@字符串/指令
        机器人:ID =@ + ID /按钮
        安卓的onClick =showInstructions
        机器人:layout_alignParentBottom =真
        机器人:layout_centerHorizo​​ntal =真/>< / RelativeLayout的>


解决方案

  @覆盖
公共无效onItemCheckedStateChanged(ActionMode模式,INT位置,长的ID,布尔检查)
{
    尝试
    {
        最终诠释checkedCount = listView.getCheckedItemCount();
        mode.setTitle(+ checkedCount);        如果(选中)
        {
            adapter.setNewSelection(位置,选中);
        }
        其他
        {
            adapter.removeSelection(位置);
        }
        adapter.notifyDataSetChanged();
    }
    赶上(例外五)
    {
        e.printStackTrace();
    }
}

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

 私人的HashMap<整数,布尔> mSelection =新的HashMap<整数,布尔>();
公共无效setNewSelection(INT位置,布尔值)
{
    mSelection.put(位置,值);
}公共布尔isPositionChecked(INT位置)
{
    布尔结果= mSelection.get(位置);
    返回结果== NULL?假:结果;
}公开组<整数GT; getCurrentCheckedPosition(){
    返回mSelection.keySet();
}公共无效removeSelection(INT位置){
    mSelection.remove(位置);
}公共无效clearSelection(){
    mSelection =新的HashMap<整数,布尔>();
}

里面调用这个方法 getView()适配器的方法,它正常工作

<$p$p><$c$c>convertView.setBackgroundColor(mContext.getResources().getColor(android.R.color.transparent));
如果(mSelection.get(位置)!= NULL)
{
    convertView.setBackgroundColor(mContext.getResources().getColor(R.color.message_selector_holo_blue));
                }

I've been trying to set my background colour on this list view and with following multiple tutorials it just doesn't come up, yet no errors are shown.

What I've got is an xml file called colours in the values folder:

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

Which links to an xml in the drawable folder:

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

I've tried placing the following code android:background:@id"colourhighlight.xml" literally everywhere based on various tutorials yet no luck?

Any advice? Please help...

I've got one main class with a context menu:

 @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>

Main layout.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>

解决方案

@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();
    }
}

add these methods in your adapter class

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>();
}

call this method inside getView() method of adapter it works properly

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));
                }

这篇关于高亮所选项目使用上下文菜单自定义列表视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆