listview

来源:互联网 发布:淘宝产品详情页素材 编辑:程序博客网 时间:2024/06/05 20:30

运用listview,实现效果

MainActivity

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity {

    String mPath="http://v.juhe.cn/toutiao/index?type=top&key=2f092bd9ce76c0257052d6d3c93c11b4";
    private ListView lv;

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

        init();
        initgetdata(mPath);
    }

    private void initgetdata(String mPath) {

        MyAsyncTask myAsyncTask=new MyAsyncTask(MainActivity.this,mPath,"GET",lv);
        myAsyncTask.execute();
    }

    private void init() {
        lv = (ListView) findViewById(R.id.lv);
    }

}

MyAdapter

public class MyAdapter extends BaseAdapter{
    List<Data> list;
    Context context;
    private static final int typeOne=0;
    private static final int typeTwo=1;
    private static final int typeThree=2;

    public MyAdapter(List<Data> list, Context context) {

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

        ViewHolderOne holderOne = null;
        ViewHolderTwo holderTwo = null;
        ViewHolderThree holderThree = null;

        int type = getItemViewType(i);

        if (view == null) {

            switch (type) {

                case typeOne:

                    holderOne = new ViewHolderOne();
                    view = view.inflate(context, R.layout.item_01, null);

                    holderOne.tv_01 =  view.findViewById(R.id.tv_01);
                    holderOne.img_01 =  view.findViewById(R.id.img_01);
                    holderOne.img_02 =  view.findViewById(R.id.img_02);
                    holderOne.img_03 =  view.findViewById(R.id.img_03);

                    view.setTag(holderOne);

                    break;

                case typeTwo:

                    holderTwo = new ViewHolderTwo();
                    view = view.inflate(context, R.layout.item_02, null);

                    holderTwo.tv_02 =  view.findViewById(R.id.tv_02);
                    holderTwo.img_04 =  view.findViewById(R.id.img_04);
                    holderTwo.img_05 =  view.findViewById(R.id.img_05);

                    view.setTag(holderTwo);

                    break;

                case typeThree:

                    holderThree = new ViewHolderThree();
                    view = view.inflate(context, R.layout.item_03, null);

                    holderThree.tv_03 =  view.findViewById(R.id.tv_03);
                    holderThree.img_06 =  view.findViewById(R.id.img_06);

                    view.setTag(holderThree);

                    break;

                default:

                    break;

            }

        }

        switch (type) {

            case typeOne:

                holderOne = (ViewHolderOne) view.getTag();

                holderOne.tv_01.setText(list.get(i).getTitle());

                ImageLoader.getInstance().displayImage(list.get(i).getThumbnail_pic_s(), holderOne.img_01);
                ImageLoader.getInstance().displayImage(list.get(i).getThumbnail_pic_s02(), holderOne.img_02);
                ImageLoader.getInstance().displayImage(list.get(i).getThumbnail_pic_s03(), holderOne.img_03);

                break;

            case typeTwo:

                holderTwo = (ViewHolderTwo) view.getTag();

                holderTwo.tv_02.setText(list.get(i).getTitle());

                ImageLoader.getInstance().displayImage(list.get(i).getThumbnail_pic_s(), holderTwo.img_04);
                ImageLoader.getInstance().displayImage(list.get(i).getThumbnail_pic_s02(), holderTwo.img_05);

                break;

            case typeThree:

                holderThree = (ViewHolderThree) view.getTag();

                holderThree.tv_03.setText(list.get(i).getTitle());

                ImageLoader.getInstance().displayImage(list.get(i).getThumbnail_pic_s(), holderThree.img_06);

                break;

            default:

                break;

        }

        return view;

    }

    @Override
    public int getItemViewType(int position) {

        int posttype = position % 3;

        if (posttype == 0) {

            return typeOne;

        } else if (posttype == 1) {

            return typeTwo;

        } else {

            return typeThree;

        }

    }

    @Override
    public int getViewTypeCount() {

        return 3;

    }

    class ViewHolderOne {

        View tv_01;

        View img_01;

        View img_02;

        View img_03;

    }

    class ViewHolderTwo {

        View tv_02;

        View img_04;

        View img_05;

    }

    class ViewHolderThree {

        View tv_03;

        View img_06;

    }

}

MyAsyncTask

public class MyAsyncTask extends AsyncTask<String,Integer,String>{



    Context context;
    String url;
    String type;
    ListView lv;
    private JsonRootBean dataBean;

    public MyAsyncTask(Context context, String url, String type, ListView lv) {

        super();
        this.context = context;
        this.url = url;
        this.type = type;
        this.lv = lv;

    }

    public void GetDatas(String murl, String mtype) {

        try {

            URL url = new URL(murl);

            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();

            connection.setRequestMethod(mtype);
            connection.setReadTimeout(5 * 1000);
            connection.setConnectTimeout(5 * 1000);
            connection.setDoOutput(true);

            String params = "type=top&key=2f092bd9ce76c0257052d6d3c93c11b4";
            connection.getOutputStream().write(params.getBytes());

            int responseCode = connection.getResponseCode();

            if (responseCode == 200) {

                InputStream inputStream = connection.getInputStream();

                String json = streamToString(inputStream, "utf-8");

                Gson gson = new Gson();

                dataBean = gson.fromJson(json, JsonRootBean.class);

            }

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

    private String streamToString(InputStream inputStream, String charset) {

        try {

            InputStreamReader inputStreamReader = new InputStreamReader(
                    inputStream, charset);

            BufferedReader bufferedReader = new BufferedReader(
                    inputStreamReader);

            StringBuilder builder = new StringBuilder();

            String con = null;

            while ((con = bufferedReader.readLine()) != null) {

                builder.append(con);

            }

            bufferedReader.close();

            return builder.toString();

        } catch (Exception e) {

            e.printStackTrace();

        }

        return null;

    }

    @Override
    protected String doInBackground(String... params) {
        GetDatas(url, type);
        return null;
    }

    @Override
    protected void onPostExecute(String result) {

        List<Data> listbean = dataBean.getResult().getData();

        MyAdapter myAdapter = new MyAdapter(listbean, context);

        lv.setAdapter(myAdapter);

        super.onPostExecute(result);

    }

    public MyAsyncTask() {
        super();
    }

}
Img

public class Img extends Application{
    @Override
    public void onCreate() {
        dis options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.ic_launcher)
                .displayer(new FadeInBitmapDisplayer(2000)).cacheOnDisc(true)
                .cacheInMemory(true).build();

        ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(
                this).defaultDisplayImageOptions(options).threadPoolSize(5)
                .build();

        ImageLoader.getInstance().init(configuration);

        super.onCreate();
    }
}

布局

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/lv"></ListView>


item_01.xml

 <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tv_01"
        android:layout_gravity="clip_horizontal"
        android:text="aaaa"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/img_01"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@mipmap/ic_launcher"/>

        <ImageView
            android:id="@+id/img_02"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@mipmap/ic_launcher" />

        <ImageView
            android:id="@+id/img_03"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@mipmap/ic_launcher" />
    </LinearLayout>

(不完整,慎重!!!)