#XListView...扩展了ListView
github上的开源代码
* https://github.com/Maxwin-z/XListView-Android
* 能够实现listView上拉和下拉的操作
操作数据的地址
* http://gank.io/api
1.下拉刷新上拉加载的效果展示
2.原理的分析
* 当前条目展示第一条0的时候可以下拉
* 当前展示最后一条的时候可以上拉
3.代码实现
* 1.复制头布局,footer布局问价及所需要的string,和图片
* 2.使用方法和listView类似
* 主要使用的方法
xListView.setPullLoadEnable(true);//加载更多...false时上拉不加载
xListView.setPullRefreshEnable(true);//下拉刷新...false不刷新
xListView.setXListViewListener(this);//设置监听
* 当下拉的时候会自动调用onRefresh()方法,在这里面可以刷新数据,数据刷新完成之后调用xListView.stopRefresh();//头布局回弹
* xListView.setRefreshTime("刚刚2秒钟");//设置本次刷新的时间...再次下拉的时候会看到
* 当上拉会自动调用onLoadMore()方法,在这里可以加载更多数据,数据加载完成后,调用xListView.stopLoadMore();
* main函数里
* public class MainActivity extends AppCompatActivity implements XListView.IXListViewListener {
private XListView xListView;
private List<DataDataBean.ResultsBean> list = new ArrayList<>();
private int NUM = 10;
private MyAdapter myAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
xListView = (XListView) findViewById(R.id.xListView);
//设置下拉和上拉可用....如果false不可用
xListView.setPullLoadEnable(true);
xListView.setPullRefreshEnable(true);
xListView.setXListViewListener(this);
getDataFromNet();
}
private void getDataFromNet() {
AsyncTask<Void, Void, String> asyncTask = new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... voids) {
String path = "http://gank.io/api/data/Android/10/"+NUM;
try {
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//设置
connection.setRequestMethod("GET");
connection.setReadTimeout(5000);
connection.setConnectTimeout(5000);
//获取
int responseCode = connection.getResponseCode();
if (responseCode == 200){
InputStream inputStream = connection.getInputStream();
String json = streamToString(inputStream,"utf-8");
return json;
}
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
@Override
protected void onPostExecute(String json) {
Gson gson = new Gson();
DataDataBean dataDataBean = gson.fromJson(json, DataDataBean.class);
//dataDataBean.getResults();
//将这次的十条数据添加到集合
list.addAll(dataDataBean.getResults());
setAdapter();
//数据加载展示完之后...停止
xListView.stopLoadMore();
}
};
asyncTask.execute();
}
private void setAdapter() {
if (myAdapter == null){
myAdapter = new MyAdapter(MainActivity.this,list);
xListView.setAdapter(myAdapter);
}else {
myAdapter.notifyDataSetChanged();
}
}
private String streamToString(InputStream inputStream,String charset) {
try {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream,charset);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String s = null;
StringBuilder builder = new StringBuilder();
while ((s = bufferedReader.readLine()) != null){
builder.append(s);
}
bufferedReader.close();
return builder.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
//下拉刷新
@Override
public void onRefresh() {
if (NUM>0){
NUM--;
refreshData();
}else {
Toast.makeText(MainActivity.this,"没有最新数据了",Toast.LENGTH_SHORT).show();
xListView.stopRefresh();//停止刷新
}
}
private void refreshData() {
AsyncTask<Void, Void, String> asyncTask = new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... voids) {
String path = "http://gank.io/api/data/Android/10/"+NUM;
try {
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//设置
connection.setRequestMethod("GET");
connection.setReadTimeout(5000);
connection.setConnectTimeout(5000);
//获取
int responseCode = connection.getResponseCode();
if (responseCode == 200){
InputStream inputStream = connection.getInputStream();
String json = streamToString(inputStream,"utf-8");
return json;
}
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
@Override
protected void onPostExecute(String json) {
Gson gson = new Gson();
DataDataBean dataDataBean = gson.fromJson(json, DataDataBean.class);
//dataDataBean.getResults();
//数据要添加到集合的最前边
list.addAll(0,dataDataBean.getResults());
setAdapter();
//停止刷新
xListView.stopRefresh();
}
};
asyncTask.execute();
}
//上啦加载
@Override
public void onLoadMore() {
NUM++;
getDataFromNet();
}
}
xml布局里 <demo.bwie.com.xlistview0920.view.XListView
android:id="@+id/xListView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#00ff00">
文章标题Xlistview
最新推荐文章于 2019-01-13 19:36:20 发布