XlistView的使用加XlistView的多布局


XlistViewDemo通过网络获取数据 解析json字符串 通过Xlistview多布局展示

首先下载XlistView的源码 下面的是地址


https://github.com/Maxwin-z/XListView-Android


将源码复制到自己的代码中  各种布局  图片 也用源码中的



首先是布局



<view.XListView
    android:id="@+id/lv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>


实现XlistView的接口

public class MainActivity extends AppCompatActivity implements XListView.IXListViewListener{


    private List list;
    private XListView lv;
//定义你要获取数据的地址
 private  String geturl="http://v.juhe.cn/toutiao/index?key=22a108244dbb8d1f49967cd74a0c144d";
    private String title;
    private String thumbnail_pic_s;
    private NewsAdapter adapter;

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

//通过AsyncTask获取网络数据
        new MyAsyncTask().execute(geturl);
    }


//图片是通过Imageloader来获取
    public void initImageLoader() {
        //构建者模式
        DisplayImageOptions options=new DisplayImageOptions.Builder()
                .cacheOnDisk(true)//允许磁盘缓存
                .cacheInMemory(true)//允许内存缓存
                .showImageForEmptyUri(R.drawable.ic_empty)//uri为空时 设置默认图片
                .showImageOnFail(R.drawable.ic_error)//失败时 设置图片
                .showImageOnLoading(R.drawable.ic_loading)//下载时 设置图片
                .bitmapConfig(Bitmap.Config.RGB_565)//设置bitmap的类型
                .displayer(new RoundedBitmapDisplayer(60))
                .build();


        ImageLoaderConfiguration config=new ImageLoaderConfiguration.Builder(this)
                .writeDebugLogs()//打印
                .diskCacheSize(5*1024*1024)//设置磁盘内存大小
                .memoryCacheSize(5*1024*1024)//设置内存大小
                .threadPoolSize(5)//设置线程池多大 5个线程同时下载图片
                .threadPriority(6) //线程的优先级
                .diskCacheFileNameGenerator(new Md5FileNameGenerator())//缓存图片进行加密
                .defaultDisplayImageOptions(options)//optionsconfig
                .build();
        //初始化ImageLoder
        ImageLoader.getInstance().init(config);

    }

    private void inviData() {

        adapter = new NewsAdapter(this,list);
        lv.setAdapter(adapter);
    }

//异步加载数据 
    class MyAsyncTask extends AsyncTask {
        private StringBuffer result;
        @Override
        protected String doInBackground(String... strings) {
            result = new StringBuffer();
            try {
                URL url = new URL(strings[0]);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("GET");
                connection.connect();
                if (200 == connection.getResponseCode()) {
                    InputStream inputStream = connection.getInputStream();
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                    String line = null;
                    while ((line=bufferedReader.readLine()) != null) {

                        result.append(line);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return result.toString();
        }
        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            parseData(s);
            adapter.notifyDataSetChanged();

//下拉刷新后消失
            lv.stopRefresh();
//上拉加载后消失
 lv.stopLoadMore();
        }
    }

//解析json字符串
    private void parseData(String s) {

        Gson gson=new Gson();
        News news = gson.fromJson(s, News.class);
        News.ResultBean result = news.result;
        List data = result.data;
        for (int i = 0; i < data.size(); i++) {
            News.ResultBean.DataBean dataBean = data.get(i);
            title = dataBean.title;
            thumbnail_pic_s = dataBean.thumbnail_pic_s;
            News2 news2=new News2(title,thumbnail_pic_s);
            list.add(news2);
        }
    }
//初始化控件 
 private void initView() {
        lv = (XListView) findViewById(R.id.lv);
        list = new ArrayList<>();
        lv.setPullLoadEnable(true);//xlistView可以下拉刷新
        lv.setXListViewListener(this); //必须实现这个接口 必须给接口才能使用
    }
    @Override
    public void onRefresh() {
list.clear();
        new MyAsyncTask().execute(geturl);
    }

    @Override
    public void onLoadMore() {
        new MyAsyncTask().execute(geturl);
    }
}
 



XlistView适配器的实现



public class NewsAdapter extends BaseAdapter {
    private Context context;
    private List list;
    private final int atype=0;//第一种布局
    private final int btype=1;//第二种布局
    private final int type_num=2; //布局的总数

    public NewsAdapter(Context context, List 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;
    }

//获取listView的多种布局 模拟两种布局
    @Override
    public int getItemViewType(int position) {

        if(position%2==0)
        {
            return atype;
        }
        else
        {
            return btype;
        }
    }

//获取布局的总数
    @Override
    public int getViewTypeCount() {
        return type_num;
    }


//xlistView的优化
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        //获取布局类型
        int type = getItemViewType(i);

        ViewHolder1 holder1=null;
        ViewHolder2 holder2=null;
        if(view==null)
        {
    //判断布局
            switch (type)

            {



                case atype:

                    holder1=new ViewHolder1();
                    view = View.inflate(context, R.layout.item1, null);

                    holder1.tv_title=view.findViewById(R.id.tv_title);
                    holder1.iv=view.findViewById(R.id.iv);
                    view.setTag(holder1);

                    break;
                case btype:

                    holder2=new ViewHolder2();
                    view = View.inflate(context, R.layout.item2, null);

                    holder2.tv_title2=view.findViewById(R.id.tv_title2);
                    holder2.iv2=view.findViewById(R.id.iv2);
                    view.setTag(holder2);
                    break;

            }
        }else
        {
            switch (type)
            {
                case atype:
                    holder1= (ViewHolder1) view.getTag();
                    holder1.tv_title.setText(list.get(i).getTitle());
//使用ImageLoader实现图片的加载
 ImageLoader.getInstance().displayImage(list.get(i).getThumbnail_pic_s(),holder1.iv);
                    break;

                case btype:
                    holder2= (ViewHolder2) view.getTag();
                    holder2.tv_title2.setText(list.get(i).getTitle());
                    ImageLoader.getInstance().displayImage(list.get(i).getThumbnail_pic_s(),holder2.iv2);
                    break;
            }
        }

        return view;
    }
    class ViewHolder1{
        TextView tv_title;
        ImageView iv;
    }
    class ViewHolder2{
        TextView tv_title2;
        ImageView iv2;
    }
}











你可能感兴趣的:(XlistView的使用加XlistView的多布局)