高亮显示水平列表视图中选择的项目

高亮显示水平列表视图中选择的项目

本文介绍了高亮显示水平列表视图中选择的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在默认情况下,我需要显示一个项目所强调的水平列表视图,并在用户中我想强调该项目的水平列表视图中选择另一个项目(除去较早,并强调当前选定的),对于我正在用下面的code试图在我的适配器

适配器: -

  INT的selectedIndex;
 @覆盖
 公共查看getView(INT位置,查看convertView,父母的ViewGroup){
   视图V = convertView;
   V = LayoutInflater.from(parent.getContext())。膨胀(
        R.layout.hlist_rowitem,NULL);  如果(位置==的selectedIndex){
    v.setBackgroundColor(Color.parseColor(#ABCDEF));
 }
}

和从列表视图做什么,在做活动来改变突出了项目的位置与活动选择在另一个项目之后。

活动: -

  INT SINDEX;
。SINDEX = getIntent()getIntExtra(位置,0);
hlAdapter =新HSelectedAdapter(InsuranceCard.this,rowItems,SINDEX);
hListView.setAdapter(hlAdapter);
hListView.setOnItemClickListener(新OnItemClickListener(){
@覆盖
公共无效onItemClick(适配器视图<>母公司,观景,INT位置,长的id){
 //其他code到这里
}
});


解决方案

我会使用的并有ListView的处理与选择的。

颜色列表中会是这个样子:

 <?XML版本=1.0编码=UTF-8&GT?;
<选择的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
    <项目的android:STATE_ pressed =真
          机器人:颜色=#为aabbcc/> <! - pressed - >
    <项目的android:state_activated =真
          机器人:颜色=#FEDCBA/> <! - 选择 - >
    <项目的android:颜色=#ABCDEF/> <! - 默认 - >
< /选择>

和它应该被设置为在列表视图的 R.layout.hlist_rowitem listSelector 背景。

编辑:
要接收click事件时更改的选择:

  hListView.setOnItemClickListener(新OnItemClickListener(){
    @覆盖
    公共无效onItemClick(适配器视图<>母公司,观景,INT位置,长的id){
        hListView.setSelection(位置);
    }
});

ListView控件将取消旧的/默认项,并在指定位置选择新项目。

编辑2:默认情况下在ListView没有选择的模式设置,以便确保你将它设置在XML或code: listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

By default i need to show one item as highlighted in horizontal list view and when the user selected another item in the horizontal list view i want to highlight that item(removing the earlier and highlight the currently selected) for that i'm trying with the following code,in my adapter

Adapter:-

 int selectedIndex;
 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
   View v = convertView;
   v = LayoutInflater.from(parent.getContext()).inflate(
        R.layout.hlist_rowitem, null);

  if (position == selectedIndex) {
    v.setBackgroundColor(Color.parseColor("#abcdef"));
 }
}

and after selecting another item from activity in from the list view what to do in activity to change highlighting position of the item.

Activity:-

int sIndex;
sIndex = getIntent().getIntExtra("POSITION", 0);
hlAdapter = new HSelectedAdapter(InsuranceCard.this, rowItems, sIndex);
hListView.setAdapter(hlAdapter);
hListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
 //other code goes here
}
});
解决方案

I'd would use a color state list resource and have the ListView handle the selection with setSelection(position).

The color list would look something like this:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:color="#aabbcc"/> <!-- pressed -->
    <item android:state_activated="true"
          android:color="#fedcba"/> <!-- selected -->
    <item android:color="#abcdef"/> <!-- default -->
</selector>

and it should be set as background of the R.layout.hlist_rowitem or as listSelector on the listview.

Edit:To change the selection when receiving a click event:

hListView.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
        hListView.setSelection(position);
    }
});

The ListView will deselect the old/default item and selects the new item at the specified position.

Edit 2: By default the ListView don't have a choice mode set so make sure you either set it in xml or in code: listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

这篇关于高亮显示水平列表视图中选择的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 01:17