listView自動載入
阿新 • • 發佈:2018-11-12
public class ListViewDongtaiActivity extends AppCompatActivity implements View.OnClickListener, AbsListView.OnScrollListener {
private ListView mListView;
private int mPageIndex = 1;
private MyAdapter myAdapter;
private List<MyData.ParamzBean.FeedsBean> mList = new ArrayList<>();
private View mFootView;
private Button mLoadMore;
private ProgressBar mPro;
private int mLastIndex;
private SwipeRefreshLayout mSwipe;
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
String jsonStr = (String) msg.obj;
Gson gson = new Gson();
//反射???
MyData myData = gson.fromJson(jsonStr, MyData.class);
//給集合賦值
// mList = myData.getParamz().getFeeds();
// Log.e("sfds",mList.toString());
mList.addAll(myData.getParamz().getFeeds());
myAdapter.notifyDataSetChanged();
mSwipe.setRefreshing(false);
}
};
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initFootView();
initView();
getInfo();
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() {
mList.clear();
mPageIndex = 1;
getInfo();
}
});
}
private void initView() {
mSwipe = (SwipeRefreshLayout) findViewById(R.id.SwipeRefresh);
mListView = (ListView) findViewById(R.id.lv);
mListView.addFooterView(mFootView);
mListView.setOnScrollListener(this);
myAdapter = new MyAdapter(mList, this);
mListView.setAdapter(myAdapter);
}
//初始化底部檢視
private void initFootView() {
mFootView = getLayoutInflater().inflate(R.layout.layout, null);
mLoadMore = (Button) mFootView.findViewById(R.id.Load_More_Btn);
mPro = (ProgressBar) mFootView.findViewById(R.id.progressBar2);
mLoadMore.setOnClickListener(this);
}
private void getInfo() {
new Thread(new Runnable() {
@Override
public void run() {
try {
String s = MyOkHttp.getMyOkHttp().sendGet("http://litchiapi.jstv.com/api/GetFeeds?column=3&PageSize=10&pageIndex=" + mPageIndex + "&val=100511D3BE5301280E0992C73A9DEC41");
Message msg = new Message();
msg.obj = s;
mHandler.sendMessage(msg);
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.Load_More_Btn:
mPro.setVisibility(View.VISIBLE);
mLoadMore.setVisibility(View.GONE);
mPageIndex++;
getInfo();
break;
}
}
//滑動狀態監聽
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
Log.e("mLastIndex", mLastIndex + "條");
Log.e("myAdapter", myAdapter.getCount() + "條");
if (scrollState == 0 && myAdapter.getCount() == mLastIndex) {
mPro.setVisibility(View.GONE);
mLoadMore.setVisibility(View.VISIBLE);
}
}
//滑動時走的方法
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
mLastIndex = totalItemCount - 1;
}
}
private ListView mListView;
private int mPageIndex = 1;
private MyAdapter myAdapter;
private List<MyData.ParamzBean.FeedsBean> mList = new ArrayList<>();
private View mFootView;
private Button mLoadMore;
private ProgressBar mPro;
private int mLastIndex;
private SwipeRefreshLayout mSwipe;
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
String jsonStr = (String) msg.obj;
Gson gson = new Gson();
//反射???
MyData myData = gson.fromJson(jsonStr, MyData.class);
//給集合賦值
// mList = myData.getParamz().getFeeds();
// Log.e("sfds",mList.toString());
mList.addAll(myData.getParamz().getFeeds());
myAdapter.notifyDataSetChanged();
mSwipe.setRefreshing(false);
}
};
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initFootView();
initView();
getInfo();
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() {
mList.clear();
mPageIndex = 1;
getInfo();
}
});
}
private void initView() {
mSwipe = (SwipeRefreshLayout) findViewById(R.id.SwipeRefresh);
mListView = (ListView) findViewById(R.id.lv);
mListView.addFooterView(mFootView);
mListView.setOnScrollListener(this);
myAdapter = new MyAdapter(mList, this);
mListView.setAdapter(myAdapter);
}
//初始化底部檢視
private void initFootView() {
mFootView = getLayoutInflater().inflate(R.layout.layout, null);
mLoadMore = (Button) mFootView.findViewById(R.id.Load_More_Btn);
mPro = (ProgressBar) mFootView.findViewById(R.id.progressBar2);
mLoadMore.setOnClickListener(this);
}
private void getInfo() {
new Thread(new Runnable() {
@Override
public void run() {
try {
String s = MyOkHttp.getMyOkHttp().sendGet("http://litchiapi.jstv.com/api/GetFeeds?column=3&PageSize=10&pageIndex=" + mPageIndex + "&val=100511D3BE5301280E0992C73A9DEC41");
Message msg = new Message();
msg.obj = s;
mHandler.sendMessage(msg);
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.Load_More_Btn:
mPro.setVisibility(View.VISIBLE);
mLoadMore.setVisibility(View.GONE);
mPageIndex++;
getInfo();
break;
}
}
//滑動狀態監聽
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
Log.e("mLastIndex", mLastIndex + "條");
Log.e("myAdapter", myAdapter.getCount() + "條");
if (scrollState == 0 && myAdapter.getCount() == mLastIndex) {
mPro.setVisibility(View.GONE);
mLoadMore.setVisibility(View.VISIBLE);
}
}
//滑動時走的方法
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
mLastIndex = totalItemCount - 1;
}
}