- <pre name="code" class="java"></pre><pre name="code" class="java">关键点提示</pre><pre name="code" class="java">1-->Adapter需要重写getViewTyptCount,getItemViewTypt</pre><pre name="code" class="java"></pre><pre name="code" class="java">getView 方法中要写两个Holder进行优化;</pre><pre name="code" class="java"></pre><pre name="code" class="java"></pre><pre name="code" class="java">一 javabean对象
- package com.wzq.moreitemlistviewdemo;
- public class Goods {
- private String name;
- private String price;
- private int resId;
- private int typeId;
- public Goods(String name, String price, Integer typeId) {
- super();
- this.name = name;
- this.price = price;
- this.typeId = typeId;
- }
- public Goods(String name, String price, int resId, Integer typeId) {
- super();
- this.name = name;
- this.price = price;
- this.resId = resId;
- this.typeId = typeId;
- }
- public int getTypeId() {
- return typeId;
- }
- public void setTypeId(int typeId) {
- this.typeId = typeId;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getPrice() {
- return price;
- }
- public void setPrice(String price) {
- this.price = price;
- }
- public int getResId() {
- return resId;
- }
- public void setResId(int resId) {
- this.resId = resId;
- }
- @Override
- public String toString() {
- return "Goods [name=" + name + ", price=" + price + ", resId=" + resId
- + "]";
- }
- }
- </pre><br>
- <br>
- <pre></pre>
- <p></p>
- <pre></pre>
- <pre name="code" class="java"><pre name="code" class="java">第二listview和adapter的 写法
- package com.wzq.moreitemlistviewdemo;
- 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 butterknife.BindView;
- import butterknife.ButterKnife;
- /**
- * listview多条目加载,使用项目使用imageloader,butterknife;
- */
- public class MainActivity extends AppCompatActivity {
- @BindView(R.id.lv)
- ListView lv;
- private ArrayList<Goods> goodsList;
- String imgUrl = "http://image.tianjimedia.com/uploadImages/2012/067/N80N0GUA36N0.jpg";
- private ImageView img;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- //使用butterKnife来注解控件;
- ButterKnife.bind(this);
- initData();
- }
- private void initData() {
- goodsList = new ArrayList<Goods>();
- goodsList.add(new Goods("商品1", "21", 0));
- goodsList.add(new Goods("商品3", "23", 0));
- goodsList.add(new Goods("商品4", "24", R.drawable.hu, 1));
- goodsList.add(new Goods("商品2", "22", R.drawable.hu, 1));
- goodsList.add(new Goods("商品5", "25", 0));
- goodsList.add(new Goods("商品6", "26", R.drawable.hu, 1));
- goodsList.add(new Goods("商品21", "21", 0));
- goodsList.add(new Goods("商品5", "25", 0));
- goodsList.add(new Goods("商品22", "22", R.drawable.hu, 1));
- goodsList.add(new Goods("商品23", "23", 0));
- goodsList.add(new Goods("商品24", "24", R.drawable.hu, 1));
- goodsList.add(new Goods("商品25", "25", 0));
- goodsList.add(new Goods("商品26", "26", R.drawable.hu, 1));
- goodsList.add(new Goods("商品31", "21", 0));
- goodsList.add(new Goods("商品32", "22", R.drawable.hu, 1));
- goodsList.add(new Goods("商品34", "24", R.drawable.hu, 1));
- goodsList.add(new Goods("商品36", "26", R.drawable.hu, 1));
- goodsList.add(new Goods("商品33", "23", 0));
- goodsList.add(new Goods("商品35", "25", 0));
- //把数据配置给listview
- lv.setAdapter(new MBaseAdapter());
- }
- class MBaseAdapter extends BaseAdapter {
- private final int type1 = 0;
- private final int type2 = 1;
- @Override
- public int getCount() {
- return goodsList.size();
- }
- @Override
- public Object getItem(int i) {
- return goodsList.get(i);
- }
- @Override
- public long getItemId(int i) {
- return i;
- }
- @Override
- public View getView(int i, View convertView, ViewGroup viewGroup) {
- int typeId = goodsList.get(i).getTypeId();
- switch (typeId) {
- case type1:
- Holder1 holder1 = null;
- if (convertView == null) {
- holder1 = new Holder1();
- convertView = View.inflate(MainActivity.this, R.layout.lv_item_1, null);
- holder1.textView1 = convertView.findViewById(R.id.textView1);
- holder1.textView2 = convertView.findViewById(R.id.textView2);
- convertView.setTag(holder1);
- } else {
- holder1 = (Holder1) convertView.getTag();
- }
- holder1.textView1.setText(goodsList.get(i).getName());
- holder1.textView2.setText(goodsList.get(i).getPrice());
- break;
- case type2:
- Holder2 holder2 = null;
- if (convertView == null) {
- holder2 = new Holder2();
- convertView = View.inflate(MainActivity.this, R.layout.lv_item_2, null);
- holder2.textView1 = convertView.findViewById(R.id.textView1);
- holder2.textView2 = convertView.findViewById(R.id.textView2);
- holder2.imageView1 = convertView.findViewById(R.id.imageView1);
- convertView.setTag(holder2);
- } else {
- holder2 = (Holder2) convertView.getTag();
- }
- holder2.textView1.setText(goodsList.get(i).getName());
- holder2.textView2.setText(goodsList.get(i).getPrice());
- // holder2.imageView1.setImageResource(R.drawable.hu);
- ImageLoader.getInstance().displayImage(imgUrl, holder2.imageView1);
- break;
- }
- return convertView;
- }
- //返回条目有多少种类型
- @Override
- public int getViewTypeCount() {
- return 2;
- }
- //返回条目的类型
- @Override
- public int getItemViewType(int position) {
- return goodsList.get(position).getTypeId();
- }
- }
- class Holder1 {
- TextView textView1, textView2;
- }
- class Holder2 {
- TextView textView1, textView2;
- ImageView imageView1;
- }
- }
- </pre><br>
- <br>
- <pre></pre>
- <pre name="code" class="java"></pre><pre name="code" class="java"></pre><pre name="code" class="java"></pre><pre name="code" class="java"></pre><pre name="code" class="java"></pre><p></p></pre>
ListView多条目
最新推荐文章于 2017-11-08 15:28:30 发布