添加一个jar包 v7_recyleview
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
compile 'com.android.support:recyclerview-v7:26.0.0-alpha1' compile 'com.google.code.gson:gson:2.8.1' compile 'com.squareup.okhttp3:okhttp:3.8.1'
MainActivity:
public class MainActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener { private SwipeRefreshLayout swipe; private RecyclerView recyclerView; private LinearLayoutManager linearLayoutManager; private RecyleAdapter recyleAdapter; private ProgressBar pro; private boolean isRefresh=true; private List<Bean.ResultBean.DataBean> list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initview(); initData(); } private void initData() { swipe = (SwipeRefreshLayout) findViewById(R.id.swipe); pro = (ProgressBar) findViewById(R.id.pro); recyclerView = (RecyclerView) findViewById(R.id.recy); recyleAdapter = new RecyleAdapter(this); linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false); recyclerView.setLayoutManager(linearLayoutManager); recyclerView.setAdapter(recyleAdapter); recyleAdapter.setOnClick(new RecyleAdapter.onClick() { @Override public void onCli(View v, int position) { Toast.makeText(MainActivity.this,"点击了"+position,Toast.LENGTH_LONG).show(); } }); swipe.setEnabled(true); swipe.setOnRefreshListener(this); swipe.setColorSchemeColors(Color.BLUE,Color.RED,Color.GREEN); final boolean[] isLoadMore = {false}; recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) { super.onScrollStateChanged(recyclerView, newState); } @Override public void onScrolled(final RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); if (dy>0) { int lastVisibleItemPosition = linearLayoutManager.findLastVisibleItemPosition(); int itemCount = linearLayoutManager.getItemCount(); if (lastVisibleItemPosition + 1 == itemCount && !isLoadMore[0]) { isLoadMore[0] = true; pro.setVisibility(View.VISIBLE); new Thread() { @Override public void run() { super.run(); SystemClock.sleep(5000); isRefresh=false; recyleAdapter.setData(list.subList(0,30),isRefresh); runOnUiThread(new Runnable() { @Override public void run() { isLoadMore[0] = false; Toast.makeText(MainActivity.this,list.size() +"" ,Toast.LENGTH_SHORT).show(); recyleAdapter.notifyDataSetChanged(); pro.setVisibility(View.GONE); } }); } }.start(); } } } }); } private void initview() { OkHttpClient okHttpClient = new OkHttpClient(); Request request = new Request.Builder().url("http://result.eolinker.com/k2BaduF2a6caa275f395919a66ab1dfe4b584cc60685573?uri=gj").build(); Call call = okHttpClient.newCall(request); call.enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { if (response.code() == 200) { String str = response.body().string(); Log.d("haha",""+str); Gson gson = new Gson(); Bean bean = gson.fromJson(str, Bean.class); list = bean.result.data; runOnUiThread(new Runnable() { @Override public void run() { isRefresh=true; recyleAdapter.setData(list,isRefresh); recyleAdapter.notifyDataSetChanged(); swipe.setRefreshing(false); } }); } else { Log.d("myMessage", "" + response.message()); } } }); } @Override public void onRefresh() { initview(); } }
适配器类:
public class RecyleAdapter extends RecyclerView.Adapter { private List<Bean.ResultBean.DataBean> list=new ArrayList<>(); private Context mcontext; public RecyleAdapter (Context context){ this.mcontext=context; } public void setData(List<Bean.ResultBean.DataBean> data,boolean isRefresh){ if (isRefresh){ this.list=data; }else{ this.list.addAll(data); } } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = View.inflate(mcontext, R.layout.home_item, null); MyHolderView myHolderView = new MyHolderView(view); return myHolderView; } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) { MyHolderView my= (MyHolderView) holder; my.textView.setText(list.get(position).title); my.textView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { monClick.onCli(v,position); } }); } @Override public int getItemCount() { return list!=null ?list.size():0; } class MyHolderView extends RecyclerView.ViewHolder{ private final ImageView imageView; private final TextView textView; public MyHolderView(View itemView) { super(itemView); imageView = (ImageView) itemView.findViewById(R.id.imageView); textView = (TextView) itemView.findViewById(R.id.textView); } } public interface onClick{ void onCli(View v,int position); } private onClick monClick; public void setOnClick(onClick onClick){ this.monClick=onClick; } }
自定义画线:
public class MyDividerItemDecoration extends RecyclerView.ItemDecoration { private Context mcontext; private final int widthPixels; public MyDividerItemDecoration(Context context){ this.mcontext=context; DisplayMetrics dm = new DisplayMetrics(); ((Activity)mcontext).getWindowManager().getDefaultDisplay().getMetrics(dm); widthPixels = dm.widthPixels; System.out.println("width_dispaly"+dm.widthPixels); System.out.println("height_dispaly"+dm.heightPixels); } @Override public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) { super.onDraw(c, parent, state); Paint paint = new Paint(); paint.setColor(Color.RED); //圆形在条目下面 // c.drawCircle(200,200,200,paint); //矩形 // RectF rectF = new RectF(0,100,1000,110); // c.drawRect(rectF,paint); int childCount = parent.getChildCount(); for (int i = 0; i <childCount ; i++) { View childAt = parent.getChildAt(i); int bottom = childAt.getBottom(); int left = childAt.getLeft(); int right = childAt.getRight(); c.drawRect(left,bottom,widthPixels,bottom+10,paint); } } @Override public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) { super.onDrawOver(c, parent, state); //圆形在条目上面 // Paint paint = new Paint(); // paint.setColor(Color.BLUE); // c.drawCircle(300,300,300,paint); } }