Android中获取网络数据时的分页加载
Posted 巫山老妖
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android中获取网络数据时的分页加载相关的知识,希望对你有一定的参考价值。
//此实在Fragment中实现的,黄色部分为自动加载,红色部分是需要注意的和手动加载, 蓝色部分是睡眠时间,自我感觉不用写 ,还有就是手动加载时,不知道为什么进去后显示的就是最后一行,求大神指教
public class Fragment1 extends Fragment{
//加载的第几页
private int index = 0;
private List<News> news=new ArrayList<News>();
private ListView lv;
private MyAdapter adapter;
private String urlPath="http://www.93.gov.cn/93app/data.do?channelId=0&startNum=";
//使用Handler刷新UI
private Handler handler=new Handler(){
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case 0:
//清空集合
//刷新适配器
adapter.notifyDataSetChanged();
//获得消息数据
String text=(String) msg.obj;
//解析json串
tojson(text);
break;
default:
break;
}
}
};
//解析json串
private void tojson(String text) {
try {
JSONObject obj=new JSONObject(text);
JSONArray data=obj.getJSONArray("data");
for (int i = 0; i < data.length(); i++) {
JSONObject json=data.getJSONObject(i);
String ID=json.getString("ID");
String TITLE=json.getString("TITLE");
String SUBTITLE=json.getString("SUBTITLE");
String IMAGEURL=json.getString("IMAGEURL");
String FROMNAME=json.getString("FROMNAME");
String SHOWTIME=json.getString("SHOWTIME");
String RN=json.getString("RN");
//添加到集合中去
News newss=new News(ID, TITLE, SUBTITLE, IMAGEURL, FROMNAME, SHOWTIME, RN);
news.add(newss);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//找到对应的布局文件
View view=inflater.inflate(R.layout.frgment_item1, null);
//找到Listview控件
lv = (ListView) view.findViewById(R.id.lv);
View v=LayoutInflater.from(getActivity()).inflate(R.layout.button_footer, null);
lv.addFooterView(v);
Button button=(Button) v.findViewById(R.id.bt_button);
/*lv.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (scrollState==OnScrollListener.SCROLL_STATE_IDLE) {
if (view.getLastVisiblePosition()==news.size()-1) {
index=index++;
Toast.makeText(getActivity(), "加载中...", 0).show();
huodeshuju(index);
}
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
}
});*/
//获得数据
huodeshuju(index);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
index=index++;
Toast.makeText(getActivity(), "加载中...", 0).show();
huodeshuju(index);
}
});
//返回view
return view;
}
//获得数据
private void huodeshuju(final int index) {
//使用线程
new Thread(){
public void run() {
try {
//使用HttpURLConnection获取网络数据
URL url=new URL(urlPath+index);
HttpURLConnection urlConnection=(HttpURLConnection) url.openConnection();
urlConnection.setConnectTimeout(5000);
urlConnection.setReadTimeout(5000);
urlConnection.setRequestMethod("GET");
urlConnection.connect();
int code=urlConnection.getResponseCode();
if (code==200) {
InputStream inputStream=urlConnection.getInputStream();
BufferedReader reader=new BufferedReader(new InputStreamReader(inputStream));
String line;
StringBuffer buffer=new StringBuffer();
while ((line=reader.readLine())!=null) {
buffer.append(line);
}
String str=buffer.toString();
//创建一个消息
Message message=new Message();
message.what=0;
message.obj=str;
//给handler发送消息
handler.sendMessage(message);
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
}.start();
//有种加载的延时感
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (adapter == null) {
//设置Listview的适配器
adapter = new MyAdapter(news, getActivity());
lv.setAdapter(adapter);
}else {
adapter.notifyDataSetChanged();
}
}
}
以上是关于Android中获取网络数据时的分页加载的主要内容,如果未能解决你的问题,请参考以下文章
基于Android官方AsyncListUtil优化经典ListView分页加载机制
使用 Jetpack Compose 无限加载 Android Paging 3 库而无需滚动