SwipeLayout(https://github.com/daimajia/AndroidSwipeLayout),顾名思义,Android平台上的滑动布局,是一个可以让我们很方便使用滑动的库,典型的应用就是侧滑删除与侧滑菜单。在android的开发中运用的场景也是很多。如图所示的效果。
本篇文章主要介绍它的基本用法,目的在于能让你快速上手使用。
复制代码
1
2
1.配置
在gradle中添加以下依赖
复制代码
1compile "com.daimajia.swipelayout:library:1.2.0@aar"
复制代码
1compile 'com.android.support:recyclerview-v7:25.3.1'
2.xml文件布局 //最外层的cardview可以自己替换
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_marginTop="1dp" app:contentPadding="10dp"> <com.daimajia.swipe.SwipeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <RelativeLayout android:layout_width="wrap_content" android:layout_height="match_parent"> <TextView android:id="@+id/item_text1" android:text="删除" android:background="@color/red" android:textSize="30sp" android:textColor="@color/black" android:layout_width="wrap_content" android:layout_height="match_parent" /> <TextView android:layout_toRightOf="@+id/item_text1" android:id="@+id/item_text2" android:text="收藏" android:textSize="30sp" android:textColor="@color/black" android:background="@android:color/holo_blue_light" android:layout_width="wrap_content" android:layout_height="match_parent" /> </RelativeLayout> <RelativeLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/note_item_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="标题" android:textColor="@color/black" android:textSize="30sp"/> <TextView android:layout_below="@+id/note_item_title" android:id="@+id/note_item_abs" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="内容摘要:565656565" android:layout_marginTop="10dp" android:textSize="20sp"/> <TextView android:layout_below="@+id/note_item_abs" android:layout_marginTop="10dp" android:id="@+id/note_item_time" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="2017-05-23 15:35" android:textSize="20sp"/> </RelativeLayout> </com.daimajia.swipe.SwipeLayout> </android.support.v7.widget.CardView>
复制代码
3.构建适配器(注意要继承
RecyclerSwipeAdapter
)
1
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53import android.content.Context; import android.support.v7.widget.CardView; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import com.daimajia.swipe.adapters.RecyclerSwipeAdapter; import java.util.List; /** * Created by pc on 2017/5/26. */ public class MyRecyclerViewAdapter extends RecyclerSwipeAdapter<MyRecyclerViewAdapter.MyViewHolder> { private Context context; private List<Note> list; public MyRecyclerViewAdapter(Context context,List<Note> list){ this.context=context; this.list=list; } @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.note_item,parent,false); return new MyViewHolder(view); } @Override public void onBindViewHolder(MyViewHolder viewHolder, final int position) { Note note = list.get(position); viewHolder.note_title.setText(note.getNote_title()); viewHolder.note_content_abs.setText(note.getNote_content_abs()); viewHolder.note_time.setText(note.getNote_time()); } @Override public int getItemCount() { return list.size(); } @Override public int getSwipeLayoutResourceId(int position) { return position; } class MyViewHolder extends RecyclerView.ViewHolder{ CardView cardView; TextView note_title; TextView note_content_abs; TextView note_time; public MyViewHolder(View itemView) { super(itemView); cardView = (CardView) itemView; note_title = (TextView) itemView.findViewById(R.id.note_item_title); note_content_abs = (TextView) itemView.findViewById(R.id.note_item_abs); note_time = (TextView) itemView.findViewById(R.id.note_item_time); } } }
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41package bbh.fzu.com.yunbiji; /** * Created by pc on 2017/5/24. */ public class Note { private String note_title; private String note_content; private String note_content_abs; private String note_time; public Note(String note_title, String note_content, String note_content_abs, String note_time) { this.note_title = note_title; this.note_content = note_content; this.note_content_abs = note_content_abs; this.note_time = note_time; } public String getNote_title() { return note_title; } public void setNote_title(String note_title) { this.note_title = note_title; } public String getNote_content() { return note_content; } public void setNote_content(String note_content) { this.note_content = note_content; } public String getNote_content_abs() { return note_content_abs; } public void setNote_content_abs(String note_content_abs) { this.note_content_abs = note_content_abs; } public String getNote_time() { return note_time; } public void setNote_time(String note_time) { this.note_time = note_time; } }
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113import android.os.Bundle; import android.support.annotation.NonNull; import android.support.design.widget.NavigationView; import android.support.v4.widget.DrawerLayout; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.Toolbar; import android.view.Gravity; import android.view.Menu; import android.view.MenuItem; import java.util.ArrayList; import java.util.List; import java.util.Random; public class MainActivity extends AppCompatActivity { private DrawerLayout drawerLayout; private Note [] notes = {new Note("标题1","内容:4545454545454","摘要:5555","2017-5-23 15:22"),new Note("标题1","内容:4545454545454","摘要:5555","2017-5-23"), new Note("标题1","内容:4545454545454","摘要:5555","2017-5-23"),new Note("标题1","内容:4545454545454","摘要:5555","2017-5-23"), new Note("标题1","内容:4545454545454","摘要:5555","2017-5-23"),new Note("标题1","内容:4545454545454","摘要:5555","2017-5-23"), new Note("标题1","内容:4545454545454","摘要:5555","2017-5-23"),new Note("标题1","内容:4545454545454","摘要:5555","2017-5-23"), new Note("标题1","内容:4545454545454","摘要:5555","2017-5-23"),new Note("标题1","内容:4545454545454","摘要:5555","2017-5-23")}; private List<Note> noteList = new ArrayList<>(); private NoteAdapter noteAdapter; private MyRecyclerViewAdapter myRecyclerViewAdapter; private SwipeRefreshLayout swipeRefreshLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); inits(); } private void initNotes() {//初始化笔记 noteList.clear(); for (int i=0; i<20;i++){ Random rand = new Random(); int index = rand.nextInt(notes.length); noteList.add(notes[index]); } } private void inits() { //添加自定义标题栏 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); NavigationView nav_view = (NavigationView) findViewById(R.id.nav_view); // ActionBar actionbar = getSupportActionBar(); // actionbar.setDisplayHomeAsUpEnabled(true); // actionbar.setHomeAsUpIndicator(R.drawable.ic_menu); //侧滑菜单点击事件 nav_view.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { drawerLayout.closeDrawers();//关闭侧滑菜单 return true; } }); initNotes(); RecyclerView recycle_view = (RecyclerView) findViewById(R.id.recycler_view); GridLayoutManager layoutManager = new GridLayoutManager(this,1); recycle_view.setLayoutManager(layoutManager); //noteAdapter = new NoteAdapter(noteList); myRecyclerViewAdapter = new MyRecyclerViewAdapter(this,noteList); recycle_view.setAdapter(myRecyclerViewAdapter); //下拉刷新 swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh); swipeRefreshLayout.setColorSchemeResources(R.color.black); swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { new Thread(new Runnable() { @Override public void run() { try { Thread.sleep(2000);//休眠2秒 } catch (InterruptedException e) { e.printStackTrace(); } runOnUiThread(new Runnable() { @Override public void run() { initNotes(); myRecyclerViewAdapter.notifyDataSetChanged(); swipeRefreshLayout.setRefreshing(false);//关闭刷新提示 } }); } }).start(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) {//工具栏添加功能菜单 getMenuInflater().inflate(R.menu.toolbar,menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) {//工具栏菜单点击事件处理 switch (item.getItemId()){ case R.id.toolbar_open: drawerLayout.openDrawer(Gravity.RIGHT); break; default: break; } return true; } }
复制代码
1
复制代码
1上面的Note是填充 RecyclerView的item。
复制代码
1
复制代码
1
复制代码
1
复制代码
14.最终效果实现侧滑菜单
复制代码
1
2
![]()
复制代码
1
最后
以上就是飘逸台灯最近收集整理的关于SwipeLayout的简单使用的全部内容,更多相关SwipeLayout内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复