首先需要在https://github.com/下载一个XListView的库
下载后将所有的资源文件和java下的文件复制到我们自己建的Moudle中
只需要将他们导的R路径删掉重新导入即可。
下面MainActivity的代码
public class MainActivity extends AppCompatActivity {
String url = "http://apis.juhe.cn/cook/query?key=91c73d6bce9656b27ad1e6db09b2ecad&menu=%E8%A5%BF%E7%BA%A2%E6%9F%BF&rn=10&pn=" ;
private MyAdapter adapter;
private int page = 0;
private XListView xListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找控件
xListView = findViewById(R.id.xListView);
//设置适配器
adapter = new MyAdapter(this);
xListView.setAdapter(adapter);
//设置支持
xListView.setPullLoadEnable(true);
xListView.setPullRefreshEnable(true);
//拉动监听
xListView.setXListViewListener(new XListView.IXListViewListener() {
@Override
public void onRefresh() {
//上拉刷新
page = 0;
loadData(page);
}
@Override
public void onLoadMore() {
//下拉加载
loadData(page);
}
});
//默认显示第一个页面
loadData(page);
}
@SuppressLint("StaticFieldLeak")
private void loadData(int page) {
String requestUrlWithPageNum = url + page;
//异步处理
new AsyncTask<String, Void, List<DataItem>>() {
@Override
protected List<DataItem> doInBackground(String... strings) {
ResponBean responBean = null;
//地址
try {
URL url = new URL(strings[0]);
//打开连接
try {
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
//设置请求方法 、 超时
urlConnection.setRequestMethod("GET");
urlConnection.setReadTimeout(5000);
urlConnection.setConnectTimeout(5000);
//获取请求码
int responseCode = urlConnection.getResponseCode();
if(responseCode == 200){
//将流转换成字符串
String result = stream2String(urlConnection.getInputStream());
//解析json
responBean = new Gson().fromJson(result, ResponBean.class);
}else{
}
return responBean == null ? null : responBean.getResult().getData();
} catch (IOException e) {
e.printStackTrace();
}
} catch (MalformedURLException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(List<DataItem> dataItems) {
super.onPostExecute(dataItems);
if(dataItems == null){
Toast.makeText(MainActivity.this,"数据加载错误",Toast.LENGTH_SHORT).show();
return;
}
//更新数据
updateData(dataItems);
loadConpleted();
}
}.execute(requestUrlWithPageNum);
}
private String stream2String(InputStream is) {
StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
try {
for(String tmp = br.readLine() ; tmp != null ; tmp = br.readLine()){
sb.append(tmp);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
private void updateData(List<DataItem> dataItems){
if(page == 0){
adapter.setData(dataItems);
}else{
//添加数据
adapter.addDatas(dataItems);
}
}
//通过 加载 / 刷新 / 完成
private void loadConpleted(){
page++;
xListView.stopLoadMore();
xListView.stopRefresh();
}
主要介绍XListView的基本使用所以注释偷懒了。
下面Adapter的代码:
public class MyAdapter extends BaseAdapter {
/*private View[] cache;
private void init() {
//0 - typecount -1;
cache = new View[getViewTypeCount()];
cache[getItemViewType(0)] = null;
}*/
private List<DataItem> mDatas ;
private Context context ;
private static final int TYPE1 = 0;
private static final int TYPE2 = 1;
public MyAdapter(Context context) {
this.context = context;
mDatas = new ArrayList<>();
}
public void setData(List<DataItem> dataItems){
mDatas.clear();
if(dataItems != null){
mDatas.addAll(dataItems);
}
notifyDataSetChanged();
}
//追加数据
public void addDatas(List<DataItem> dataItems){
if(dataItems != null){
mDatas.addAll(dataItems);
notifyDataSetChanged();
}
}
@Override
public int getCount() {
return mDatas.size();
}
@Override
public DataItem getItem(int position) {
return mDatas.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
if(position % 2 ==0){
return TYPE1;
}else{
return TYPE2;
}
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder1 vh1;
ViewHolder2 vh2;
int type = getItemViewType(position);
switch (type){
case TYPE1 :
if(convertView == null){
convertView = View.inflate(context , R.layout.item1,null);
vh1 = new ViewHolder1(convertView);
}else{
vh1 = (ViewHolder1) convertView.getTag();
}
//设置内容
vh1.setDatas(getItem(position));
break;
case TYPE2 :
if(convertView == null){
convertView = View.inflate(context,R.layout.item2,null);
vh2 = new ViewHolder2(convertView);
}else{
vh2 = (ViewHolder2) convertView.getTag();
}
//设置内容
vh2.setDatass(getItem(position));
break;
}
return convertView;
}
public class ViewHolder1 {
ImageView imageView;
TextView title;
TextView id;
public ViewHolder1 (View itemView){
imageView = itemView.findViewById(R.id.image1_l);
title = itemView.findViewById(R.id.title1_l);
id = itemView.findViewById(R.id.id1_l);
itemView.setTag(this);
}
private void setDatas(DataItem item) {
title.setText(item.getTitle());
id.setText(item.getId());
//ImageLoader,imageView.getContext
ImageLoader.getInstance().displayImage(item.getFirstImageUrl(),imageView,ImagLoaderConfigs.getDusplayImageOptions(imageView.getContext()));
}
}
public class ViewHolder2{
TextView title2;
public ViewHolder2 (View itemView) {
title2 = itemView.findViewById(R.id.title2_l);
itemView.setTag(this);
}
public void setDatass (DataItem dataItem){
title2.setText(dataItem.getTitle());
}
}
}