ListView:当我滚动时,突出显示消失

时间:2017-01-31 08:31:03

标签: android android-layout listview

我使用下面的代码突出显示点击的项目。但是有两个问题:

  • 我第一次点击某个项目时,则不会显示突出显示。因此,只有在我第二次点击项目后才会突出显示项目。
  • 滚动时,突出显示消失。相反,我需要突出显示项目,直到单击另一个项目。

我该如何解决?

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

            view.setSelected(true);

bg_key.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector
    android:id="@+id/myselector"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Non focused states -->
    <item
        android:state_selected="true"
        android:drawable="@color/activated_color" />
    <item
        android:drawable="@color/default_color" />
</selector>

song_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<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="0dp"
    android:paddingRight="0dp"
    tools:context=".MainActivity"
    android:paddingTop="5dp"
    android:paddingBottom="5dp"
    android:background="@drawable/bg_key">

2 个答案:

答案 0 :(得分:1)

您需要在适配器中保留对突出显示的position的引用。然后

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        view.setSelected(true);
        lvAdpapter.highlightPos = position; // declare highlightPos =-1 by default in you adapter.

然后在适配器的getView内,执行此操作:

public View getView(int position, View convertView, ViewGroup parent) {
//existing code

songLay.setSelected(position ==highlightPos);
return songLay;
}

答案 1 :(得分:0)

列表视图的getView()调用每个滚动以及每个新的列表项调用getView,如果listview项当前在UI上不可见,则其convertView等于null:< / p>

解决这个问题:获取一个boolean isSelected []数组,每个数组初始值为false。

boolean[] isSelected = new boolean[20]; //your list view size
Arrays.fill(isSelected, false);
适配器类中的

 public View getView(int position, View convertView, ViewGroup parent) {

        final ViewHolder holder;
                    string text = itemData[position]
        if (convertView == null) {
            rowLayout = (RelativeLayout) LayoutInflater.from(context)
                    .inflate(R.layout.list_view_item, parent, false);
            holder = new ViewHolder();

            holder.txtString= (TextView) rowLayout
                    .findViewById(R.id.txtTitle);
            rowLayout.setTag(holder);
        } else {
            rowLayout = (RelativeLayout) convertView;
            holder = (ViewHolder) rowLayout.getTag();
        }

        if(isSelected[position] == true){
                   holder.txtString.setText("Selected")
                   rowLayout.setBackGround(selected)
        }else{
             holder.txtString.setText("Not Selected")
             rowLayout.setBackGround(notSelected)
        }




    public class ViewHolder {
        public TextView txtString;
        //your listview contents
    }

列表视图oItemClick使用:

 listView.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1,
                        int position, long arg3) {
                    // TODO Auto-generated method stub
                      isSelected[position] = true  // on selection

                      RelativeLayout rowLayout = (RelativeLayout) view;
                      rowLayout.setBackGround(Selected);

                    }
            });