Android - Listview/Gridview item selection with scrolling

你。 提交于 2019-12-06 07:33:25

In your getView() method just add this test:

if (items.get(position).isSelected()){
   view.setBackgroundResource(R.drawable.rect_sel);
} else {
   view.setBackgroundResource(R.drawable.rect);
}

Or just view.setSelected(items.get(position).isSelected());. While you already have a selector for your list item.

You must define the current selection state of view inside getView method.

Add this line:

viewitem.setSelected(items.get(position).isSelected());

after viewholder has been created like:

holder.img.setImageResource(items.get(position).getImage());
viewitem.setSelected(items.get(position).isSelected());

I think you should set the id for your RelativeLayout then add it to ViewHolder

private static class ViewHolder{
    public TextView text;
    public ImageView img;
    RelativeLayout rl;
}

After that you handle event when click RelativeLayout then change background for RelativeLayout

 public View getView(...)
    ...
    ...
    // you should update the state of relative layout first
     if (items.get(position).isSelected()) {
        holder.setBackgroundColor(Color.parseColor("#ffff00"));
    }else{
         holder.setBackgroundColor(Color.parseColor("#ff0000"));
    }

    holder.rl.setOnClickListener(new View.OnClickListener(){ //remmember it is rl.setOnClick... not view.setOnClick...
        public void onClick(View v) {
              if (!items.get(position).isSelected()) {
                  items.get(position).setSelected(true);
                  holder.setBackgroundColor(Color.parseColor("#ffff00"));
              }else {
                  items.get(position).setSelected(false);
                  holder.setBackgroundColor(Color.parseColor("#ff0000"));
              }
        }
    });
}

Suggestion
You should modify your row layout like this (I have removed LinearLayout but the new layout still good)

    <!-- list_row.xml --> 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/list_selector"
android:orientation="horizontal"
android:padding="5dip" >

  <ImageView 
     android:id="@+id/item_image"
     android:layout_marginRight="5dip"
     android:padding="3dip"
     android:layout_alignParentLeft="true"
     android:layout_width="@dimen/img_side"
     android:layout_height="@dimen/img_side" />

<TextView
    android:id="@+id/item_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/item_image"
    android:layout_centerVertical="true"
    android:textColor="@color/black"
    android:textSize="@dimen/textnorm"
    />

</RelativeLayout>

Remember that your list row layout is more simple then your listview will scroll faster, smooth and prevent some annoying bug.

Hope this help

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!