Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have horizontal listView with TextView items, my item layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?android:attr/activatedBackgroundIndicator">

<TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="35dp" >
</TextView>

<View 
    android:layout_width="5dp"
    android:layout_height="35dp"/>

</LinearLayout>

I want to highlight only one selected item by setting borded to it's textView, eg. when I click to item1 I want to see this item with small border, and now when I click item2 - this will be highlighted and border from item1 will disappear.

Any ideas ?

Thanks in advance!

share|improve this question
    
Use ListView.getSelected() which should return the list item view you want to highlight, find the TextView in it, change border and remember it to reset border when another item is selected –  Michael Butscher Aug 6 '13 at 10:03
add comment

2 Answers

  • May be you need property to store last highlight item.
    Etc:

    View lastHighlightItem=null;

  • Overwrite onItemClick method.
    Etc:

onItemClick(AdapterView parent, View view, int position, long id){

       if(lastHighlightItem!=null)
          lastHighlightItem.recover();
        //recover unHighlight state 
        view.highlight();//Highlight as you like
        lastHighlightItem = view;

}

share|improve this answer
add comment

In your activity create a private class like the following:

private class ListChoice 
{
    private int value;

    public ListChoice() 
    {
        value = -1;
    }

    public void setListChoice(int v) 
    { 
        value = v; 
    }

    public final int getListChoice()
    {
        return value;
    }
}

In setOnItemClickListener set the clicked row

listView.setOnItemClickListener(new OnItemClickListener()
{
    @Override
    public void onItemClick(AdapterView<?> parent, View v, int position, long id)
    {
        listChoice.setListChoice(position);
        adapter.notifyDataSetChanged();
    }
});

In the listview adapter use it to highlight or disable highlight

@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
    ......
    if(position == listChoice.getListChoice())
    {
        //highlight clicked item
    }
    else
    {
        //disable highlight for the rest of items
    }
    ......
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.