Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
92 views
in Technique[技术] by (71.8m points)

java - Custom adapter, selected item background

I have a problem whith custom adapter view. I try, change background of view on Click event. I have AdapterView.OnItemClickListener, where i get selected item, and calling myListView.invalidate();

After invalidate, calling adapters getView(...). Here code for this:

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

    View row = convertView;
    ProjectAdapterData projectItem;


    if (row == null) {

        LayoutInflater inflater = LayoutInflater.from(context);
        row = inflater.inflate(R.layout.project_small_item_layout, null);

        ProjectAdapterData projectAdapterData = new ProjectAdapterData();

        row.setTag(projectAdapterData);
        name = (TextView)row.findViewById(R.id.txtObjectName);
        if (objectData[position].Name!= null)
            name.setText(objectData[position].Name);
        adress = (TextView)row.findViewById(R.id.txtObjectAdress);
        if (objectData[position].Adress != null)
            adress.setText(objectData[position].Adress);
    }
    else {
        background = (RelativeLayout)row.findViewById(R.id.rlProjectBackground);
        if (objectData[position].isSelected)
            background.setBackgroundColor(context.getResources().getColor(R.color.cProjectSelected));
        else
            background.setBackgroundResource(R.color.cProjectUnSelected); //it's calls, but no result
        row.invalidate();
    }
    return row;
}

My question, why background doesn't change?

My selector_list

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_selected="true"
          android:color="@color/cProjectSelected"/>
        <item android:state_selected="false"
          android:color="@color/cProjectUnSelected"/>
    </selector>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

you can use selector to highlight item

In drawable folder create a xml file

list_selector.xml

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

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

</selector>

and set listSelector in xml for your listview like

android:listSelector="@drawable/list_selector"

color.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <color name="BLACK">#000000</color>
    <color name="WHITE">#FFFFFF</color>
    <color name="light_grey">#a5acb0</color>
    <color name="brown">#525964</color>
    <color name="dark_grey">#212121</color>
    <color name="aqua">#a6b1ba</color>
    <color name="red_cherry">#C9282D</color>
    <color name="silver">#A9A9A9</color>
    <color name="black">#000000</color>
    <color name="transparent">#00000000</color>
    <color name="white">#FFFFFF</color>
    <color name="blue">#00aceb</color>
    <color name="spiritclips_bck">#8AB8E0</color>
    <color name="translucent_black">#55000000</color>
    <color name="grid_bck">#627583</color>
    <color name="grey">#393430</color>
    <color name="dark_grey_bg">#1f1c17</color>
    <color name="login_font_color_1">#546778</color>
    <color name="login_font_color_2">#8E8E8E</color>
    <color name="blue_txt">#0f5690</color>

</resources>

for custom_list_item the layout should be

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="?android:attr/activatedBackgroundIndicator" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:textStyle="bold" />

</LinearLayout>

and minimum version of your application should be 11

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...