一. 运行效果以及源码
1.gif图效果

2.源码地址
PictureShow
二.主要的思路
使用RecyclerView显示数据,通过使用CardView让页面美观。使用系统提供的 SwipeRefreshLayout 实现刷新。
三. 使用到的技术点
1. RecyclerView
参考:RecyclerView
2. CardView
在 Item 最外层包裹一个 CardView,并且设置了两个 CardView 经常使用的属性。
一个是 cardElevation 指的是 CardView 的一个高度,可以这样想象,RecyclerView 是一个平面,而 CardView 就在它的上面,我们可以通过设置 cardElevation 来控制这个高度,也就是 CardView 在这样的高度时的一个阴影效果,高度越高,阴影越明显。
还有一个属性是 cardCornerRadius 指的是 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
| <?xml version="1.0" encoding="utf-8"?> <androidx.cardview.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" app:cardCornerRadius="12dp" app:cardElevation="4dp" android:layout_margin="8dp" >
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="8dp" > <ImageView android:id="@+id/friend_icon" android:layout_width="150dp" android:layout_height="150dp" android:src="@drawable/ic_launcher_foreground" android:scaleType="fitXY" />
<LinearLayout android:layout_marginStart="20dp" android:layout_width="0dp" android:layout_height="match_parent" android:orientation="vertical" android:layout_weight="1" android:gravity="center_vertical" >
<TextView android:layout_marginTop="15dp" android:id="@+id/friend_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textStyle="bold" android:textSize="25sp" android:text="姓名" />
<TextView android:layout_marginTop="10dp" android:id="@+id/friend_mode" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:text="动态" />
</LinearLayout> </LinearLayout>
</androidx.cardview.widget.CardView>
|
3. SwipeRefreshLayout
① 简单的使用
SwipeRefreshLayout 必须是 RecyclerView 的父容器,也就是 SwipeRefreshLayout 包裹 RecyclerView。
SwipeRefreshLayout 要刷新的话,需要添加一个监听事件:
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
| public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View inflate = inflater.inflate(R.layout.grid_layout, null);
final SwipeRefreshLayout refresh = inflate.findViewById(R.id.refresh); final RecyclerView recycler = inflate.findViewById(R.id.recycler); refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { DataManager.getDataManager(getContext()).reloadData();
RecyclerView.Adapter adapter = recycler.getAdapter(); adapter.notifyDataSetChanged();
new Handler().postDelayed(new Runnable() { @Override public void run() { if (refresh.isRefreshing()) { refresh.setRefreshing(false); } } }, 500); } });
return inflate; }
|
② 常用的方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| swipeRefreshLayout.setColorSchemeColors(Color.BLUE, Color.RED, Color.GREEN);
swipeRefreshLayout.setDistanceToTriggerSync(200);
swipeRefreshLayout.setSlingshotDistance(400);
swipeRefreshLayout.setRefreshing(true);
swipeRefreshLayout.isRefreshing();
|