ListView Item selection Android

2020-02-07 02:40发布

I have a listview in that i want when my activity starts then the first item will have background and the other items don't have any background.After that if the user selects any other item then the background of that particular item will now be blue and the rest don't have any background.Please help me on this.

My layout_effect.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_focused="true">
    <shape>
        <solid android:color="#ffffff" />
    </shape>
</item>
    <item>
    <shape>
        <solid android:color="#00a7eb" />
    </shape>
</item>
</selector>

My GetView Code where i am setting the default Background

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

            vi.setBackgroundResource(R.drawable.selection_effect);
            vi.setSelected(true);

            }
        }

Any my onclickListner of the listview

onewayListView .setOnItemClickListener(new OnItemClickListener() {


        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub


        Toast.makeText(getBaseContext(), "Clicked", 1000).show();
        }
    });

My Listview xml

<ListView
            android:id="@+id/lvDepartures"
            android:layout_weight="1"
            android:layout_marginLeft="5dp"
            android:choiceMode="singleChoice"
            android:layout_height="wrap_content"
            android:listSelector="@drawable/selection_effect"
            android:layout_width="wrap_content"/>

Please help me how could i solve this problem i am stucked in it from last 1day.

6条回答
混吃等死
2楼-- · 2020-02-07 02:55

Create a selector for listview

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/button_pressed_yellow"
          android:state_pressed="true" />
    <item android:drawable="@drawable/button_focused_orange"
          android:state_focused="true" />
    <item android:drawable="@drawable/button_normal_green" />
</selector>

setOnItemClickListener write the following function

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

                Toast.makeText(getApplicationContext(),
                ""+position, Toast.LENGTH_SHORT).show();
            }
        });
查看更多
Summer. ? 凉城
3楼-- · 2020-02-07 02:55

you should check this:

public View getView(int position, View convertView, ViewGroup parent) { 
    if(position%2 == 0){

        vi.setBackgroundResource(R.drawable.selection_effect);
        vi.setSelected(true);

        }else{

         // set another background.

        }
    }
查看更多
forever°为你锁心
4楼-- · 2020-02-07 03:00

For doing this what you can do is create a selector and apply it to parent view of your row.xml

  • Define color in strings.xml

    <color name="blue">#009FE3</color>

  • Create a selector

    <item android:drawable="@color/blue" android:state_activated="true" />
    <item android:drawable="@color/blue" android:state_focused="true" />
    

Now apply it as android:background="@drawable/row_selector" for parent view of your row.

  • Set ListView as CHOICE_MODE_SINGLE in java code

    listview.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

  • And finally setAdapter to your ListView.

    listview.setAdapter(adapter);

  • For selection of default item in ListView use,

    listview.setItemChecked(position, true);

I had created a demo example for the same you can download it from my github

查看更多
我只想做你的唯一
5楼-- · 2020-02-07 03:06

Remove your default list-selector in your listview or set it as null:

   <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:listSelector="@null" >
    </ListView>

Hope this helps.

查看更多
Emotional °昔
6楼-- · 2020-02-07 03:20

OnListItem click pass the selected position to the adapter and set the adapter again and in adapter check if selected position then set the background otherwise does not set the background.

查看更多
老娘就宠你
7楼-- · 2020-02-07 03:22

I'm not sure but try putting listView.setSelection(position); in listview's setOnItemClickListener

查看更多
登录 后发表回答