欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

XListView的简单使用

程序员文章站 2022-04-06 16:44:41
...
public class MainActivity extends AppCompatActivity {

String baseUrl = "http://api.expoon.com/AppNews/getNewsList/type/1/p/";
XListView xListView;
List<GsonData.DataBean>list = new ArrayList<GsonData.DataBean>();
private MAdapter mAdapter;
private int page;
private ImageLoader instance;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //获取控件
    xListView = findViewById(R.id.xlv);
    //使用imageLoader
    instance = ImageLoader.getInstance();

    //适配器
    mAdapter = new MAdapter();
    xListView.setAdapter(mAdapter);

    //添加数据
    initData(0);

    //支持加载更多数据
    xListView.setPullLoadEnable(true);

    //xListView监听事件
    xListView.setXListViewListener(new XListView.IXListViewListener() {
        @Override
        public void onRefresh() {//下拉刷新
            list.clear();//清除之前的数据
            initData(0);//加载新数据
        }

        @Override
        public void onLoadMore() {//上拉刷新
            page++;
            initData(page);
        }
    });

}
}

//联网加载数据

private void initData(int page) {
    String urlData = baseUrl+page;
    
    //使用异步获取数据
    new MAsyncTask().execute(urlData);
}

//异步

private class MAsyncTask extends AsyncTask<String,Void,String> {
    @Override
    protected String doInBackground(String... strings) {
    
        return NetData.getNetjson(strings[0]);//请求接口数据
    }
    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);

        Gson gson = new Gson();

        GsonData gsonData = gson.fromJson(s, GsonData.class);

        List<GsonData.DataBean> data = gsonData.getData();

        //将所有的集合添加到大集合中
        list.addAll(data);

        //适配器刷新数据
        mAdapter.notifyDataSetChanged();;

        //隐藏刷新头和刷新底部
       Complete();
    }
}


//隐藏刷新头和刷新底部
private void Complete() {
    //刷新时出现的字
    xListView.setRefreshTime("刚刚刷新,还没有新数据");
    xListView.stopLoadMore();//底部消失
    xListView.stopRefresh();//头部消失
}

//适配器

 private class MAdapter extends BaseAdapter {

    @Override
    public int getCount() {
        return list.size();
    }

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder=null;
        if (convertView == null) {
        
            holder = new ViewHolder();
            convertView = getLayoutInflater().inflate(R.layout.lv_item,null);
            holder.imageView = convertView.findViewById(R.id.imageView);
            holder.textView = convertView.findViewById(R.id.textView);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.textView.setText(list.get(position).getNews_title());
		//imageLoader加载图片
        instance.displayImage(list.get(position).getPic_url(),holder.imageView);
        return convertView;
    }
}
class ViewHolder{
    ImageView imageView;
    TextView textView;
}