XListView的简单使用入门级

初学XListView(简易版)

XlistView可以实现上拉加载和下拉刷新的功能;
    
首先需要导入三个Java文件

需要删除以前的R文件并导入当前文件夹R文件,


以及导入两个布局文件


一个需要用到的图片(Drawable文件夹中)


Values中的String也需要复制到我们当前的demo当中,接下来就是处理了


首先在layout中声明我们的控件(这只是个示范)



 <com.bwie.oneandonly.test14.view.XListView
        android:id="@+id/xlistview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

紧接着就是HTTPUtil类


public class Httputils {
   private static Httputils httputils;
    private HttpListener httpListener;


    public static Httputils getInstance(){
        if (httputils == null){
            httputils = new Httputils();
        }
        return httputils;
    }


    public void getdata(String url){
        MySynctack synctack = new MySynctack();
        synctack.execute(url);
    }


    class MySynctack extends AsyncTask<String,Void,String>{


        @Override
        protected String doInBackground(String... strings) {


            try {
                URL url = new URL(strings[0]);
                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                urlConnection.setRequestMethod("GET");
                int responsecode = urlConnection.getResponseCode();
                while(responsecode == 200){


                    InputStream inputStream = urlConnection.getInputStream();
                    String json = inputStreamTostring(inputStream);
                    return  json;
                }


            } catch (Exception e) {
                e.printStackTrace();
            }


            return null;
        }




        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
           httpListener.getjsondata(s);


        }
    }
    
    private String inputStreamTostring(InputStream inputStream) throws Exception {
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);


        String string = null;
        StringBuilder builder = new StringBuilder();


        while((string = bufferedReader.readLine()) != null){
            builder.append(string);
        }
        bufferedReader.close();
        return builder.toString();
    }


    public interface HttpListener{
        void getjsondata(String json);
    }


    public void SetHttpListener(HttpListener httpListener){
        this.httpListener = httpListener;
    }


}


最后是Activity方面的

public class MainActivity extends AppCompatActivity {
    private XListView xListView;
    private String path = "http://www.yulin520.com/a2a/impressApi/news/mergeList?pageSize=15&page=";
    private int page = 1;
    private Httputils httputils = Httputils.getInstance();
    private List<MyBean.DataBean> list = new ArrayList<>();
    private MyBase adapter ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        init();
        getweb();
    }


    private void init() {
        xListView = findViewById(R.id.xlistview);


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


        xListView.setXListViewListener(new XListView.IXListViewListener() {
            @Override
            public void onRefresh() {
                page = 1;
                getweb();
            }


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


        adapter = new MyBase(MainActivity.this,list);
        xListView.setAdapter(adapter);
    }


    public void getweb() {
       String url = path+page;


       httputils.getdata(url);


       httputils.SetHttpListener(new Httputils.HttpListener() {
           @Override
           public void getjsondata(String json) {


               Gson gson = new Gson();
               MyBean myBean = gson.fromJson(json,MyBean.class);
               List<MyBean.DataBean> data =  myBean.getData();


               if (page == 1){
                   list.clear();
               }
               list.addAll(data);
               adapter.notifyDataSetChanged();
               if (page ==1){
                   xListView.stopRefresh();
               }else{
                   xListView.stopLoadMore();
               }


           }
       });


    }
}




猜你喜欢

转载自blog.csdn.net/big_fff/article/details/80697557