项目展示
项目依赖
api 'com.android.support:recyclerview-v7:28.0.0' api 'com.android.support:design:28.0.0' api 'com.google.code.gson:gson:2.2.4' api 'com.squareup.picasso:picasso:2.3.2' api 'com.squareup.okhttp3:okhttp:3.2.0'
接口地址出自 干货集中营
在做这个之前,先写一个bean类
public class Meizi {
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
private String url;
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
private int page;
}
接下来是主页面和是适配器,我写在了一起
public class LineActivity extends AppCompatActivity {
private static RecyclerView recyclerview;
private CoordinatorLayout coordinatorLayout;
private MyAdapter mAdapter;
private List<Meizi> meizis;
private LinearLayoutManager mLayoutManager;
private int lastVisibleItem;
private int page = 1;//默认加载页
private ItemTouchHelper itemTouchHelper;
private int screenwidth;
private SwipeRefreshLayout swipeRefreshLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_line);
initView();
setListener();
new GetData().execute("http://gank.io/api/data/福利/10/1");
//获取屏幕宽度
WindowManager wm = (WindowManager) LineActivity.this
.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics outMetrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(outMetrics);
screenwidth = outMetrics.widthPixels;
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus && Build.VERSION.SDK_INT >= 19) {
View decorView = getWindow().getDecorView();
decorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
}
}
private void initView() {
coordinatorLayout = (CoordinatorLayout) findViewById(R.id.line_coordinatorLayout);
recyclerview = (RecyclerView) findViewById(R.id.line_recycler);
mLayoutManager = new LinearLayoutManager(this);
recyclerview.setLayoutManager(mLayoutManager);
swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.line_swipe_refresh);
swipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary, R.color.colorPrimaryDark, R.color.colorAccent);
swipeRefreshLayout.setProgressViewOffset(false, 0, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24, getResources().getDisplayMetrics()));
}
private void setListener() {
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
page = 1;
new GetData().execute("http://gank.io/api/data/福利/10/1");
}
});
itemTouchHelper = new ItemTouchHelper(new ItemTouchHelper.Callback() {
@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHold