如何突出在Android的行中的ListView? [英] How to highlight row in ListView in Android?

查看:111
本文介绍了如何突出在Android的行中的ListView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要强调的一排的ListView 被选择(对他选择的用户),因此,它不是一个要被选择,这是一个他之前选择了。

我已经有通过的位置:

  ListView.setSelection(位置);
 

现在,我要的是选择这个特定的行,使其突出显示。

的onCreate()中包含的ListView 活动的code:

 公共类CountryView扩展活动
{
    受保护的静态最后弦乐LOG_TAG = NULL;
    / **第一次创建活动时调用。 * /
    的String [] lv_arr = {};

    ListAdapter适配器;
    TextView的吨;
    私人的ListView lvUsers;
    私人的ArrayList< COUN> mListUsers;
    字符串的反响= NULL;
    公众诠释D组;
    INT selectedListItem = -1;


    @覆盖
    公共无效的onCreate(包savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.country);

        意图数据= getIntent();

        mListUsers = getCoun();
        lvUsers =(ListView控件)findViewById(R.id.counlistView);


        lvUsers.setAdapter(新ListAdapter(这一点,R.id.counlistView,mListUsers));


        selectedListItem = data.getExtras()调用getInt(PositionInList);

       lvUsers.setChoiceMode(ListView.CHOICE_MODE_SINGLE);



        lvUsers.setOnItemClickListener(新OnItemClickListener()
        {

            INT positionItem;

            公共无效onItemClick(适配器视图<>母公司视图中查看,INT位置,长ID)
            {
                意图pongIntent =新的意图(getApplicationContext(),Trav.class);

                INT counId = mListUsers.get(位置).ID;

                pongIntent.putExtra(回应,mListUsers.get(位置).P);
                pongIntent.putExtra(responseCounID,counId);

                //把里面多余的选择列表中的位置
                positionItem =位置;
                pongIntent.putExtra(PositionInListSet,positionItem);

                的setResult(Activity.RESULT_OK,pongIntent);

                Log.i(CounID *********,+ counId);
                完();
            }
         });
    }
}
 

解决方案

由于默认情况下列表视图设置为 NONE的选择模式,在触摸模式 setSelection 方法不会有视觉效果。

有关保持previous选择/直观地显示一个明确的选择,首先必须正确设置你的列表视图的选择方式:

  listview.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
 

这是有用的阅读这些方法的API文档:

  • setSelection

 无效android.widget.AdapterView.setSelection(INT位置)
 

设定当前选择的项目。至   支持辅助功能的子类   覆盖此方法必须调用   被覆盖的超级方法第一。

     

参数
  位置数据项目的索引(从0开始)被选中。

  • setChoiceMode

 无效android.widget.ListView.setChoiceMode(INT choiceMode)
 

     

定义选择行为的   列表。缺省情况下,解释不必   任何选择行为   ( CHOICE_MODE_NONE )。通过设置   choiceMode到 CHOICE_MODE_SINGLE 中,   列表允许最多一个项目是在一个   选择的状态。通过设置   choiceMode到 CHOICE_MODE_MULTIPLE ,   列表中允许任何数量的项目,以   被选择

     

参数
choiceMode 一个 CHOICE_MODE_NONE    CHOICE_MODE_SINGLE CHOICE_MODE_MULTIPLE

在情况下,这是不够的(说你想始终显示最后选择不同的当前选择的旁边),你应该保存你的最后选择的项目(其中填充数据的 ListAdapter )的 lastSelectedItem ,并在您的适配器 getView 方法分配不同的背景资源的渲染器,如果它等于该 lastSelectedItem

如果你最后的选择就不会选择更改刷新,你应该明确地调用你的适配器实例的 notifyDataSetChanged 方法。

更新
由于包含的ListView 的活动是等待这一个结果的活动的孩子(根据的setResult(Activity.RESULT_OK,pongIntent); 部分),最初的想法是正确的,最后的位置,应通过意图开始活动时,通过了:

  selectedListItem = getIntent()getIntExtra(PositionInList,-1)。
lvUsers.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
lvUsers.setSelection(selectedListItem);
 

如果你保持相同的活性的 ListView.CHOICE_MODE_SINGLE 解决方案的工作,但要在每一个itemClick在(选择城市)完成它,这就是为什么额外的数据应被传递到起始意图

您还可以设置pviously所选项目的背景,从您的适配器$ P $ -As提到above-,覆盖了 getView 方法:

  lvUsers.setAdapter(新ArrayAdapter(这一点,R.id.counlistView,组)
{
    @覆盖
    公共查看getView(INT位置,查看convertView,父母的ViewGroup)
    {
        最终视图渲染= super.getView(位置,convertView,父母);
        如果(位置== selectedListItem)
        {
            // TODO:设置正确的选择的颜色在这里:
            renderer.setBackgroundResource(android.R.color.darker_gray);
        }
        返回渲染器;
    }
});
 

I need to highlight a row in a ListView that was selected (to show the user what he chose), so, it's not the one that is going to be chosen, it's the one he chose before.

I already have the location by:

ListView.setSelection(position);

And now what I want is to select this specific row and to highlight it.

The code of the onCreate() in the activity that contains the ListView:

public class CountryView extends Activity
{
    protected static final String LOG_TAG = null;
    /** Called when the activity is first created. */
    String[] lv_arr = {};

    ListAdapter adapter;
    TextView t;
    private ListView lvUsers;
    private ArrayList<Coun> mListUsers;
    String responce=null;
    public int d;
    int selectedListItem = -1;


    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.country);

        Intent data =getIntent();

        mListUsers = getCoun();
        lvUsers = (ListView) findViewById(R.id.counlistView);


        lvUsers.setAdapter(new ListAdapter(this, R.id.counlistView, mListUsers)); 


        selectedListItem=data.getExtras().getInt("PositionInList");

       lvUsers.setChoiceMode(ListView.CHOICE_MODE_SINGLE);



        lvUsers.setOnItemClickListener(new OnItemClickListener()
        {

            int positionItem;

            public void onItemClick(AdapterView<?> parent, View view,int position, long id)
            {
                Intent pongIntent = new Intent(getApplicationContext(),Trav.class);

                int counId=mListUsers.get(position).id;

                pongIntent.putExtra("response",mListUsers.get(position).p);
                pongIntent.putExtra("responseCounID",counId);

                //Put the position of the choose list inside extra
                positionItem=position;
                pongIntent.putExtra("PositionInListSet",positionItem);

                setResult(Activity.RESULT_OK,pongIntent);

                Log.i("CounID *******************************"," "+counId);
                finish();
            }
         });
    }
}

解决方案

Since by default ListViews are set to a selection mode of NONE, in touch mode the setSelection method won't have visual effect.

For keeping the previous selection / visually display an explicit selection, first you must set your listview's choice mode appropriately:

listview.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

It's useful to read the API Docs of these methods:

  • setSelection

void android.widget.AdapterView.setSelection(int position)

Sets the currently selected item. To support accessibility subclasses that override this method must invoke the overriden super method first.

Parameters:
position Index (starting at 0) of the data item to be selected.

  • setChoiceMode

void android.widget.ListView.setChoiceMode(int choiceMode)

Defines the choice behavior for the List. By default, Lists do not have any choice behavior (CHOICE_MODE_NONE). By setting the choiceMode to CHOICE_MODE_SINGLE, the List allows up to one item to be in a chosen state. By setting the choiceMode to CHOICE_MODE_MULTIPLE, the list allows any number of items to be chosen.

Parameters:
choiceMode One of CHOICE_MODE_NONE, CHOICE_MODE_SINGLE, or CHOICE_MODE_MULTIPLE

In case this is not enough (say you'd like to always show the last selection differently beside the current selection), you should store your last selected item (a data which populates the ListAdapter) as lastSelectedItem, and in your adapter's getView method assign a different background resource to the renderer if it equals this lastSelectedItem.

If your last selection wouldn't refresh on selection change, you should explicitly call the notifyDataSetChanged method on your adapter instance.

Update
Since your activity containing the ListView is a child of an activity which waits for this one's result (based on the setResult(Activity.RESULT_OK,pongIntent); part), the initial idea is correct, the last position should be passed through the intent when starting the activity:

selectedListItem = getIntent().getIntExtra("PositionInList", -1);
lvUsers.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
lvUsers.setSelection(selectedListItem);

The ListView.CHOICE_MODE_SINGLE solution would work if you remain in the same activity, but you are finishing it on every itemClick (selection change), that's why the extra data should be passed to the starting Intent.

You can also set the previously selected item's background from your adapter -as mentioned above-, overriding its getView method:

lvUsers.setAdapter(new ArrayAdapter(this, R.id.counlistView, groups)
{
    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        final View renderer = super.getView(position, convertView, parent);
        if (position == selectedListItem)
        {
            //TODO: set the proper selection color here:
            renderer.setBackgroundResource(android.R.color.darker_gray);
        }
        return renderer;
    }
});

这篇关于如何突出在Android的行中的ListView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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