Question

i used horizontallistview in android. and it was worked very nice. and i had horizontallistview item selected when i click. and it also worked. when i click, one item's background was changed. and this is my source code.

    TextView selectedListItem = null;
    String pre_Color = "#6C6C6C";
    String select_Color = "#FFFFFF";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        listview = (HorizontialListView) findViewById(R.id.listview);
        listview.setAdapter(mAdapter);

        listview.setOnItemClickListener(new ListViewItemClickListener());

    }

    private class ListViewItemClickListener implements
            AdapterView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            mPager.setCurrentItem(position);

            if (selectedListItem != null) {
                TextView previousTitle = (TextView) selectedListItem
                        .findViewById(R.id.title);
                previousTitle.setBackgroundResource(R.drawable.list_shape);
                previousTitle.setTextColor(Color.parseColor(pre_Color));
            }

            TextView title = (TextView) view.findViewById(R.id.title);
            title.setBackgroundResource(R.drawable.list_selected_shape);
            title.setTextColor(Color.parseColor(select_Color));

            selectedListItem = title;
        }
    }

but when i drag horizontallistview and back again at the same point, the item that i had changed by clicking was not changed!!!! this make me crazy.

enter image description here

like this picture, i select one item. and i drag some items and come back again this point

then.....

enter image description here

it became like this. there's no blue background!!!

what can i have to do to solve this problem.

Was it helpful?

Solution

That error is because Android keeps the view when it has been generated, to reuse it when showing the view one more time.

Especially read about convertView argument in the documentation of Adapter :

http://developer.android.com/reference/android/widget/Adapter.html#getView(int, android.view.View, android.view.ViewGroup)

You may have something like that in getView :

if(convertView != null){
    return convertView;
}

Which is not a good way to do if you plan to update the view in your listview. The best way is to use ViewHolder pattern, as described here :

http://www.jmanzano.es/blog/?p=166

OTHER TIPS

this is due to list view recycling, I'd make a custom drawable and have it show as selected when checked, then in your on item click call

listview.setItemChecked(position, true);

you will need handling for unsettling other items as not checked ect.

keep a track of what item was clicked on. Then in the adapter, in the getview, check if the position of the item matches the one that was clicked, if so, set it bg resource to the color or drawable u want.

Hope that helps.

In this case I think you need to manage it from your ArrayAdapter class. There you have to remember last clicked item view and position.

Basic idea is onListItemClick pass the clicked position and itemview to adapter there we will set it's background with the color you like and changed the last selected to view background color to default color.

 public class SampleAdapter extends ArrayAdapter<Object> {

    private int mSelection = 0;

    public SampleAdapter(Context context, int resource, int textViewResourceId,
            List<Object> objects) {
        super(context, resource, textViewResourceId, objects);
    }

    public void setSelection(int mSelection, View selectedItemView) {
        this.mSelection = mSelection;

        if (selectedItemView!= null && lastSelectedRow != null
                && selectedItemView!= lastSelectedRow) {
            lastSelectedRow
                    .setBackgroundResource(R.drawable.bg_normal);
            selectedItemView
                    .setBackgroundResource(R.drawable.bg_selected);
        }

        this.lastSelectedRow = selectedItemView;

    }

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

        //Usual code logic here....

        if (mSelection == position) {
            mViewHolder.mRootView
                    .setBackgroundResource(R.drawable.bg_selected);
            lastSelectedRow = mViewHolder.mRootView; 
        } else {
            mViewHolder.mRootView
                    .setBackgroundResource(R.drawable.bg_normal);
        }


        return view;
    }

    private static class ViewHolder {
        TextView name;
        View mRootView;
    }
}

On List Item click you need to pass clicked item and position to adapter.

public void onItemClick(AdapterView<?> arg0, View listItemView,
        int position, long id) {

    if(myAdapter != null )
    {
        myAdapter.setSelection(position,listItemView);
    }
}

if you want to call this set selection from other point you can call it like this.

myAdapter.setSelection(position,mListViwe.getChildAt(pos));
mListViwe.setSelectionFromTop(position, 0);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top