美文网首页
Listview及其优化

Listview及其优化

作者: 在下陈小村 | 来源:发表于2018-03-02 15:53 被阅读4次

    整个ListView展示包含1个javabean,1个adapter,一个listview,一个承载list的activity,以及listview子项的布局。
    这里就贴一下代码吧

    public class Fruit {
        private String name;
        private int id;
        public Fruit(String name,int id){
            this.name=name;
            this.id=id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    }
    

    这里定义了一个viewholder来复用控件,避免重复创建控件

    public class FruitAdapter extends BaseAdapter {
        private List<Fruit>fruitList;
        private Context context;
        public FruitAdapter(Context context,List<Fruit> fruitList){
            this.fruitList=fruitList;
            this.context=context;
        }
        @Override
        public int getCount() {
            return fruitList.size();
        }
    
        @Override
        public Fruit getItem(int position) {
            return fruitList.get(position);
        }
    
        @Override
        public long getItemId(int position) {
            return position;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Fruit fruit=getItem(position);
            View view;
            ViewHolder holder;
            if (convertView==null) {
                view = LayoutInflater.from(context).inflate(R.layout.fruititem, parent, false);
                holder=new ViewHolder();
                holder.fruitName=view.findViewById(R.id.fruit_name);
                holder.fruitImage=view.findViewById(R.id.fruit_image);
                view.setTag(holder);
            }else {
                view=convertView;
                holder= (ViewHolder) view.getTag();
            }
            holder.fruitImage.setImageResource(fruit.getId());
            holder.fruitName.setText(fruit.getName());
            return view;
        }
        class ViewHolder {
            private TextView fruitName;
            private ImageView fruitImage;
        }
    }
    
    public class ListviewActivity extends AppCompatActivity {
        List<Fruit>fruitList=new ArrayList<>();
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_listview);
            initFruits();
            ListView fruitLV= (ListView) findViewById(R.id.lvFruit);
            FruitAdapter adapter=new FruitAdapter(this,fruitList);
            fruitLV.setAdapter(adapter);
        }
    
        private void initFruits() {
            for (int i = 0; i < 2; i++) {
                Fruit apple = new Fruit("Apple", R.drawable.apple_pic);
                fruitList.add(apple);
                Fruit banana = new Fruit("Banana", R.drawable.banana_pic);
                fruitList.add(banana);
                Fruit orange = new Fruit("Orange", R.drawable.orange_pic);
                fruitList.add(orange);
                Fruit watermelon = new Fruit("Watermelon", R.drawable.watermelon_pic);
                fruitList.add(watermelon);
                Fruit pear = new Fruit("Pear", R.drawable.pear_pic);
                fruitList.add(pear);
                Fruit grape = new Fruit("Grape", R.drawable.grape_pic);
                fruitList.add(grape);
                Fruit pineapple = new Fruit("Pineapple", R.drawable.pineapple_pic);
                fruitList.add(pineapple);
                Fruit strawberry = new Fruit("Strawberry", R.drawable.strawberry_pic);
                fruitList.add(strawberry);
                Fruit cherry = new Fruit("Cherry", R.drawable.cherry_pic);
                fruitList.add(cherry);
                Fruit mango = new Fruit("Mango", R.drawable.mango_pic);
                fruitList.add(mango);
            }
        }
    
    }
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ImageView
            android:id="@+id/fruit_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/fruit_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:textSize="15sp"
            android:layout_marginLeft="10dp"/>
    
    </LinearLayout>
    

    相关文章

      网友评论

          本文标题:Listview及其优化

          本文链接:https://www.haomeiwen.com/subject/wfvaxftx.html