现在 布局文件内 写两个不同的布局
布局1 只有文字:
<?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="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tv1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tv2" android:layout_marginLeft="10dp" /> </LinearLayout> 布局2 文字加图片:<?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="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="啊哈哈哈" android:id="@+id/tv1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="啊哈哈哈" android:id="@+id/tv2" android:layout_marginLeft="10dp" /> <ImageView android:layout_width="70dp" android:layout_height="70dp" android:id="@+id/ig" android:background="@color/colorAccent" android:layout_marginLeft="20dp" /> </LinearLayout>创建一个自定义框架 为ListView 的图片属性设置网络加载的图片
public class MApp extends Application { //需要在清单文件里加载name属性 需要在类里加载依赖 @Override public void onCreate() { super.onCreate(); ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this).build(); ImageLoader.getInstance().init(config); //加载自定义框架 } }
创建一个Bean类为条目添加对象:package com.bwei.ssp.day_notlistview; /** * Created by lenovo on 2017/10/12. */ public class Goods { private String name; private String price; private int imgId; private int typeId; //根据不同条目的需求创建不同的构造方法 public Goods(String name, String price, int typeId) { this.name = name; this.price = price; this.typeId = typeId; } public Goods(String name, String price, int imgId, int typeId) { this.name = name; this.price = price; this.imgId = imgId; this.typeId = typeId; } public void setName(String name) { this.name = name; } public void setPrice(String price) { this.price = price; } public void setImgId(int imgId) { this.imgId = imgId; } public void setTypeId(int typeId) { this.typeId = typeId; } public String getName() { return name; } public String getPrice() { return price; } public int getImgId() { return imgId; } public int getTypeId() { return typeId; } }
在主累里进行操作:package com.bwei.ssp.day_notlistview; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import com.nostra13.universalimageloader.core.ImageLoader; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private ListView lv; private ImageLoader instance; //创建框架对象 private List<Goods> list; String imgUrl = "http://image.tianjimedia.com/uploadImages/2012/067/N80N0GUA36N0.jpg"; //创建网络图片地址 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv=(ListView) findViewById(R.id.lv); instance = ImageLoader.getInstance(); initData(); //在方法里进行操作 } private void initData() { list =new ArrayList<>(); //实例化一个集合 list.add(new Goods("商品3", "23", 0)); list.add(new Goods("商品4", "24", R.drawable.hu, 1)); list.add(new Goods("商品1", "21", 0)); list.add(new Goods("商品2", "22", R.drawable.hu, 1)); list.add(new Goods("商品5", "25", 0)); list.add(new Goods("商品6", "26", R.drawable.hu, 1)); list.add(new Goods("商品21", "21", 0)); list.add(new Goods("商品5", "25", 0)); list.add(new Goods("商品22", "22", R.drawable.hu, 1)); list.add(new Goods("商品23", "23", 0)); list.add(new Goods("商品24", "24", R.drawable.hu, 1)); list.add(new Goods("商品25", "25", 0)); list.add(new Goods("商品26", "26", R.drawable.hu, 1)); list.add(new Goods("商品31", "21", 0)); list.add(new Goods("商品32", "22", R.drawable.hu, 1)); list.add(new Goods("商品34", "24", R.drawable.hu, 1)); list.add(new Goods("商品36", "26", R.drawable.hu, 1)); list.add(new Goods("商品33", "23", 0)); list.add(new Goods("商品35", "25", 0)); //为集合添加对象 lv.setAdapter( new Madapter()); //设置适配器 } class Madapter extends BaseAdapter { private final int TYPE0 = 0;//只有文字的条目 private final int TYPE1 = 1;//文字+图片的条目 //分别加载的条目为哪类布局的文件 @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 int getViewTypeCount() { return 2; } //有多少个条目类型 @Override public int getItemViewType(int position) { return list.get(position).getTypeId(); } //通过那个属性区别 @Override public View getView(int i, View view, ViewGroup viewGroup) { int type = getItemViewType(i); //返回条目类型的判断条件 Holder0 holder0; Holder1 holder1; switch (type) { case TYPE0: if (view == null) { holder0 = new Holder0(); view = View.inflate(MainActivity.this, R.layout.item, null); holder0.textView1 = (TextView) view.findViewById(R.id.tv1); holder0.textView2 = (TextView) view.findViewById(R.id.tv2); view.setTag(holder0); } else { holder0 = (Holder0) view.getTag(); } holder0.textView1.setText(list.get(i).getName()); holder0.textView2.setText(list.get(i).getPrice()); break; case TYPE1: if (view == null) { holder1 = new Holder1(); view = View.inflate(MainActivity.this, R.layout.item2, null); holder1.textView1 = (TextView) view.findViewById(R.id.tv1); holder1.textView2 = (TextView) view.findViewById(R.id.tv2); holder1.imageView1 = (ImageView) view.findViewById(R.id.ig); view.setTag(holder1); } else { holder1 = (Holder1) view.getTag(); } holder1.textView1.setText(list.get(i).getName()); holder1.textView2.setText(list.get(i).getPrice()); instance.displayImage(imgUrl, holder1.imageView1); break; } return view; } } class Holder0 { private TextView textView1; private TextView textView2; } class Holder1 { private TextView textView1; private TextView textView2; private ImageView imageView1; } }