xlistview--狸菇凉_
来源:互联网 发布:cura切片软件 编辑:程序博客网 时间:2024/05/20 08:44
上拉刷新,下拉加载
本文章上拉加载一条数据,下拉加载10条数据,且有两个方法,,方法1和方法2的有几个类是一样的,所以下面就不写了
1.先找着xlistview的app,把它导里面:new -->import module-->找到xlistview的app
2.改build.gradle的buildToolsVersion的值改成下面需要的
3.再改build.gradle中的apply plugin的application改成library,,在把applicationId删掉
封装类
Utils的streamtools类
/**
* 流转化工具类
*/
public class StreamTools {
/**
* 流转化成字符串
* @param is
* @return
*/
public static String readFromNetWork(InputStream is){
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) != -1){
baos.write(buffer,0,len);
}
return baos.toString();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
Adapter的mybaseadapter类
public class MyBaseAdapter extends BaseAdapter {
private Context context;
//private MenuInfo menuInfo;
private List<MenuInfo.ResultBean.DataBean> list;
public MyBaseAdapter(Context context, List<MenuInfo.ResultBean.DataBean> list){
this.context = context;
//this.menuInfo = menuInfo;
this.list = list;
}
/**
* 加载更多数据
*/
public void addMore(List<MenuInfo.ResultBean.DataBean> lists,boolean isRefresh){
for (MenuInfo.ResultBean.DataBean data: lists) {
//将最新的数据添加到适配所定义的集合中
if(isRefresh) {
//isRefresh true 下拉的动作 要把数据添加到最前面
list.add(0, data);
}else {
//isRefresh false 上拉的动作 要把数据添加到最后面
list.add(data);
}
}
}
@Override
public int getCount() {
/* return menuInfo.getResult().getData() != null ?
menuInfo.getResult().getData().size() : 0;*/
return list != null ? list.size() : 0;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if(convertView == null){
viewHolder = new ViewHolder();
convertView = convertView.inflate(context, R.layout.item,null);
viewHolder.tvDesc = (TextView) convertView.findViewById(R.id.tvDes);
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) convertView.getTag();
}
//设置文本信息
viewHolder.tvDesc.setText(list.get(position).getTags());
return convertView;
}
//定义V
static class ViewHolder{
TextView tvDesc;
}
}
Bean的menuinfo类 这个就不写了,自己写吧
Mainactivity类
public class MainActivity extends AppCompatActivity implements XListView.IXListViewListener {
private static final String TAG = "MainActivity";
private XListView xLv;
private int pageIndex = 1;
private MyBaseAdapter adapter;
//设置一个标识,判断用户是否是上拉还是下拉
private boolean isRefreash = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
xLv = (XListView) findViewById(R.id.xLv);
//设置是否可以上拉加载更多
xLv.setPullLoadEnable(true);
//设置是否可以下拉刷新
//xLv.setPullRefreshEnable(true);
xLv.setRefreshTime("10:53:13");
xLv.setXListViewListener(this);
try {
getMenuInfo("http://apis.juhe.cn/cook/query.php?" +
"key=a7a42220c5c1c5268be7ba25af764f6c&menu="+
URLEncoder.encode("秘制红烧肉","utf-8")+"&pn"+pageIndex+"&rn="+10);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 分页加载菜谱数据
*/
private void getMenuInfo(String path){
new AsyncTask<String,Void,String>(){
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
//主线程执行
if(s == null){
return;
}
//解析
Gson gson = new Gson();
MenuInfo menuInfo = gson.fromJson(s, MenuInfo.class);
//UI更改
if(adapter == null) {
//如果是adapter第一次进来空 判定为第一次的默认加载
adapter = new MyBaseAdapter(MainActivity.this,
menuInfo.getResult().getData());
}else {
//加载更多
adapter.addMore(menuInfo.getResult().getData(),isRefreash);
adapter.notifyDataSetChanged();
}
xLv.setAdapter(adapter);
}
@Override
protected String doInBackground(String... params) {
//执行网络请求
try {
String path = params[0];
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
int code = connection.getResponseCode();
if(code == HttpURLConnection.HTTP_OK){
//获取数据
InputStream is = connection.getInputStream();
String json = StreamTools.readFromNetWork(is);
return json;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}.execute(path);
}
/**
* 下拉刷新的时候会执行的方法
*/
@Override
public void onRefresh() {
Log.d(TAG, "onRefresh: 下拉");
//请求第二页数据,需求:把最新的数据加载最前面
try {
isRefreash = true;
++pageIndex;//更改请求第几页
getMenuInfo("http://apis.juhe.cn/cook/query.php?" +
"key=a7a42220c5c1c5268be7ba25af764f6c&menu="+
URLEncoder.encode("秘制红烧肉","utf-8")+"&pn"+pageIndex+"&rn="+10);
//停止刷新 , 让刷新的UI回弹
xLv.stopRefresh(true);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
/**
* 上拉加载更多数据时所执行的方法
*/
@Override
public void onLoadMore() {
Log.d(TAG, "onLoadMore: 上拉");
try {
isRefreash = false;
++pageIndex;//更改请求第几页
getMenuInfo("http://apis.juhe.cn/cook/query.php?" +
"key=a7a42220c5c1c5268be7ba25af764f6c&menu="+
URLEncoder.encode("秘制红烧肉","utf-8")+"&pn"+pageIndex+"&rn="+10);
xLv.stopLoadMore();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
另一种方法的mainactivity类
package com.main.pull_up_refresh;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
import com.google.gson.Gson;
import com.limxing.xlistview.view.XListView;
import com.main.bean.Bean;
import com.main.myadapter.MyAdapter;
import com.main.utils.Utils;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
public class MainActivityextendsAppCompatActivityimplementsXListView.IXListViewListener {
privateXListViewxlv;
private List<Bean.ResultBean.DataBean>data;
private MyAdaptermyAdapter;
private boolean up=false;
@Override
protected voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
xlv= (XListView) findViewById(R.id.xlv);
//是否可以设置上拉加载更多
xlv.setPullLoadEnable(true);
//设置是否可以下拉刷新
xlv.setPullRefreshEnable(true);
xlv.setRefreshTime("下拉刷新");
xlv.setXListViewListener(this);
//固定了 菜名是红烧肉
getmenu("http://apis.juhe.cn/cook/query.php?menu=%E7%BA%A2%E7%83%A7%E8%82%89"+
"&dtype=&pn=0&rn=10&albums=&=&key=d119d136e1a4cc90d44c439a09e0f5a0");
}
//加载菜谱数据
public voidgetmenu(String url) {
newAsyncTask<String,Void,String>() {
@Override
protected voidonPostExecute(String s) {
super.onPostExecute(s);
if (s ==null) {
return;
}else{
Gson gson =newGson();
Bean bean = gson.fromJson(s,Bean.class);
data= bean.getResult().getData();
myAdapter=newMyAdapter(MainActivity.this,data);
xlv.setAdapter(myAdapter);
}
}
@Override
protectedStringdoInBackground(String... params) {
try{
String param = params[0];
URL url1 =newURL(param);
HttpURLConnection urlConnection = (HttpURLConnection) url1.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setConnectTimeout(8000);
urlConnection.setReadTimeout(8000);
if (urlConnection.getResponseCode() ==200) {
InputStream inputStream = urlConnection.getInputStream();
String geturl = Utils.geturl(inputStream);
return geturl;
}
} catch(MalformedURLException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}
return null;
}
}.execute(url);
}
private voidflush(final booleanup,String path) {
newAsyncTask<String,Void,String>() {
@Override
protected voidonPostExecute(String s) {
super.onPostExecute(s);
if (s ==null) {
return;
}else{
Gson gson =newGson();
Bean bean = gson.fromJson(s,Bean.class);
List<Bean.ResultBean.DataBean> data1 = bean.getResult().getData();
if (up) {
MainActivity.this.data.add(0,data1.get(0));
}else{
for(inti =0;i < data1.size();i++) {
data.add(data1.get(i));
}
}
myAdapter.notifyDataSetChanged();
}
}
@Override
protectedStringdoInBackground(String... params) {
try{
String param = params[0];
URL url1 =newURL(param);
HttpURLConnection urlConnection = (HttpURLConnection) url1.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setConnectTimeout(8000);
urlConnection.setReadTimeout(8000);
if (urlConnection.getResponseCode() ==200) {
InputStream inputStream = urlConnection.getInputStream();
String geturl = Utils.geturl(inputStream);
return geturl;
}
} catch(MalformedURLException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}
return null;
}
}.execute(path);
}
//实现接口后自动重写的 下拉刷新时会执行的方法
@Override
public voidonRefresh() {
Toast.makeText(this,"onRefresh",Toast.LENGTH_SHORT).show();
int pnn = (data.size() +1);
up=true;
flush(up,"http://apis.juhe.cn/cook/query.php?menu=%E7%BA%A2%E7%83%A7%E8%82%89&dtype=&pn="+ pnn
+ "&rn=1&albums=&=&key=d119d136e1a4cc90d44c439a09e0f5a0");
xlv.stopRefresh(true);
}
//实现接口后自动重写的 上拉加载时会执行的方法
@Override
public voidonLoadMore() {
Toast.makeText(this,"onLoadMore",Toast.LENGTH_SHORT).show();
up=false;
int pnn = (data.size() +1);
flush(up,"http://apis.juhe.cn/cook/query.php?menu=%E7%BA%A2%E7%83%A7%E8%82%89&dtype=&pn="+ pnn
+ "&rn=10&albums=&=&key=d119d136e1a4cc90d44c439a09e0f5a0");
xlv.stopLoadMore("onLoadMoreccc");
}
}
Mybaseadapter类
package com.main.myadapter;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.main.bean.Bean;
import com.main.pull_up_refresh.R;
import java.util.List;
/**
*时间:2017/4/29
* 类用途:
*/
public class MyAdapterextendsBaseAdapter {
privateContextcontext;
private List<Bean.ResultBean.DataBean>list;
public MyAdapter(Context context,List<Bean.ResultBean.DataBean> list) {
this.context= context;
this.list= list;
}
@Override
public intgetCount() {
returnlist!=null?list.size() :0;
}
@Override
publicObjectgetItem(intposition) {
returnlist.get(position);
}
@Override
public longgetItemId(intposition) {
returnposition;
}
@Override
publicViewgetView(intposition,View convertView,ViewGroup parent) {
if(convertView ==null){
convertView = View.inflate(context,R.layout.item,null);
}
TextView textView = (TextView) convertView.findViewById(R.id.text_item);
textView.setText(list.get(position).getId()+" "+list.get(position).getTags());
return convertView;
}
}
- xlistview--狸菇凉_
- XListView
- XListView
- Xlistview
- XListView
- xListView
- XListView
- xlistview
- xListView
- XlistView
- xlistview
- xlistview
- XListView
- xlistview
- xListview
- XListView
- XListView
- xlistview
- C++实验五
- ZCMU 1098 查找元素
- React Native打包
- 在阿里云上部署Laravel应用
- 【学习笔记09】java面向对象-final、抽象类
- xlistview--狸菇凉_
- 【Java集合类源码分析】Vector源码分析
- Linux-权限知识
- ROI_PAC--use_rsc.pl使用说明
- Margin Convention
- 西电捷通:面向安全协议套件的基线生成方法
- K-means以及K-Means++
- Linux下修改Mysql的用户(root)的密码
- c++第5次上机实验:数组分离