多条目

//适配器
public class MyAdapter extends BaseAdapter {
    private final static int TYPE_ONE=0;
    private final static int TYPE_TWO=1;
    private final static int TYPE_THREE=2;


    private Context context;
    private List<Json.DataBean> list;
    private DisplayImageOptions build;


    public MyAdapter(Context context, List<Json.DataBean> list) {
        this.context = context;
        this.list = list;
        build=new  DisplayImageOptions.Builder()
                .showImageForEmptyUri(R.mipmap.ic_launcher)//设置默认图片
                .bitmapConfig(Bitmap.Config.RGB_565)//设置色彩模式 避免OOM
                .cacheInMemory(true)//设置内存缓存
                .cacheOnDisk(true)//设置支持SD卡缓存
                .displayer(new RoundedBitmapDisplayer(50))//设置圆角
                .build();
    }

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

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public int getViewTypeCount() {
        return 3;
    }

    @Override
    public int getItemViewType(int position) {
        String pic_s = list.get(position).getThumbnail_pic_s();
        String pic_s02 = list.get(position).getThumbnail_pic_s02();
        String pic_s03 = list.get(position).getThumbnail_pic_s03();
        //三张图片
        if(pic_s!=null&&pic_s02!=null&&pic_s03!=null){
            return TYPE_THREE;
        }else if(pic_s!=null&&pic_s02!=null&&pic_s03==null){
        //二张图片
            return TYPE_TWO;
        }else{
        //一张图片
            return TYPE_ONE;
        }

    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHodler viewHodler=null;
        ViewHodler2 viewHodler2=null;
        ViewHodler3 viewHodler3=null;
        
        //获取条目类型
        int type = getItemViewType(position);

        if(convertView==null){

            //根据条目类型加载布局
            if(type==TYPE_TWO){
               convertView=View.inflate(context, R.layout.item2,null);
                viewHodler2=new ViewHodler2();
                viewHodler2.img=convertView.findViewById(R.id.img);
                viewHodler2.img1=convertView.findViewById(R.id.img1);
                viewHodler2.text=convertView.findViewById(R.id.text);
                convertView.setTag(viewHodler2);
            }else if (type==TYPE_ONE){
                convertView=View.inflate(context, R.layout.item,null);
                viewHodler=new ViewHodler();
                viewHodler.img=convertView.findViewById(R.id.img);
                viewHodler.text=convertView.findViewById(R.id.text);
                convertView.setTag(viewHodler);
            }else{
                convertView=View.inflate(context, R.layout.item3,null);
                viewHodler3=new ViewHodler3();
                viewHodler3.img=convertView.findViewById(R.id.img);
                viewHodler3.img1=convertView.findViewById(R.id.img1);
                viewHodler3.img2=convertView.findViewById(R.id.img2);
                viewHodler3.text=convertView.findViewById(R.id.text);
                convertView.setTag(viewHodler3);
            }

        }else{
            if(type==TYPE_TWO){
                viewHodler2= (ViewHodler2) convertView.getTag();
            }else if (type==TYPE_ONE){
                viewHodler= (ViewHodler) convertView.getTag();
            }else{
                viewHodler3= (ViewHodler3) convertView.getTag();

            }

        }

        //不同类型的条目赋值
        if(type==TYPE_TWO){
            viewHodler2.text.setText(list.get(position).getTitle());
            ImageLoader.getInstance().displayImage(list.get(position).getThumbnail_pic_s(),viewHodler2.img,build);
            ImageLoader.getInstance().displayImage(list.get(position).getThumbnail_pic_s02(),viewHodler2.img1,build);
        }else if (type==TYPE_ONE){
            viewHodler.text.setText(list.get(position).getTitle());
            ImageLoader.getInstance().displayImage(list.get(position).getThumbnail_pic_s(),viewHodler.img,build);
        }else{
            viewHodler3.text.setText(list.get(position).getTitle());
            ImageLoader.getInstance().displayImage(list.get(position).getThumbnail_pic_s(),viewHodler3.img,build);
            ImageLoader.getInstance().displayImage(list.get(position).getThumbnail_pic_s02(),viewHodler3.img1,build);
            ImageLoader.getInstance().displayImage(list.get(position).getThumbnail_pic_s03(),viewHodler3.img2,build);
        }





        return convertView;
    }

    //存储控件

     class ViewHodler{
        private ImageView img;
        private TextView text;
    }

    class ViewHodler2{
        private ImageView img;
        private ImageView img1;
        private TextView text;
    }

    class ViewHodler3{
        private ImageView img;
        private ImageView img1;
        private ImageView img2;
        private TextView text;
    }

}

//Frag中
public class Frag1 extends Fragment {

    private XlistView xlv;
    private Handler handler=new Handler();
    int page=1;
    List<Json.DataBean> list1=null;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.frag1, null, false);
        //控件
        xlv = view.findViewById(R.id.xlv);

      //上拉加载
        xlv.setPullLoadEnable(true);
        //下拉刷新
        xlv.setPullRefreshEnable(true);

        //设置监听
        xlv.setXListViewListener(new XlistView.IXListViewListener() {
            @Override
            public void onRefresh() {

                page=1;
                //获取数据
                getData();
                //获取系统时间
                getTime();

                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {


                        xlv.stopRefresh();
                    }
                },2000);
            }

            @Override
            public void onLoadMore() {
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page++;

                        //获取数据
                        getData();
                        xlv.stopLoadMore();
                    }
                },2000);

            }
        });

//条目点击
        xlv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
           			
                   Log.i("xxx","条目"+position);
            }
        });

        //获取数据
        getData();
        //获取系统时间
        getTime();



        return view;
    }


    //获取时间
    private void getTime() {
        long time = System.currentTimeMillis();
        Date date = new Date(time);
        //创建简单的时间类
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss");
        String s = simpleDateFormat.format(date);
        xlv.setRefreshTime(s);


    }


    //执行异步
    private void getData() {
        MyAsyncTask myAsyncTask = new MyAsyncTask();
        myAsyncTask.execute();

    }

    //异步
    class MyAsyncTask extends AsyncTask<String,Integer,String>{

        @Override
        protected String doInBackground(String... strings) {
            String path="http://www.xieast.com/api/news/news.php?page="+page;
            try {
                URL url = new URL(path);
                HttpURLConnection conn= (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("GET");
                conn.setConnectTimeout(5000);
                conn.setReadTimeout(5000);
                if(conn.getResponseCode()==200){
                    InputStream inputStream = conn.getInputStream();
                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
                    byte[] buffer = new byte[1024];
                    int len=0;
                    while ((len=inputStream.read(buffer))!=-1){
                        bos.write(buffer,0,len);
                    }
                    bos.close();
                    inputStream.close();
                    String json = bos.toString();
                    return json;

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

            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            Gson gson = new Gson();
            Json json = gson.fromJson(s, Json.class);
            List<Json.DataBean> list = json.getData();

            //创建集合
            if(page==1){
                list1=new ArrayList<Json.DataBean>();
            }

               //添加到新集合中
                for (int i=0;i<list.size();i++){
                    list1.add(list.get(i));
                }


       /*     //直接添加到集合
            list1.addAll(list);*/


            MyAdapter myAdapter = new MyAdapter(getActivity(), list1);
            xlv.setAdapter(myAdapter);
             //加载新的数据之后显示最新一页第一条
            xlv.setSelection(myAdapter.getCount()-29);
      }
    }

}



//ImageLoader
		//获得SD卡路径
 		String path =Environment.getExternalStorageDirectory()+"/image";
          //配置
        ImageLoaderConfiguration config=new ImageLoaderConfiguration.Builder(this)
                .diskCache(new UnlimitedDiskCache(new File( path)))
                .build();
        //初始化
        ImageLoader.getInstance().init(config)

//权限

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <application
        android:name=".app.MyApp"
		...
	/>
   </application>





相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页