开发者

How to highlight ListView-Items

开发者 https://www.devze.com 2023-01-30 13:15 出处:网络
i got the following problem. i hav开发者_开发知识库e a ListView with custom rows consisting of an imageview and a textview.

i got the following problem.

i hav开发者_开发知识库e a ListView with custom rows consisting of an imageview and a textview. the textview's xml code is

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="26px"
    android:layout_marginLeft="3px"
    android:singleLine="true"
    android:ellipsize="end"
    android:textColorHighlight="#FEC403"
/>

then i have an itemclicklistener that works fine and i want to highlight the textview that has been clicked by doing the following.

public void onItemClick(AdapterView<?> adaptview, View clickedview, int position,
                long id) {
            //TODO: ACTIONS
            String pathtofile = (String) adaptview.getItemAtPosition(position);
            View rowview = (View) adaptview.getChildAt(position);
            rowview.setSelected(true);}

i wanted the highlight color to be "#FEC403" in the xml (that is a light orange) but the highlightcolor still is gray. so how to set the highlightcolor correctly?

thanks in advance

EDIT:

here is how i did it finally:

this is my ListView Item xml file:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/rowselector"
>

<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/musicicon"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:src="@drawable/musicicon"
    android:paddingLeft="3px"
/>


<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="26px"
    android:layout_marginLeft="3px"
    android:singleLine="true"
    android:ellipsize="end"
    android:focusable="false"
/>

and the rowselector.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"
    android:drawable="@color/orange" />
</selector>

and at last my OnItemClick is very short now:

@Override
public void onItemClick(AdapterView<?> adaptview, View clickedview, int position,
                long id) {
            //TODO: ACTIONS
            clickedview.setSelected(true);
}


@Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long arg3)
        {
            for(int a = 0; a < parent.getChildCount(); a++)
            {
                parent.getChildAt(a).setBackgroundColor(Color.BLACK);
            }

            view.setBackgroundColor(Color.RED);

        }


You should use a Selector.

This question and its answer might help....


In your Activity:

listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long itemid) {
            itemOnclickList(position, view);
        }
    });

    public void itemOnclickList(int position, View view) {      
    if (listview.isItemChecked(position)) {                           
       view.setBackgroundDrawable(getResources().getDrawable(R.drawable.image_checked));            
    } else {    
    view.setBackgroundDrawable(getResources().getDrawable(R.drawable.image_uncheck));           
    }
    adapter.notifyDataSetChanged();

}

In your Adapter:

  public View getView(int position, View view, ViewGroup parent) {
    View convertView = inflater.inflate(R.layout.listdocument_item, null);      

        ListView lvDocument = (ListView) parent;
        if (lvDocument.isItemChecked(position)) {
            convertView.setBackgroundDrawable(activity.getResources().getDrawable(R.drawable.image_checked));               
        } else {
            convertView.setBackgroundDrawable(activity.getResources().getDrawable(R.drawable.image_uncheck));               
        }

    return convertView;
}

Good luck!


This is how it should look using selector:

Create a file within drawable called selector_listview_item.xml, which looks like (change the colors to your own):

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- pressed -->
    <item android:drawable="@color/md_grey_200" android:state_pressed="true" />
    <!-- default -->
    <item android:drawable="@color/white" />
</selector>

Now in your layout which describes the list row (e.g. layout_list_row.xml), On the root layout add the android:background="@drawable/selector_listview_item", So the layout would look something like:

<?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="50dp"
    android:background="@drawable/selector_listview_item" <!-- this is the important line -->
    android:orientation="vertical">

    <!-- more stuff here -->
</LinearLayout>

(Note: you might want to add android:focusable="false" on you items within that listview row, to make the row clickable, as mentioned here)

Done.

0

精彩评论

暂无评论...
验证码 换一张
取 消