Android UI Libs之AndroidSwipeLayout

Android UI Libs之AndroidSwipeLayout


1. 說明


AndroidSwipeLayout,顧名思義,Android平臺上的滑動布局,是一個可以讓我們很方便使用滑動的庫,典型的應(yīng)用就是側(cè)滑刪除與側(cè)滑菜單。

2. 配置


在模塊中添加依賴:compile "com.daimajia.swipelayout:library:1.2.0@aar"

3.基本使用


1. Show Mode

  • LayDown


  • PullOut

2. Drag edge

  • LEFT
  • RIGHT
  • TOP
  • BOTTOM

3. 在xml布局文件中使用SwipeLayout

  • SwipeLayout的最后一個孩子是SurfaceView,其他孩子都是BottomView
  • BottomView最好添加上andorid:layout_gravity屬性
 <com.daimajia.swipe.SwipeLayout
        android:layout_width="match_parent"
        android:layout_height="80dp">
        <!-- Bottom View Start-->
        <LinearLayout
            android:background="#66ddff00"
            android:id="@+id/bottom_wrapper"
            android:layout_width="160dp"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <!--What you want to show-->
            <TextView
                android:text="歡迎關(guān)注我的微信公眾號:Android技術(shù)漫談"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>
        <!-- Bottom View End-->

        <!-- Surface View Start -->
        <LinearLayout
            android:padding="10dp"
            android:background="#ffffff"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <!--What you want to show in SurfaceView-->
            <TextView
                android:text="歡迎關(guān)注我的github:lavor-zl"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>
        <!-- Surface View End -->
    </com.daimajia.swipe.SwipeLayout>

4. 在java文件中對SwipeLayout進行一些相關(guān)操作

SwipeLayout swipeLayout =  (SwipeLayout)findViewById(R.id.sample1);

//set show mode.
swipeLayout.setShowMode(SwipeLayout.ShowMode.LayDown);

//add drag edge.(If the BottomView has 'layout_gravity' attribute, this line is unnecessary)
swipeLayout.addDrag(SwipeLayout.DragEdge.Left, findViewById(R.id.bottom_wrapper));

swipeLayout.addSwipeListener(new SwipeLayout.SwipeListener() {
            @Override
            public void onClose(SwipeLayout layout) {
                //when the SurfaceView totally cover the BottomView.
            }

            @Override
            public void onUpdate(SwipeLayout layout, int leftOffset, int topOffset) {
               //you are swiping.
            }

            @Override
            public void onStartOpen(SwipeLayout layout) {

            }

            @Override
            public void onOpen(SwipeLayout layout) {
               //when the BottomView totally show.
            }

            @Override
            public void onStartClose(SwipeLayout layout) {

            }

            @Override
            public void onHandRelease(SwipeLayout layout, float xvel, float yvel) {
               //when user's hand released.
            }
        });  

4. 高級使用


我們經(jīng)常需要在ListView,RecyclerView等中使用側(cè)滑刪除或者側(cè)滑菜單,那么我們?nèi)绾卫肁ndroidSwipeLayout來實現(xiàn)呢。

Adapter的定義如下:

public class MyRecyclerViewAdapter extends RecyclerSwipeAdapter<MyRecyclerViewAdapter.MyViewHolder>{
    private Context context;
    private List<String> list;
    public MyRecyclerViewAdapter(Context context, List<String> list) {
        this.context = context;
        this.list = list;
    }
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder viewHolder, final int position) {
        viewHolder.swipeLayout.setShowMode(SwipeLayout.ShowMode.LayDown);
        viewHolder.surface.setText(list.get(position));
        viewHolder.bottom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                list.remove(position);
                notifyDataSetChanged();
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public int getSwipeLayoutResourceId(int position) {
        return position;
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder{
        private SwipeLayout swipeLayout;
        private Button bottom;
        private TextView surface;
        public MyViewHolder(View itemView) {
            super(itemView);
            swipeLayout=(SwipeLayout)itemView.findViewById(R.id.swipe_layout);
            bottom=(Button)itemView.findViewById(R.id.bottom);
            surface=(TextView)itemView.findViewById(R.id.surface);
        }
    }
}

adapter.xml的內(nèi)容:

<com.daimajia.swipe.SwipeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipe_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <!-- Bottom View Start-->
    <LinearLayout
        android:background="#66ddff00"
        android:id="@+id/bottom_wrapper"
        android:layout_width="160dp"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <!--What you want to show-->
        <Button
            android:id="@+id/bottom"
            android:text="刪除"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
    <!-- Bottom View End-->

    <!-- Surface View Start -->
    <LinearLayout
        android:padding="10dp"
        android:background="#ffffff"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <!--What you want to show in SurfaceView-->
        <Button
            android:id="@+id/surface"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
    <!-- Surface View End -->
</com.daimajia.swipe.SwipeLayout>  

RecyclerView相關(guān)設(shè)置

 List<String> list=new ArrayList<>();
        for(int i=0;i<40;i++){
             list.add("歡迎關(guān)注我的github:lavor-zl:"+(i+1));
        }
        MyRecyclerViewAdapter myRecyclerViewAdapter=new MyRecyclerViewAdapter(this,list);
        recyclerview.setLayoutManager(new LinearLayoutManager(this));
        recyclerview.setAdapter(myRecyclerViewAdapter);  

這些代碼基本上實現(xiàn)了側(cè)滑刪除,程序運行界面如下


繼承RecyclerSwipeAdapterAdapter比繼承RecyclerView.AdapterAdapter多了很多方法,常用的有下面幾個:

  • openItem(int position):打開某個item的側(cè)滑
  • closeItem(int position):關(guān)閉某個打開的側(cè)滑
  • getOpenItems():獲取所有打開的item
  • isOpen(int position):判斷某個位置的item是否打開側(cè)滑
  • getMode():獲取側(cè)滑顯示模式
  • setMode(Attributes.Mode mode):設(shè)置側(cè)滑顯示模式

同理可以操作ListView等,操作ListView時可以讓Adapter繼承BaseSwipeAdapter

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容