1. 程式人生 > >XListView多條目 +ImageLoader 分頁載入

XListView多條目 +ImageLoader 分頁載入

XListView +ImageLoader分頁載入

1.許可權!

匯入xlistview

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

2.設定xlistview

佈局

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.bwei.xlistview.XlistView
        android:id="@+id/xlistview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></com.bwei.xlistview.XlistView>

</android.support.constraint.ConstraintLayout>

主頁面設定

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        xlistView = (XlistView)findViewById(R.id.xlistview);
        xlistView.setPullLoadEnable(true);
        xlistView.setPullRefreshEnable(true);
        xlistView.setXListViewListener(new XlistView.IXListViewListener() {
            @Override
            public void onRefresh() {

                getTime();

                page=1;
                getserverData();
                handler.sendEmptyMessageAtTime(0,5000);

            }

            @Override
            public void onLoadMore() {
                page++;
                getserverData();

                handler.sendEmptyMessageAtTime(1,5000);
            }
        });
        getserverData();
        getTime();
        xlistView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                JsonBean.DataBean dataBean=lists.get(position-1);
                Log.i("xxx","條目"+position);
            }
        });
    }

獲取時間

@SuppressLint("SimpleDateFormat")
    private void getTime() {
        long time = System.currentTimeMillis();
        Date date = new Date(time);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd日 HH:mm:ss");
        String s = format.format(date);
        xlistView.setRefreshTime(s);
    }

AsyncTask獲取資料

private void getserverData() {
        MyAsyncTask myAsyncTask=new MyAsyncTask();
        myAsyncTask.execute();
    }

    private 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);
                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();
            JsonBean jsonBean = gson.fromJson(s, JsonBean.class);
            List<JsonBean.DataBean> list = jsonBean.getData();
            if (page == 1) {
                lists=new ArrayList<>();
            }
            for (int i=0;i<list.size();i++){
                lists.add(list.get(i));
            }
            MyAdapter adapter=new MyAdapter(MainActivity.this, (ArrayList<JsonBean.DataBean>) lists);
            xlistView.setAdapter(adapter);
            xlistView.setSelection(adapter.getCount()-29);
        }
    }

分頁載入建立hander停止載入

 private XlistView xlistView;
    private int page=1;
    private List<JsonBean.DataBean> lists=null;
    Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0:
                    xlistView.stopRefresh();
                    break;
                case 1:
                    xlistView.stopLoadMore();
                    break;
            }
        }
    };

3.imageloader

建立appaction類(記得註冊!)

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        ImageLoaderConfiguration configuration=new ImageLoaderConfiguration.Builder(this).build();

        ImageLoader.getInstance().init(configuration);
    }
}

4.介面卡

佈局1

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">



    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <ImageView
        android:id="@+id/img"
        android:layout_width="100dp"
        android:layout_height="100dp" />
</LinearLayout>

佈局2

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/name2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <ImageView
            android:id="@+id/img1"
            android:layout_width="100dp"
            android:layout_height="100dp" />
        <ImageView
            android:id="@+id/img2"
            android:layout_width="100dp"
            android:layout_height="100dp" />

    </LinearLayout>

</LinearLayout>

介面卡

public class MyAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<JsonBean.DataBean> arrayList=new ArrayList<JsonBean.DataBean>();
    private final static int TYPE_ONE=0;
    private final static int TYPE_TWO=1;
    private final static int TYPE_THREE=2;

    public MyAdapter(Context context, ArrayList<JsonBean.DataBean> arrayList) {
        this.context = context;
        this.arrayList = arrayList;
    }

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

    @Override
    public int getItemViewType(int position) {
        if (position==24||position==29){
            return  TYPE_TWO;
        }else {
            return TYPE_ONE;
        }
    }

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

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        ViewHolder2 holder2 = null;
        int itemViewType = getItemViewType(position);
        if (convertView==null){
            if (itemViewType==TYPE_ONE){
                convertView=LayoutInflater.from(context).inflate(R.layout.item_list,null);
                holder=new ViewHolder();
                holder.view=(TextView)convertView.findViewById(R.id.name);
                holder.img=(ImageView)convertView.findViewById(R.id.img);
                convertView.setTag(holder);
            }else {
                convertView=LayoutInflater.from(context).inflate(R.layout.item_list2,null);
                holder2= new ViewHolder2();
                holder2.view2=(TextView)convertView.findViewById(R.id.name2);
                holder2.img1=(ImageView)convertView.findViewById(R.id.img1);
                holder2.img2=(ImageView)convertView.findViewById(R.id.img2);
                convertView.setTag(holder2);
            }
        }else {
            if (itemViewType==TYPE_ONE){
                holder=(ViewHolder)convertView.getTag();
            }else {
                holder2=(ViewHolder2)convertView.getTag();
            }
        }
        if (itemViewType==TYPE_ONE){
            holder.view.setText(arrayList.get(position).getTitle());
            DisplayImageOptions options=new DisplayImageOptions.Builder()
                    .showImageOnLoading(R.mipmap.ic_launcher_round)
                    .build();
            ImageLoader.getInstance().displayImage(arrayList.get(position).getThumbnail_pic_s(),holder.img,options);
        }else {
            holder2.view2.setText(arrayList.get(position).getTitle());
            DisplayImageOptions options=new DisplayImageOptions.Builder()
                    .showImageOnLoading(R.mipmap.ic_launcher_round)
                    .build();
            ImageLoader.getInstance().displayImage(arrayList.get(position).getThumbnail_pic_s02(),holder2.img1,options);
            ImageLoader.getInstance().displayImage(arrayList.get(position).getThumbnail_pic_s02(),holder2.img2,options);
        }

        return convertView;
    }
    class ViewHolder{
        private TextView view;
        private ImageView img;
    }
    class ViewHolder2{
        private TextView view2;
        private ImageView img1;
        private ImageView img2;
    }
}