listview - Swipe right display button in android custom list view -
i trying implement, show button on swipe right in custom list view. problem facing right is, implemented onswipetouchlistener found online. able see swipe button. not consistent, mean if swipe right on 1 row, button displaying in other row. tried.
i created adapter holding view
//transactionadddropviewholder public static final class transactionadddropviewholder { public view moveupbutton = null; public view movedownbutton = null; public view withdrawbutton = null; public view reviewbutton = null; public view approvebutton = null; public view rejectbutton = null; public linearlayout addcontainer = null; public linearlayout dropcontainer = null; public void swipebuttons() { adddroplistview.setontouchlistener(new onswipetouchlistener() { public void onswiperight() { withdrawbutton.setvisibility(view.visible); } public void onswipeleft() { withdrawbutton.setvisibility(view.invisible); } }); } public void showuserbuttons() { this.moveupbutton.setvisibility(view.visible); this.movedownbutton.setvisibility(view.visible); // this.withdrawbutton.setvisibility(view.visible); } public void hideuserbuttons() { this.moveupbutton.setvisibility(view.gone); this.movedownbutton.setvisibility(view.gone); // this.withdrawbutton.setvisibility(view.gone); } }
and custom view displaying onswipebutton
@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(); 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(); }
and onswipetouchlistener class
private final class gesturelistener extends simpleongesturelistener { private static final int swipe_threshold = 100; private static final int swipe_velocity_threshold = 100; @override public boolean ondown(motionevent e) { return true; } @override public boolean onfling(motionevent e1, motionevent e2, float velocityx, float velocityy) { boolean result = false; try { float diffy = e2.gety() - e1.gety(); float diffx = e2.getx() - e1.getx(); if (math.abs(diffx) > math.abs(diffy)) { if (math.abs(diffx) > swipe_threshold && math.abs(velocityx) > swipe_velocity_threshold) { if (diffx > 0) { onswiperight(); } else { onswipeleft(); } } } else { if (math.abs(diffy) > swipe_threshold && math.abs(velocityy) > swipe_velocity_threshold) { if (diffy > 0) { onswipebottom(); } else { onswipetop(); } } } } catch (exception exception) { exception.printstacktrace(); } return result; } }
adding xml layout
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res/com.cbssports.nflapp.ffb" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/swiperight" android:orientation="vertical" > <linearlayout android:id="@+id/claimlayout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <linearlayout android:id="@+id/claim" android:layout_width="60dp" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical" android:padding="10dp" > <com.cbssports.nflapp.ffb.customtext android:id="@+id/claimnumber" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centervertical="true" android:text="1" android:textcolor="#b4b4b5" android:textsize="36dp" app:typeface="oswald_regular" /> </linearlayout> <relativelayout android:id="@+id/pendingplayers" android:layout_width="200dp" android:layout_height="wrap_content" > <linearlayout android:id="@+id/tradedate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparenttop="true" android:orientation="horizontal" android:layout_torightof="@+id/claimlayout" android:paddingtop="5dp" > <com.cbssports.nflapp.ffb.customtext android:id="@+id/pendingtransactionitem_teamname" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textcolor="#b4b4b5" android:textsize="9dp" app:typeface="oswald_regular" /> <com.cbssports.nflapp.ffb.customtext android:id="@+id/pendingtransactionitem_bidamount" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#797979" android:text="" android:textcolor="#ffffff" android:textsize="9dp" /> </linearlayout> <linearlayout android:id="@+id/adddropview" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/tradedate" android:layout_torightof="@+id/claimlayout" android:orientation="vertical" > <linearlayout android:id="@+id/addviewcontainer" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <com.cbssports.nflapp.ffb.customtext android:layout_width="50dp" android:layout_height="wrap_content" android:paddingright="10dp" android:text="add" android:textcolor="#797979" android:textsize="15dp" app:typeface="oswald_regular" /> <linearlayout android:id="@+id/pendingtransactionitem_addcontainer" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" /> </linearlayout> <linearlayout android:id="@+id/adddropviewcontainer" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_torightof="@+id/claimlayout" android:orientation="horizontal" > <com.cbssports.nflapp.ffb.customtext android:layout_width="50dp" android:layout_height="wrap_content" android:paddingright="10dp" android:text="drop" android:textcolor="#797979" android:textsize="15dp" app:typeface="oswald_regular" /> <linearlayout android:id="@+id/pendingtransactionitem_dropcontainer" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" /> </linearlayout> </linearlayout> </relativelayout> <linearlayout android:id="@+id/pendingtransactionitem_userbuttoncontainer" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentright="true" android:layout_gravity="center_vertical" android:layout_torightof="@+id/pendingplayers" android:gravity="center" android:orientation="vertical" > <linearlayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentright="true" android:layout_gravity="center_vertical" android:gravity="center" android:orientation="horizontal" > <button android:id="@+id/pendingtransactionitem_moveupbutton" android:layout_width="20dp" android:layout_height="15dp" android:layout_margin="3dp" android:background="@drawable/gfx_up_arrow" android:padding="1dp" android:textcolor="#ffffff" android:textsize="7sp" android:visibility="visible" /> <button android:id="@+id/pendingtransactionitem_movedownbutton" android:layout_width="20dp" android:layout_height="15dp" android:layout_margin="3dp" android:background="@drawable/gfx_down_arrow" android:padding="1dp" android:textcolor="#ffffff" android:textsize="7sp" android:visibility="visible" /> </linearlayout> <imageview android:id="@+id/pendingtransactionitem_withdrawbutton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="1dp" android:src="@drawable/ic_cancelclaim" android:visibility="gone" /> <!-- transaction review --> <imageview android:id="@+id/pendingtransactionitem_reviewbutton" android:layout_width="38dp" android:layout_height="38dp" android:padding="1dp" android:src="@drawable/btn_review" android:visibility="gone" /> </linearlayout> </linearlayout>
adding 1 more question. getting total count of view. want display in different layout. here oncreatedview
public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { fragmentpendingtrades = inflater.inflate(r.layout.fragment_transactions_pending, container, false); pendingtradesview = inflater; return fragmentpendingtrades; } public void onviewcreated(final view view, final bundle savedinstancestate) { this.adddroplistview = (listview) view.findviewbyid(r.id.transactions_pending_transactionslistview); this.adddropadapter = new transactionadddropadapter(pendingtradesview); this.adddroplistview.setadapter(this.adddropadapter); this.emptytransationscontainer = view.findviewbyid(r.id.transactions_pending_transactions_emptycontainer); textview gettotalcount = (textview) view.findviewbyid(r.id.transactions_pending_transactionsadddropcount); gettotalcount.settext(""+adddroplistview.getcount()); }
so, can done 2 ways see it:
- listen swipes on entire list, figure out row swiped occurred on, , show/hide button correct row.
- listen swipes on each row, , show/hide button row.
right setting listener on list, trying apply show/hide button attached in adapter. in way, doing half of each solution. have, suggest go option #2.
in transactionadddropviewholder
, need reference root view
in row layout, can set onswipetouchlistener
it.
public static final class transactionadddropviewholder { public view rootview = null; // other code public void swipebuttons() { rootview.setontouchlistener(new onswipetouchlistener() { public void onswiperight() { withdrawbutton.setvisibility(view.visible); } public void onswipeleft() { withdrawbutton.setvisibility(view.invisible); } }); } // other code }
next, in getview
method, update transactionadddropviewholder
gets reference root view
in 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(); // reference root of 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 1 more issue have, button correctly show when swiped, if user continues scroll, appear on other rows. because listview
recycles row's view
s. suggest either 2 things:
- when user starts scrolling again after button present, hide again. ios "swipe delete" in lists.
- use
boolean
store in data object if row in mode button visible,isdeleteshowing
example. in onswipe callbacks, setisdeleteshowing
true/false. lastly, ingetview
, show/hide of button there depending onisdeleteshowing
data row.
hope helps.