packageweizexu.baway.com.monthtests;
public classMainActivityextendsAppCompatActivity {
privateListlist=newArrayList<>();
privateRecyclerViewlistviews;
privateAdapteradapter;
privateSwipeRefreshLayoutsx;
private intbl=1;
privateTextViewtest;
Handlerhandler=newHandler(){
@Override
public voidhandleMessage(Message msg) {
super.handleMessage(msg);
getData(++bl);
list.clear();
list.addAll(0,list);
sx.setRefreshing(false);
if(bl>=8)
{
Toast.makeText(MainActivity.this,"接口id最大为8其他无值,已经刷完了,亲!", Toast.LENGTH_SHORT).show();
}
adapter.notifyDataSetChanged();
}
};
@Override
protected voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listviews= (RecyclerView) findViewById(R.id.listviews);
sx= (SwipeRefreshLayout) findViewById(R.id.sx);
getData(bl);
getstate();
//test.setText("lkll");
// final LinearLayoutManager la = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
// listviews.setLayoutManager(la);
StaggeredGridLayoutManager st =newStaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
listviews.setLayoutManager(st);
adapter=newAdapter(list, MainActivity.this);
listviews.setAdapter(adapter);
adapter.notifyDataSetChanged();
sx.setOnRefreshListener(newSwipeRefreshLayout.OnRefreshListener() {
@Override
public voidonRefresh() {
handler.sendEmptyMessageDelayed(2000,0);
}
});
listviews.addOnScrollListener(newRecyclerView.OnScrollListener() {
@Override
public voidonScrollStateChanged(RecyclerView recyclerView,intnewState) {
super.onScrollStateChanged(recyclerView, newState);
booleana =isSlideToBottom(listviews);
if(a) {
list.addAll(0,list);
Toast.makeText(MainActivity.this,"加载成功", Toast.LENGTH_SHORT).show();
adapter.notifyDataSetChanged();
}else{
}
}
});
Logger.addLogAdapter(newAndroidLogAdapter());
Logger.d("debug");
Logger.e("error");
Logger.w("warning");
Logger.v("verbose");
Logger.i("information");
Logger.wtf("wtf!!!!");
FormatStrategy formatStrategy = CsvFormatStrategy.newBuilder()
.tag("custom")
.build();
Logger.addLogAdapter(newDiskLogAdapter(formatStrategy));
SpacesItemDecoration decoration=newSpacesItemDecoration(getApplicationContext());
listviews.addItemDecoration(decoration);
test.setText("Sssss");
}
private voidgetstate() {
booleanb = checkNet();
if(!b) {
// 使用对话框判断是否可以操作
AlertDialog.Builder builder =newAlertDialog.Builder(this);
builder.setMessage("是否去设置网络");
builder.setPositiveButton("确定",newDialogInterface.OnClickListener() {
@Override
public voidonClick(DialogInterface dialog,intwhich) {
// 使用隐士跳转自动跳转到设置网络界面
Toast.makeText(MainActivity.this,"设置网络!", Toast.LENGTH_LONG).show();
Intent intent =newIntent("android.settings.WIRELESS_SETTINGS");
startActivity(intent);
return;
}
});
builder.setNegativeButton("取消",newDialogInterface.OnClickListener() {
@Override
public voidonClick(DialogInterface dialog,intwhich) {
Toast.makeText(MainActivity.this,"查看本地", Toast.LENGTH_LONG).show();
}
});
builder.create();
builder.show();
}else{
Toast.makeText(this,"网络正处于连接中", Toast.LENGTH_LONG).show();
}
}
private voidgetData(intbl) {
String url ="http://www.yulin520.com/a2a/impressApi/news/mergeList?sign=C7548DE604BCB8A17592EFB9006F9265&pageSize=20&gender=2&ts=1871746850&page="+bl;
Httputils httputils =newHttputils();
httputils.Senthelp(url,newCallback() {
@Override
public voidonFailure(Call call, IOException e) {
}
@Override
public voidonResponse(Call call, Response response)throwsIOException {
Months months =newGson().fromJson(response.body().string(), Months.class);
list.addAll(months.getData());
runOnUiThread(newRunnable() {
@Override
public voidrun() {
adapter.notifyDataSetChanged();
}
});
}
});
}
public static booleanisSlideToBottom(RecyclerView listviews) {
if( listviews==null) {
return false;
}
if(listviews.computeVerticalScrollExtent() + listviews.computeVerticalScrollOffset() >= listviews.computeVerticalScrollRange()) {
return true;
}
return false;
}
private booleancheckNet() {
ConnectivityManager conn = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo net = conn.getActiveNetworkInfo();
if(net !=null&& net.isConnected()) {
return true;
}
return false;
}
}