Android?側(cè)滑按鈕的實(shí)現(xiàn)代碼
Android側(cè)滑按鈕效果如下所示:

導(dǎo)入閉包
將以下語(yǔ)句倒入目錄下build.gradle文件內(nèi)
implementation 'com.github.WangcWj:WeSwipe:1.0.2' implementation 'cn.wang.we:network:1.0.0'
將以下語(yǔ)句倒入項(xiàng)目build.gradle文件內(nèi)
創(chuàng)建RecyclerView子項(xiàng)布局文件
效果描述
整體效果圖,因?yàn)槭褂肦elativeLayout布局的緣故,將側(cè)滑按鈕給覆蓋了

我們接下來(lái)選中側(cè)滑按鈕,由此我們可以看見按鈕的位置

將側(cè)滑按鈕釋放出來(lái),看一下效果

布局代碼
<?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="工人文化宮停車場(chǎng)"
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號(hào)附件"
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個(gè)"
android:textColor="#ff0000"
android:textSize="15sp"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
創(chuàng)建RecyclerView適配器
描述
使用第三方API輪子時(shí),不能像當(dāng)初我們建立適配器一樣繼承一個(gè)RecyclerView.Adapter<VH>類,而要繼承WeSwipeProxyAdapter<VH>抽象類
我們一般根據(jù)需求需要實(shí)現(xiàn)以下三個(gè)方法
@Override
public float getSwipeWidth() {
//側(cè)滑按鈕的寬度
}
@Override
public View needSwipeLayout() {
//子項(xiàng)布局文件的最外層Layout
}
@Override
public View onScreenView() {
//子項(xiàng)布局文件的最外層Layout
}
子項(xiàng)點(diǎn)擊事件
//創(chuàng)建點(diǎn)擊事件接口函數(shù)
public interface ParkingOnClick{
void OnClickListener(View view,int Position);
}
//點(diǎn)擊事件調(diào)用
public void setOnclick(ParkingOnClick onclick) {
this.onClick = onclick;
}
//引入布局View中
return new ViewHolder(view,onClick);
//內(nèi)部類實(shí)現(xiàn)點(diǎn)擊事件
class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
//構(gòu)造函數(shù)
public ViewHolder(@NonNull View itemView,ParkingOnClick Listener)
this.Listener = Listener;
//點(diǎn)擊事件外露
@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());
//回調(diào)接口
public interface ParkingOnClick{
void OnClickListener(View view,int Position);
//點(diǎn)擊事件
public void setOnclick(ParkingOnClick onclick) {
this.onClick = onclick;
}應(yīng)用
解析xml數(shù)據(jù)
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);
}點(diǎn)擊事件引用
adapter.setOnclick(new ParkingAdapter.ParkingOnClick() {
@Override
public void OnClickListener(View view, int Position) {
//do anything...
}
});原作者GitHub項(xiàng)目地址
https://github.com/WangcWj/SideslippingDemo
到此這篇關(guān)于Android 側(cè)滑按鈕的文章就介紹到這了,更多相關(guān)android 側(cè)滑按鈕內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android編程實(shí)現(xiàn)在自定義對(duì)話框中獲取EditText中數(shù)據(jù)的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)在自定義對(duì)話框中獲取EditText中數(shù)據(jù)的方法,結(jié)合實(shí)例形式分析了Android對(duì)話框數(shù)據(jù)傳遞相關(guān)操作技巧,需要的朋友可以參考下2018-01-01
Android識(shí)別預(yù)裝的第三方App方法實(shí)例
這篇文章主要給大家介紹了關(guān)于Android如何識(shí)別預(yù)裝的第三方App的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01
kotlin Standard中的內(nèi)聯(lián)函數(shù)示例詳解
這篇文章主要給大家介紹了關(guān)于kotlin Standard中內(nèi)聯(lián)函數(shù)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用kotlin具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
Android 開機(jī)直接運(yùn)行app并當(dāng)做手機(jī)桌面的實(shí)例
今天小編就為大家分享一篇Android 開機(jī)直接運(yùn)行app并當(dāng)做手機(jī)桌面的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
Android 中 SwipeLayout一個(gè)展示條目底層菜單的側(cè)滑控件源碼解析
這篇文章主要介紹了Android 中 SwipeLayout一個(gè)展示條目底層菜單的側(cè)滑控件源碼解析,需要的朋友可以參考下2016-12-12
Android利用Senser實(shí)現(xiàn)不同的傳感器
這篇文章主要為大家詳細(xì)介紹了Android利用Senser實(shí)現(xiàn)不同傳感器的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02
Android指紋識(shí)別認(rèn)識(shí)和基本使用詳解
這篇文章主要為大家詳細(xì)介紹了Android指紋識(shí)別認(rèn)識(shí)和基本的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07
Android進(jìn)階NestedScroll嵌套滑動(dòng)機(jī)制實(shí)現(xiàn)吸頂效果詳解
這篇文章主要為大家介紹了Android進(jìn)階NestedScroll嵌套滑動(dòng)機(jī)制實(shí)現(xiàn)吸頂效果詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01

