XListView上拉下拉

XML主:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.example.a08_xlistview_demo.view.XListView
        android:id="@+id/xListView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:cacheColorHint="#00000000">

    </com.example.a08_xlistview_demo.view.XListView>
</RelativeLayout>

=====================================================================

=====================================================================

list_item页面:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/list_item_textview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="5dp"
    android:textColor="#000"
    android:textSize="16sp">


</TextView>

======================================================================

======================================================================

主Activity:

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.setPullRefreshEnable(true);
        xListView.setPullLoadEnable(true);
        //设置监听事件
        xListView.setXListViewListener(this);

        getDataFromNet();


    }

    /**
     * 刚开始的时候获取网络上的数据...添加到list集合,,,设置适配器
     */
    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() {
        NUM --;
        if (NUM >0){
            //获取数据
            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();
                //设置本次刷新的时间

                Date date = new Date(System.currentTimeMillis());
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
                String time = simpleDateFormat.format(date);


                xListView.setRefreshTime(time);

            }
        };

        asyncTask.execute();
    }

    /**
     * 上拉的时候调用的....加载
     */
    @Override
    public void onLoadMore() {
        NUM ++;
        //请求网络获取数据
        getDataFromNet();
    }
}

=======================================================================

=======================================================================

Adapter页面:

public class MyAdapter extends BaseAdapter {

    Context context;
    List<DataDataBean.ResultsBean> list;

    public MyAdapter(Context context, List<DataDataBean.ResultsBean> list) {
        this.context = context;
        this.list = list;
    }

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

    @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) {

        if (view == null){
            view = View.inflate(context,R.layout.list_item,null);
        }

        TextView textView = view.findViewById(R.id.list_item_textview);

        //设置
        textView.setText(list.get(i).getDesc());

        return view;
    }
}

====================================================

主要是要与XListView联动

====================================================

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值