public class MainActivity extends AppCompatActivity implements View.OnClickListener, AbsListView.OnScrollListener {
private ListView lv;
private int mPageIndex = 1;
private View mFootView;
private MyAdapter myAdapter;
private List<Mybean.ParamzBean.FeedsBean> feeds= new ArrayList<>();
private Button foot;
private int count;
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
String str = (String) msg.obj;
Gson gson = new Gson();
Mybean mybean = gson.fromJson(str, Mybean.class);
feeds.addAll(mybean.getParamz().getFeeds());
myAdapter.notifyDataSetChanged();
mswipe.setRefreshing(false);
}
};
private SwipeRefreshLayout mswipe;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
下拉刷新----->
mswipe.setProgressBackgroundColorSchemeResource(android.R.color.white);
// 设置下拉进度的主题颜色
mswipe.setColorSchemeResources(R.color.colorAccent, R.color.colorPrimary, R.color.colorPrimaryDark);
mswipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
feeds.clear();
mPageIndex =1;
initData();
}
});
}
获取网络数据---->
private void initData() {
new Thread(new Runnable() {
@Override
public void run() {
try {
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder().url("http://litchiapi.jstv.com/api/GetFeeds?column=3&PageSize=10&pageIndex=" + mPageIndex + "&val=100511D3BE5301280E0992C73A9DEC41").build();
Response response = okHttpClient.newCall(request).execute();
String string = response.body().string();
Log.e("aaaaaa",string);
handler.obtainMessage(100,string).sendToTarget();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
将适配器实例化,添加尾布局
private void initView() {
lv = (ListView) findViewById(R.id.lv);
mswipe = findViewById(R.id.swiperefresh);
myAdapter = new MyAdapter(MainActivity.this,feeds);
mFootView = LayoutInflater.from(this).inflate(R.layout.weibuju, null);
foot = (Button) mFootView.findViewById(R.id.weibu);
foot.setVisibility(View.GONE);
lv.setOnScrollListener(this);
foot.setOnClickListener(this);
lv.addFooterView(mFootView);
lv.setAdapter(myAdapter);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.weibu:
mPageIndex++;
initData();
break;
}
}
ListView的滑动监听
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (scrollState==0&&myAdapter.getCount()==count){
foot.setVisibility(View.VISIBLE);
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
count = totalItemCount-1;
}
}
网友评论