0

enter image description here

Games, Community, Music etc are in Horizontal list view.

Right now Community is selected and community related data is shown below.

I want to highlight Music when it is clicked and change Community colour to default.

listview.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                long arg3) {

((android.widget.LinearLayout)((android.widget.LinearLayout)arg1).getChildAt(2)).setBackgroundColor(getResources().getColor(R.color.MyBlue));
...

This changes the background colour of selected Item but when I scroll other item are also changed which are not selected. Above code changes linear layout which is define in item's xml file shown below.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:orientation="vertical" >

    <ImageView 
        android:id="@+id/IVCategory"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:contentDescription="@string/app_name"
        android:layout_margin="10dp"
        android:layout_gravity="center"    
        />

    <TextView 
        android:id="@+id/txtCatName"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:gravity="center"
        android:background="@android:color/transparent"
        />

    <LinearLayout
        android:id="@+id/selectedBlue"
        android:layout_width="match_parent"
        android:layout_height="3dp"
        android:orientation="vertical" >
    </LinearLayout>

</LinearLayout>

Is that possible to do that in the way I'm doing it? I have searched a lot but I couldn't understand how selectors can help me.

PageNotFound
  • 2,320
  • 2
  • 23
  • 34
Yawar
  • 1,924
  • 3
  • 29
  • 39
  • look at this post [item highlighting][1] [1]: http://stackoverflow.com/questions/16189651/android-listview-selected-item-stay-highlighted – FreshD Sep 18 '14 at 08:24

1 Answers1

0
    * Create Class:

    public class Test {
        private String  name;
        private int selected = 0;


        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }

        public void setSelected(int selected) {
            this.selected = selected;
        }

        public int getSelected() {
            return selected;
        }
    }


Create Custom adaptor:

    public class TestAdaptor extends BaseAdapter {
        Context context;
        List<Test> list;
        private LayoutInflater inflater;
        public TestAdaptor(Context context, List<Test> list ) {
            this.context  =  context;
            this.list  =  list;
            this.inflater  =  LayoutInflater.from(context);
        }
        @Override
        public int getCount() {
            return list.size();
        }
        @Override
        public test getItem(int position) {
            return list.get(position);
        }
        @Override
        public long getItemId(int position) {
            return position;
        }
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            View rowView = convertView;
            try {
                if(rowView == null)
                    rowView =  this.inflater.inflate(R.layout.volume_list_item, null);

                if(list.get(position).getSelected() == 1){
                    rowView.setBackgroundColor(this.context.getResources().getColor(R.color.app_color));
                }else{
                    rowView.setBackgroundColor(this.context.getResources().getColor(R.color.gray));
                }
                rowView.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(final View v) {
                        list.get(position).setSelected(1);
                        notifyDataSetChanged();
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
            }
            return rowView;
        }
    }*


finally:

listview.setAdaptor(new TestAdaptor(this, lsit));
Sheetal Suryan
  • 495
  • 2
  • 7
  • 19