//在使用WaveSwipeRefreshLayout之前得添加依赖
repositories { maven { url "https://jitpack.io" } }
compile 'com.github.recruit-lifestyle:WaveSwipeRefreshLayout:1.6'
//RecyclerView依赖
compile 'com.android.support:recyclerview-v7:26.+'
//这个RecyclerView依赖也是根据你自己的版本来更改的
//自己的main布局
xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent" tools:context="zhoukao.demo01.com.shuaxin.MainActivity"> <jp.co.recruit_lifestyle.android.widget.WaveSwipeRefreshLayout android:id="@+id/wave" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.RecyclerView android:id="@+id/recycler" android:layout_width="match_parent" android:layout_height="wrap_content">android.support.v7.widget.RecyclerView> jp.co.recruit_lifestyle.android.widget.WaveSwipeRefreshLayout> LinearLayout>
//接下来开始实现吧
import android.graphics.Color; import android.os.Bundle; import android.os.Handler; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.StaggeredGridLayoutManager; import java.io.IOException; import java.util.List; import jp.co.recruit_lifestyle.android.widget.WaveSwipeRefreshLayout; import okhttp3.Call; import zhoukao.demo01.com.shuaxin.utils.GsonObjectCallback; import zhoukao.demo01.com.shuaxin.utils.OkHttp3Utils; public class MainActivity extends AppCompatActivity { private RecyclerView recyclerView; private WaveSwipeRefreshLayout mWaveSwipeRefreshLayout; private Handler handler = new Handler(); private StaggeredGridLayoutManager staggeredGridLayoutManager; private LinearLayoutManager layoutManager; private MyAdapter myAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //找控件 recyclerView = (RecyclerView) findViewById(R.id.recycler); getData(); // staggeredGridLayoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL); //布局管理器 layoutManager = new LinearLayoutManager(this); recyclerView.setLayoutManager(layoutManager); mWaveSwipeRefreshLayout = (WaveSwipeRefreshLayout) findViewById(R.id.wave); //设置中间小圆从白色到黑色 mWaveSwipeRefreshLayout.setColorSchemeColors(Color.WHITE, Color.BLACK); //设置整体的颜色 mWaveSwipeRefreshLayout.setWaveColor(Color.argb(255, 255, 0, 0)); //下拉刷新 mWaveSwipeRefreshLayout.setOnRefreshListener(new WaveSwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { handler.postDelayed(new Runnable() { @Override public void run() { OkHttp3Utils.doGet("http://gank.io/api/data/%E7%A6%8F%E5%88%A9/10/1", new GsonObjectCallback//所用到的Adapter() { @Override public void onUi(DataBean dataBean) { List list = dataBean.getResults(); myAdapter.refreshMore(list); } @Override public void onFailed(Call call, IOException e) { } }); //三秒后停止刷新 mWaveSwipeRefreshLayout.setRefreshing(false); } },3000); } }); //上拉加载 recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener(){ private int lastPosition; @Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) { super.onScrollStateChanged(recyclerView, newState); if(lastPosition+1==myAdapter.getItemCount()&&newState==RecyclerView.SCROLL_STATE_IDLE){ OkHttp3Utils.doGet("http://gank.io/api/data/%E7%A6%8F%E5%88%A9/10/1", new GsonObjectCallback () { @Override public void onUi(DataBean dataBean) { List list = dataBean.getResults(); myAdapter.loadMore(list); } @Override public void onFailed(Call call, IOException e) { } }); } } @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); lastPosition = layoutManager.findLastVisibleItemPosition(); } }); } private void getData(){ OkHttp3Utils.doGet("http://gank.io/api/data/%E7%A6%8F%E5%88%A9/10/1", new GsonObjectCallback () { @Override public void onUi(DataBean dataBean) { List list = dataBean.getResults(); if(myAdapter==null){ myAdapter = new MyAdapter(MainActivity.this,list); recyclerView.setAdapter(myAdapter); }else { myAdapter.notifyDataSetChanged(); } } @Override public void onFailed(Call call, IOException e) { } }); } }
import android.content.Context; import android.support.v7.widget.RecyclerView; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import com.squareup.picasso.Picasso; import java.util.List; public class MyAdapter extends RecyclerView.Adapter{ private Context context; private List list; public MyAdapter(Context context, List list) { this.context = context; this.list = list; } @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = View.inflate(context, R.layout.item, null); MyViewHolder myViewHolder = new MyViewHolder(view); return myViewHolder; } @Override public void onBindViewHolder(MyViewHolder holder, int position) { ViewGroup.LayoutParams params = holder.iv_recy.getLayoutParams(); if(position == 0){ params.height = 200; }else{ params.height = 400; } holder.iv_recy.setLayoutParams(params); Picasso.with(holder.iv_recy.getContext()).load(list.get(position).getUrl()).into(holder.iv_recy); holder.tv_recy.setText(list.get(position).getType()); } @Override public int getItemCount() { return list.size(); } public class MyViewHolder extends RecyclerView.ViewHolder{ ImageView iv_recy; TextView tv_recy; public MyViewHolder(View itemView) { super(itemView); iv_recy = (ImageView) itemView.findViewById(R.id.iv_recy); tv_recy = (TextView) itemView.findViewById(R.id.tv_recy); } } public void loadMore(List li){ for (DataBean.ResultsBean str : li){ list.add(str); } //更新界面 notifyDataSetChanged(); } public void refreshMore(List li){ for (DataBean.ResultsBean str : li){ list.add(0,str); } //更新界面 notifyDataSetChanged(); } }
//其中item的布局
xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent"> <ImageView android:id="@+id/iv_recy" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/tv_recy" android:layout_width="wrap_content" android:layout_height="wrap_content" /> LinearLayout>
//其中用到的接口 可以直接在网上去获取在这里就不给演示了
//其中用到了OKHttp3的一个方法,这个需要自己去封装 你可以使用HttpURLconntion去实现网络请求
在这里不给一一实现了