首先,上成功之后的效果图
接下来从gradle导入
compile 'com.jcodecraeer:xrecyclerview:1.3.2'
然后上布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.zhangshuyang01.coupondemo.MainActivity" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="优惠券Demo" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <RelativeLayout android:id="@+id/rl_empty" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:src="@mipmap/wallet_no_coupon" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="130dp" android:visibility="gone" android:text="邀请好友进行分享" /> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="-10dp" android:background="#f5f5f5"> <com.jcodecraeer.xrecyclerview.XRecyclerView android:id="@+id/xrv_list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:scrollbarSize="0.5dp" android:scrollbars="vertical" /> </RelativeLayout> </LinearLayout> </LinearLayout>
adapter中item的布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/ll_item" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="6dp" android:orientation="vertical"> <RelativeLayout android:layout_width="match_parent" android:layout_height="110dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:background="@mipmap/user_wallet_coupon"> <LinearLayout android:id="@+id/rl_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_centerVertical="true" android:layout_marginLeft="35dp"> <TextView android:id="@+id/tv_use_time1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="2" android:includeFontPadding="false" android:textColor="#333333" android:textSize="35sp"/> <TextView android:layout_alignParentBottom="true" android:id="@+id/tv_unit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:includeFontPadding="false" android:layout_marginLeft="10dp" android:layout_toRightOf="@id/tv_use_time1" android:text="小时" android:textColor="#333333" android:textSize="13sp"/> </LinearLayout> <View android:id="@+id/line" android:layout_width="0.5dp" android:layout_height="match_parent" android:layout_marginBottom="18dp" android:layout_marginLeft="18dp" android:layout_marginTop="18dp" android:layout_toRightOf="@id/rl_time" android:background="#dcdcdc"/> <RelativeLayout android:id="@+id/rl_text" android:layout_width="wrap_content" android:layout_height="60dp" android:layout_centerVertical="true" android:layout_marginLeft="18dp" android:layout_toRightOf="@+id/line"> <TextView android:id="@+id/tv_coupon_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用车券" android:textColor="#333333" android:textSize="24sp"/> <RelativeLayout android:id="@+id/rl_bottom" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true"> <TextView android:id="@+id/tv_coupon_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:text="有效期至2017-01-10" android:textColor="#666666" android:textSize="13sp"/> </RelativeLayout> </RelativeLayout> </RelativeLayout> </LinearLayout>
主程序中的代码
public class MainActivity extends AppCompatActivity { private XRecyclerView xRecyclerView; private RelativeLayout rl_empty; private int page=1,total_page,isOnRefresh=1; private ArrayList<Map<String,String>> mapList ; private MyCouponAdapter mAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); xRecyclerView = (XRecyclerView) findViewById(R.id.xrv_list); rl_empty = (RelativeLayout) findViewById(R.id.rl_empty); init(); LinearLayoutManager layoutManager = new LinearLayoutManager(this); layoutManager.setOrientation(LinearLayoutManager.VERTICAL); xRecyclerView.setLayoutManager(layoutManager); xRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader); xRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.Pacman); xRecyclerView.setEmptyView(rl_empty); xRecyclerView.setLoadingMoreEnabled(true); xRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() { @Override public void onRefresh() { page=1; isOnRefresh=2; } @Override public void onLoadMore() { if (total_page-page>0){ page++; }else { xRecyclerView.loadMoreComplete(); } } }); mAdapter = new MyCouponAdapter(mapList); xRecyclerView.setAdapter(mAdapter); } public void init() { mapList = new ArrayList<>(); for (int i = 1; i < 10; i++) { Map<String, String> maps = new HashMap<>(); maps.put("number", ""+i); maps.put("unit", ""+i); maps.put("description", "描述"+i); maps.put("failtrue_time", "2018/"+i); mapList.add(maps); } } }
大功告成!有更好的改进方法或者错误的地方,请大家指出,谢谢~
github上有源码,欢迎下载
- https://github.com/beibeiMary