An_ListView 多样展示

2017-08-25  本文已影响9人  android_en

MyApplication类:加载ImageLoader的实现
!!!!别忘了加权限和
在清单里加上MyApplication:

 <application
//name---->        
android:name="com.example.listview_3item.MyApplication"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
package com.example.listview_3item;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.DisplayImageOptions.Builder;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;

import android.app.Application;

public class MyApplication extends Application{
@Override
public void onCreate() {
    // TODO Auto-generated method stub
    DisplayImageOptions options = new DisplayImageOptions.Builder()
    .showImageOnLoading(R.drawable.ic_launcher)
    .displayer(new FadeInBitmapDisplayer(3000))
    .build();
    ImageLoaderConfiguration config = ImageLoaderConfiguration.createDefault(this);
    ImageLoader.getInstance().init(config);
    super.onCreate();

MainActivity 加载图片放到listview

package com.example.listview_3item;

import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import com.example.listview_3item.bean.Max;
import com.example.listview_3item.bean.news;
import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;

public class MainActivity extends Activity {
List<news> list;
private MyAdapter myAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ListView lv=(ListView) findViewById(R.id.lv);
        getData();
        myAdapter = new MyAdapter();
        lv.setAdapter(myAdapter);
    }

    private void getData() {
        // TODO Auto-generated method stub
        new AsyncTask<String, String, String>() {

            @Override
            protected String doInBackground(String... params) {
                // TODO Auto-generated method stub
                initData();
                return null;
            }
            @Override
            protected void onPostExecute(String result) {
                // TODO Auto-generated method stub
                myAdapter.notifyDataSetChanged();
                super.onPostExecute(result);
            }
        }.execute();
    }

    protected void initData() {
        // TODO Auto-generated method stub
        try {
            URL url = new URL("http://169.254.122.215:8080/day10/news.json");
            HttpURLConnection conn=(HttpURLConnection) url.openConnection();
            int code = conn.getResponseCode();
            if(code==200){
                Gson gson = new Gson();
                Max m = gson.fromJson(new InputStreamReader(conn.getInputStream(),"GBK"), Max.class);
                list=new ArrayList<news>();
                list=m.getData();
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    class MyAdapter extends BaseAdapter{
        public static final int TPONE=0;
        public static final int TPTWO=1;
        public static final int TPTHREE=2;
        
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            if(list!=null){
                return list.size();
            }
            return 0;
        }
        
        //在使用多条目加载的时候    返回当前position对应的类型
        //************必须满足的条件   getItemViewType的返回值  必须要小于getViewTypeCount的返回值
        @Override
        public int getItemViewType(int position) {
            // TODO Auto-generated method stub
            if(list.get(position).getType()==10001){
                return TPONE;
            }else if(list.get(position).getType()==10002){
                return TPTWO;
            }else {
                return TPTHREE;
            }
            
        }

        //使用多条目加载时   一共有几种类型
        //从写该方法后,listview控件会知道  当前的布局样式不止一种  
        //所以 当ListView发现convertview在复用时  类型不配不上时  会重写让convertView加载一遍布局
        @Override
        public int getViewTypeCount() {
            // TODO Auto-generated method stub
            return 3;
        }


        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            ViewHolder0 holder0=null;
            ViewHolder1 holder1=null;
            ViewHolder2 holder2=null;
            int type=getItemViewType(position);
            if(convertView==null){
                if(type==TPONE){
                    convertView=View.inflate(MainActivity.this, R.layout.item0,null);
                    holder0=new ViewHolder0();
                    holder0.ivleft=(ImageView) convertView.findViewById(R.id.ivleft);
                    convertView.setTag(holder0);
                }else if(type==TPTWO){
                    convertView=View.inflate(MainActivity.this, R.layout.item1,null);
                    holder1=new ViewHolder1();
                    holder1.ivm=(ImageView) convertView.findViewById(R.id.ivm);
                    convertView.setTag(holder1);
                }else{
                    convertView=View.inflate(MainActivity.this, R.layout.item2,null);
                    holder2=new ViewHolder2();
                    holder2.ivright=(ImageView) convertView.findViewById(R.id.ivright);
                    convertView.setTag(holder2);
                }
            }else{
                if(type==TPONE){
                    holder0=(ViewHolder0) convertView.getTag();
                }else if(type==TPTWO){
                    holder1=(ViewHolder1) convertView.getTag();
                }else if(type==TPTHREE){
                    holder2=(ViewHolder2) convertView.getTag();
                }
            }
            if(type==TPONE){
                ImageLoader.getInstance().displayImage(list.get(position).getPic_url(), holder0.ivleft);
            }else if(type==TPTWO){
                ImageLoader.getInstance().displayImage(list.get(position).getPic_url(), holder1.ivm);
            }else if(type==TPTHREE){
                ImageLoader.getInstance().displayImage(list.get(position).getPic_url(), holder2.ivright);

            }
            return convertView;
        }
        
    }
    static class ViewHolder0{
        ImageView ivleft;
    }
    static class ViewHolder1{
        ImageView ivm;
    }
    static class ViewHolder2{
        ImageView ivright;
    }

    
    
}

布局 item0

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
<ImageView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/ivleft"
    android:src="@drawable/ic_launcher"
    />


</LinearLayout>

布局 item1

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
<ImageView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/ivm"
    android:layout_gravity="center_horizontal"
    android:src="@drawable/ic_launcher"
    />
</LinearLayout>

布局 item2

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    
<ImageView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/ivright"
    android:src="@drawable/ic_launcher"
    android:layout_alignParentRight="true"
    />
</RelativeLayout>

}
}

上一篇下一篇

猜你喜欢

热点阅读