So, this can be done 2 ways as I see it:
- Listen to swipes on the entire list, figure out which row the swiped occurred on, and show/hide the button for the correct row.
- Listen to swipes on each row, and show/hide the button for that row.
Right now you are setting the listener on the list, but trying to apply show/hide the button attached in the adapter. In a way, you are doing half of each solution. With what you have, I suggest you go with option #2.
In your TransactionAddDropViewHolder
, you need to get a reference to the root View
in the row layout, so we can set the OnSwipeTouchListener
to it.
public static final class TransactionAddDropViewHolder {
public View rootView = null;
// Your other code
public void swipeButtons() {
rootView.setOnTouchListener(new OnSwipeTouchListener() {
public void onSwipeRight() {
withdrawButton.setVisibility(View.VISIBLE);
}
public void onSwipeLeft() {
withdrawButton.setVisibility(View.INVISIBLE);
}
});
}
// Your other code
}
Next, in your getView
method, update it so your TransactionAddDropViewHolder
gets a reference to the root View
in the layout.
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final TransactionAddDrop addDropData = this.addDropList.get(position);
TransactionAddDropViewHolder holder = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.fragment_pending_transaction_list_item, null);
holder = new TransactionAddDropViewHolder();
// Get a reference to the root of the row layout
holder.rootView = convertView.findViewById(R.id.swipeRight);
holder.withdrawButton = convertView.findViewById(R.id.pendingTransactionItem_withdrawButton);
holder.addContainer = (LinearLayout) convertView.findViewById(R.id.pendingTransactionItem_addContainer);
holder.swipeButtons();
convertView.setTag(holder);
} else {
holder = (TransactionAddDropViewHolder) convertView.getTag();
holder.swipeButtons();
}
}
There is one more issue you will have, the button will correctly show up when swiped, but if the user continues to scroll, it will appear on other rows. This is because ListView
recycles its row's View
s. I suggest you do either 2 things:
- When the user starts scrolling again after the button is present, you hide it again. This is what iOS does with "swipe to delete" in lists.
- Use a
boolean
to store in your data object if the row is in the mode where the button is visible,isDeleteShowing
for example. Then in your onSwipe callbacks, you setisDeleteShowing
to true/false. Lastly, in yourgetView
, you show/hide of the button there depending onisDeleteShowing
for that data row.
Hope this all helps.