1.导入依赖
compile 'com.jcodecraeer:xrecyclerview:1.3.2'
2,权限配置
<uses-permission android:name="android.permission.INTERNET"></uses-permission> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
3,activity_main中的使用
<com.jcodecraeer.xrecyclerview.XRecyclerView android:layout_width="match_parent" android:id="@+id/xre_xrv" android:layout_height="match_parent"> </com.jcodecraeer.xrecyclerview.XRecyclerView>
4。mainActivity中的使用
package com.bwie.xrecyclerview_lianxi; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import android.widget.Toast; import com.jcodecraeer.xrecyclerview.XRecyclerView; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { String mpath="http://result.eolinker.com/umIPmfS6c83237d9c70c7c9510c9b0f97171a308d13b611?uri=homepage"; XRecyclerView xrv_xrv; // private RecyclerView mRecyclerView; private List<String> mDatas; private HomeAdapter mAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); xrv_xrv = (XRecyclerView) findViewById(R.id.xre_xrv); initData(); xrv_xrv.setLayoutManager(new LinearLayoutManager(this)); xrv_xrv.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST)); xrv_xrv.setAdapter(mAdapter = new HomeAdapter()); xrv_xrv.setLoadingListener(new XRecyclerView.LoadingListener() { @Override public void onRefresh() { Toast.makeText(MainActivity.this,"加载页面",Toast.LENGTH_LONG).show(); xrv_xrv.refreshComplete(); } @Override public void onLoadMore() { mDatas.add("下拉加载"); mDatas.add("下拉加载"); mDatas.add("下拉加载"); mDatas.add("下拉加载"); mDatas.add("下拉加载"); mDatas.add("下拉加载"); mDatas.add("下拉加载"); mDatas.add("下拉加载"); mDatas.add("下拉加载"); mDatas.add("下拉加载"); mDatas.add("下拉加载"); mDatas.add("下拉加载"); mDatas.add("下拉加载"); xrv_xrv.loadMoreComplete(); } }); } protected void initData() { mDatas = new ArrayList<String>(); for (int i = 'A'; i < 'z'; i++) { mDatas.add("" + (char) i); } } class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.MyViewHolder> { @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { MyViewHolder holder = new MyViewHolder(LayoutInflater.from( MainActivity.this).inflate(R.layout.item_home, parent, false)); return holder; } @Override public void onBindViewHolder(MyViewHolder holder, int position) { holder.tv.setText(mDatas.get(position)); } @Override public int getItemCount() { return mDatas.size(); } class MyViewHolder extends RecyclerView.ViewHolder { TextView tv; public MyViewHolder(View view) { super(view); tv = (TextView) view.findViewById(R.id.id_num); } } } }
5.item_home布局文件
<TextView android:id="@+id/id_num" android:layout_width="match_parent" android:layout_height="50dp" android:gravity="center" android:text="1" />