27
2017
09

Xlistview 应用

导入:
compile 'com.github.andyoom:xlistview:v1.0.1'
maven {url "https://jitpack.io"}

MAIN:
package test.bwie.com.rikao1;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.List;

import me.maxwin.view.XListView;

public class MainActivity extends AppCompatActivity implements XListView.IXListViewListener{

private XListView xListView;
int page = 1;
private String str = "http://api.expoon.com/AppNews/getNewsList/type/1/p/";
private Myadater myadater;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    xListView = (XListView) findViewById(R.id.xlistview);



    xListView.setPullRefreshEnable(true);
    xListView.setPullLoadEnable(true);
    xListView.setXListViewListener(this);


    myadater = new Myadater(MainActivity.this);
    xListView.setAdapter(myadater);

    Getsuju(page);



}

private void Getsuju(final int page) {


    x.http().get(new RequestParams(str + page), new Callback.CommonCallback<String>() {
        @Override
        public void onSuccess(String result) {
            Mydata m = new Gson().fromJson(result,Mydata.class);
            List<Mydata.DataBean> list = m.getData();
            myadater.Add(list,page);
        }

        @Override
        public void onError(Throwable ex, boolean isOnCallback) {

        }

        @Override
        public void onCancelled(CancelledException cex) {

        }

        @Override
        public void onFinished() {
            xListView.stopLoadMore();
            xListView.stopRefresh();
            xListView.setRefreshTime("刚刚");

        }
    });

}


@Override
public void onRefresh() {
    page =1;
    Getsuju(page);
}

@Override
public void onLoadMore() {
    page +=1;
    Getsuju(page);
}

}

Adater:
package test.bwie.com.rikao1;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

/**
* Created by 白玉春 on 2017/9/26.
*/

public class Myadater extends BaseAdapter {

Context context;
List<Mydata.DataBean> list;

public Myadater(Context context) {
    this.context = context;
    this.list=  new ArrayList<>();
}



public void Add(List<Mydata.DataBean> newadd, int page){
    if(page==1){
        this.list.clear();
    }
    this.list.addAll(newadd);
    notifyDataSetChanged();
}

@Override
public int getCount() {
    return list!=null?list.size():0;
}

@Override
public Object getItem(int i) {
    return list.get(i);
}

@Override
public long getItemId(int i) {
    return i;
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
    Viewholder viewholder = null;

    if(view == null){
        view  = View.inflate(context,R.layout.item,null);
        viewholder  = new Viewholder();
        viewholder.im = view.findViewById(R.id.image);
        viewholder.t = view.findViewById(R.id.tv);
        view.setTag(viewholder);
    }else{
        viewholder = (Viewholder) view.getTag();
    }
    ImageLoader.getInstance().displayImage(list.get(i).getPic_url(),viewholder.im);
    viewholder.t.setText(list.get(i).getNews_title());
    return view;
}


class Viewholder{
    ImageView im;
    TextView t;
}

}

buju:

上一篇:APP登录界面(网页仿制版) 下一篇:STC89C51单片机串口程序下载失败总结