1

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

4 回答 4

2

我会使用颜色状态列表资源并让 ListView 使用setSelection(position)处理选择。

颜色列表看起来像这样:

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

并且它应该被设置为R.layout.hlist_rowitem或作为listSelector列表视图的背景。

编辑:在接收点击事件时更改选择:

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

ListView 将取消选择旧/默认项并在指定位置选择新项。

编辑 2:默认情况下 ListView 没有设置选择模式,因此请确保您在 xml 或代码中设置它:listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

于 2014-07-08T11:40:27.320 回答
1

您可以通过两种方式实现这一目标。

  1. 手动清除所有项目并设置在 onItemClick() 中选择

    listview.setOnItemClickListener(new OnItemClickListener() {
    
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
    
            for (int i = 0; i < adapter.getCount(); i++) {
            View item = listview.getChildAt(i);
            if (item != null) {
                item.setBackgroundResource(R.drawable.unselected);
            }
            arg1.setBackgroundResource(R.drawable.selected);
        }
    
        }
    });
    
  2. 使用选择器并让 listview 自己做。

/drawable/selector_list.xml

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

   <item android:drawable="@drawable/selected" android:state_selected="true"/>
   <item android:drawable="@drawable/selected" android:state_activated="true"/>
   <item android:drawable="@drawable/unselected"/>

</selector>

并添加android:listSelector="@drawable/selector_list"到您的列表视图

于 2014-07-08T11:41:01.653 回答
0

请按照下列步骤操作: 1) 声明一个布尔数组。公共静态布尔 ClickItem[]; 2)在oncreate ClickItem=new boolean[你的数组大小];Arrays.fill(ClickItem, false);

在您的适配器中编写此代码 a)
if ClickItem[pos] { v.setBackgroundColor(Color.parseColor("#abcdef")); }别的

a) v.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

        Arrays.fill(ClickItem, false);
            ClickItem[pos]=true;

            adapter1.notifyDataSetChanged();


        }
    });
于 2014-07-08T11:45:53.997 回答
0

在您的 drawblw 文件夹中添加 listitemclick.xml 这是代码。2)然后在你的 hlist_rowitem.xml 中设置 background="@drawable/listitemclick"

于 2014-07-08T11:34:55.447 回答