android listview background not changing on swipe

1.7k Views Asked by At

I've setup swipe list view with https://github.com/47deg/android-swipelistview. It does provide various event listeners to handle swipe related events.

Everything works perfectly except one thing.

I want to change background color of swiped list item only. It revert back once it gets back.

enter image description here

I can do this with tap event from adapter. I could've used list adapter to do this but since I want to change background on swipe left (listener ) only, I can not use adapter. So doing this from adapter might not work.

Following listeners work for me -

swipeListView.setSwipeListViewListener(new BaseSwipeListViewListener() {
    @Override
    public void onOpened(int position, boolean toRight) {       
         View v = swipeListView.getChildAt(position);
         swipeListView.getChildAt(position).setBackgroundColor(Color.CYAN);

         ci = u_items.get(position); // getter of list item data

         Toast.makeText(getApplicationContext(), ci.getTitle().toString(), Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onClosed(int position, boolean fromRight) {
    }

    @Override
    public void onListChanged() {
    }

    . // other listeners
    .
    .
    .
    .
    .

This does not throw any error or warning but I can't see background being changed. Toast does show up so swipe left listener is actually working.

Perhaps, I'm doing something wrong while setting background itself. Not sure what. Since, swipelistview is custom view that extends ListView I can't imagine how to do this from that listener only.

Even, I could've used android list selection to set background color in XML file but it works with tap listener only. So this option is also eliminated.Other than this everything else works so perfect.

A push in right direction would be good. If any code is needed further to analyze I can edit question.

2

There are 2 best solutions below

2
On

you already defined v so you can use it better for you and easier.

View v = swipeListView.getChildAt(position);
v.setBackgroundColor(Color.CYAN);
2
On

I was able to do this by obtaining the background view and changing it that way. I used the onStartOpen method instead of the onOpen method.

@Override
public void onStartOpen(int position, int action, boolean right) {
    View v = mSwipeListView.getChildAt(position);
        if (v == null) return;

        if (right) { // nothing

        } else {
            final ImageView mCurrImage = (ImageView) v.findViewById(R.id.myImageView);
            mCurrShareImage.setBackground(Color.CYAN);
        }
    }
}