首頁 > 軟體

Android 側滑按鈕的實現程式碼

2022-04-08 16:00:17

Android側滑按鈕效果如下所示:

匯入閉包

將以下語句倒入目錄下build.gradle檔案內

 implementation 'com.github.WangcWj:WeSwipe:1.0.2'
 implementation 'cn.wang.we:network:1.0.0'

將以下語句倒入專案build.gradle檔案內

建立RecyclerView子項佈局檔案

效果描述

整體效果圖,因為使用RelativeLayout佈局的緣故,將側滑按鈕給覆蓋了

我們接下來選中側滑按鈕,由此我們可以看見按鈕的位置

將側滑按鈕釋放出來,看一下效果

佈局程式碼

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_marginTop="10dp">
    <TextView
        android:id="@+id/SideText"
        android:layout_width="100dp"
        android:layout_height="60dp"
        android:text="選擇車位"
        android:textSize="15sp"
        android:background="#DD2248"
        android:gravity="center"
        android:textColor="#ffffff"
        android:layout_marginRight="10dp"
        android:layout_centerInParent="true"
        android:layout_alignParentRight="true"/>
    <!--大-->
    <LinearLayout
        android:id="@+id/SideLayout"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:orientation="horizontal"
        android:background="@drawable/btn_style"
        android:gravity="center"
        android:padding="10dp">
        <!--小-->
        <LinearLayout
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_marginLeft="5dp">
            <TextView
                android:id="@+id/SimplePlace"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="工人文化宮停車場"
                android:textColor="#000000"
                android:textSize="15sp"
                android:singleLine="true"
                android:ellipsize="end"
                />
            <TextView
                android:id="@+id/detailPlace"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="田心大道52號附件"
                android:textSize="10sp"
                android:layout_marginTop="10dp"
                android:singleLine="true"
                android:ellipsize="end"
                />
        </LinearLayout>
        <!--小-->
        <LinearLayout
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_marginLeft="10dp"
            android:layout_gravity="center"
            android:gravity="center">
            <ImageView
                android:id="@+id/ParkingIcon"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:src="@drawable/road_sign"
                android:scaleType="fitCenter"/>

            <TextView
                android:id="@+id/ParkingDistance"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="201m"
                android:textSize="10sp" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:gravity="center">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="空餘車位:"
                android:textColor="#000000"
                android:textSize="15sp"/>
            <TextView
                android:id="@+id/ParkingNumber"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="10個"
                android:textColor="#ff0000"
                android:textSize="15sp"/>
        </LinearLayout>
    </LinearLayout>
</RelativeLayout>

建立RecyclerView介面卡

描述

使用第三方API輪子時,不能像當初我們建立介面卡一樣繼承一個RecyclerView.Adapter<VH>類,而要繼承WeSwipeProxyAdapter<VH>抽象類
我們一般根據需求需要實現以下三個方法

 @Override
            public float getSwipeWidth() {
             //側滑按鈕的寬度
            }
            @Override
            public View needSwipeLayout() {
            //子項佈局檔案的最外層Layout
            }
            @Override
            public View onScreenView() {
             //子項佈局檔案的最外層Layout
            }

子項點選事件

//建立點選事件介面函數
public interface ParkingOnClick{
            void OnClickListener(View view,int Position);
        }

//點選事件呼叫
public void setOnclick(ParkingOnClick onclick) {
            this.onClick = onclick;
        }

//引入佈局View中
return new ViewHolder(view,onClick);

//內部類實現點選事件
class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener

//建構函式
public ViewHolder(@NonNull View itemView,ParkingOnClick Listener)
this.Listener = Listener;

//點選事件外露
@Override
            public void onClick(View v) {
                if (Listener != null) {
                    Listener.OnClickListener(v, getPosition());
                }
            }

介面卡程式碼

public class ParkingAdapter  extends WeSwipeProxyAdapter<ParkingAdapter.ViewHolder> {
        List<ParkingData> data = new ArrayList<>(  );
        private ParkingOnClick onClick;
        public ParkingAdapter(List<ParkingData> data ){
            this.data = data;
        }
        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from( parent.getContext() ).inflate( R.layout.parking_item,parent,false );
            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( v,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());
        //回撥介面
        public interface ParkingOnClick{
            void OnClickListener(View view,int Position);
        //點選事件
        public void setOnclick(ParkingOnClick onclick) {
            this.onClick = onclick;
}

應用

解析xml資料

private void InitData(){
        String[] parkingArray = getResources().getStringArray(R.array.parking);
        for (int i = 0; i <parkingArray.length ; i+=4) {
            ParkingData data = new ParkingData(parkingArray[i],parkingArray[i+1],parkingArray[i+2],Integer.parseInt(parkingArray[i+3]));
            dataList.add(data);
        }
    }

繫結

WeSwipe.attach(ParkingRecycler);

private void InitRecycler(){
        LinearLayoutManager manager = new LinearLayoutManager(this);
        ParkingRecycler.setLayoutManager(manager);
        adapter = new ParkingAdapter(dataList);
        ParkingRecycler.setAdapter(adapter);
        WeSwipe.attach(ParkingRecycler);
    }

點選事件參照

adapter.setOnclick(new ParkingAdapter.ParkingOnClick() {
            @Override
            public void OnClickListener(View view, int Position) {
               //do anything...
            }
        });

原作者GitHub專案地址

 https://github.com/WangcWj/SideslippingDemo

到此這篇關於Android 側滑按鈕的文章就介紹到這了,更多相關android 側滑按鈕內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com