How to realize the side slide button of Android
This article mainly introduces the relevant knowledge of "how to realize the Android side slide button". The editor shows you the operation process through the actual case, the operation method is simple and fast, and the practicality is strong. I hope this article "how to realize the Android side slide button" can help you solve the problem.
The effect of the Android side slide button is as follows:
Import closure
Pour the following statement into the build.gradle file under the directory
Implementation 'com.github.WangcWj:WeSwipe:1.0.2' implementation' cn.wang.we:network:1.0.0'
Pour the following statement into the project build.gradle file
Create a RecyclerView child layout file
Effect description
The overall effect picture, due to the use of RelativeLayout layout, overrides the side slide button.
We then select the side slide button so that we can see the position of the button
Release the side slide button and see how it works.
The layout code creates the RecyclerView adapter description
When using a third-party API wheel, we cannot inherit a RecyclerView.Adapter class as we did when we established the adapter, but instead inherit the WeSwipeProxyAdapter abstract class
We generally implement the following three methods according to our needs
@ Override public float getSwipeWidth () {/ / width of the side slide button} @ Override public View needSwipeLayout () {/ / outermost Layout of the subitem layout file @ Override public View onScreenView () {/ / the outermost subitem layout file Outer Layout} subitem Click event / / create Click event Interface function public interface ParkingOnClick {void OnClickListener (View view Int Position) } / / Click event call public void setOnclick (ParkingOnClick onclick) {this.onClick = onclick;} / / introduce return new ViewHolder (view,onClick) in layout View; / / Inner class implements click event class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener// constructor public ViewHolder (@ NonNull View itemView,ParkingOnClick Listener) this.Listener = Listener / / Click the event to expose @ Override public void onClick (View v) {if (Listener! = null) {Listener.OnClickListener (v, getPosition ());}}
Adapter code
Public class ParkingAdapter extends WeSwipeProxyAdapter {List data = new ArrayList (); private ParkingOnClick onClick; public ParkingAdapter (List data) {this.data = data;} @ NonNull @ Override public ViewHolder onCreateViewHolder (@ NonNull ViewGroup parent, int viewType) {View view = LayoutInflater.from (parent.getContext ()) .inflate Return new ViewHolder (view,onClick); public void onBindViewHolder (@ NonNull ViewHolder holder, int position) {ParkingData bookingData = data.get (position); holder.BigPlace.setText (bookingData.getBigPlace ()); holder.SmallPlace.setText (bookingData.getSmallPlace ()); holder.ParkingNumber.setText (bookingData.getParkingNumber () + "); holder.Distance.setText (bookingData.getDistance ()) Holder.SideText.setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View v) {Log.d ("Da", 1 + "); if (onClick! = null) {onClick.OnClickListener (vForce position) }); public int getItemCount () {return data.size (); class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener,WeSwipeHelper.SwipeLayoutTypeCallBack {private TextView BigPlace,SmallPlace,Distance,ParkingNumber,SideText; private LinearLayout SideLayout; private ParkingOnClick Listener Public ViewHolder (@ NonNull View itemView,ParkingOnClick Listener) {super (itemView); this.Listener = Listener; BigPlace = itemView.findViewById (R.id.SimplePlace); SmallPlace = itemView.findViewById (R.id.detailPlace); Distance = itemView.findViewById (R.id.ParkingDistance); ParkingNumber = itemView.findViewById (R.id.ParkingNumber) SideText = itemView.findViewById (R.id.SideText); SideLayout = itemView.findViewById (R.id.SideLayout);} @ Override public float getSwipeWidth () {return SideText.getWidth (); public View needSwipeLayout () {return SideLayout Public View onScreenView () {public void onClick (View v) {if (Listener! = null) {Listener.OnClickListener (v, getPosition ()); / / callback interface public interface ParkingOnClick {void OnClickListener (View view,int Position) / / Click event public void setOnclick (ParkingOnClick onclick) {this.onClick = onclick;} application
Parsing xml data
Private void InitData () {String [] parkingArray = getResources () .getStringArray (R.array.parking); for (int I = 0; I