1. 程式人生 > >資料介面卡---BaseAdapter

資料介面卡---BaseAdapter

文章詳細介紹了資料介面卡BaseAdapter三種使用方法。

程式碼:

Xml檔案:activity_main

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.third_six_baseadapter.MainActivity">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/lv_view">
    </ListView>
</RelativeLayout>
item_layout::
<?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:id="@+id/iv_image"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:src="@mipmap/ic_launcher"/>
    <TextView
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="title"
        android:id="@+id/tv_title"
        android:layout_toRightOf="@+id/iv_image"
        android:textSize="25sp"/>
    <TextView
        android:layout_below="@+id/tv_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="context"
        android:gravity="center_vertical"
        android:id="@+id/tv_context"
        android:layout_toRightOf="@+id/iv_image"
        android:textSize="20sp"/>

</RelativeLayout>
MainActivity部分程式碼:
package com.example.third_six_baseadapter;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        List<ItemBean> beanList = new ArrayList<ItemBean>();
        for(int i=0;i<20;i++) {
            beanList.add(new ItemBean("我是內容"+i,R.mipmap.ic_launcher,"我是標題"+i));
        }
        ListView mListView = (ListView) findViewById(R.id.lv_view);
        mListView.setAdapter(new MyAdapter(this,beanList));
    }
}
建立ItemBean函式,封裝資料:
package com.example.third_six_baseadapter;


public class ItemBean {
    public int item_iamge;
    public String item_title;
    public String item_context;

    public ItemBean(String item_context, int item_iamge, String item_title) {
        this.item_context = item_context;
        this.item_iamge = item_iamge;
        this.item_title = item_title;
    }
}
自定義介面卡,三種模式,逗比式,普通式,文藝式:
package com.example.third_six_baseadapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.content.Context;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public class MyAdapter extends BaseAdapter {
    private List<ItemBean> mListBean;
    private LayoutInflater mInflater;

    public MyAdapter(Context context, List<ItemBean> mList) {
        mListBean = mList;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mListBean.size();
    }

    @Override
    public Object getItem(int i) {
        return mListBean.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        //逗比式
//        View vw = mInflater.inflate(R.layout.item_layout,null);
//        ImageView image = (ImageView) vw.findViewById(R.id.iv_image);
//        TextView tv1 = (TextView) vw.findViewById(R.id.tv_title);
//        TextView tv2 = (TextView) vw.findViewById(R.id.tv_context);
//        ItemBean bean = mListBean.get(i);
//        image.setImageResource(bean.item_iamge);
//        tv1.setText(bean.item_title);
//        tv2.setText(bean.item_context);
//        return vw;

        //普通式
//        if (view == null) {
//            view = mInflater.inflate(R.layout.item_layout,null);
//        }
//        ImageView image = (ImageView) view.findViewById(R.id.iv_image);
//        TextView tv1 = (TextView) view.findViewById(R.id.tv_title);
//        TextView tv2 = (TextView) view.findViewById(R.id.tv_context);
//        ItemBean bean = mListBean.get(i);
//        image.setImageResource(bean.item_iamge);
//        tv1.setText(bean.item_title);
//        tv2.setText(bean.item_context);
//        return view;

        //文藝式
        ViewHolder mViewHolder;
        if (view == null) {
            mViewHolder = new ViewHolder();
            view = mInflater.inflate(R.layout.item_layout,null);
            mViewHolder.image = (ImageView) view.findViewById(R.id.iv_image);
            mViewHolder.tv1 = (TextView) view.findViewById(R.id.tv_title);
            mViewHolder.tv2 = (TextView) view.findViewById(R.id.tv_context);
            view.setTag(mViewHolder);
        } else {
            mViewHolder = (ViewHolder) view.getTag();
        }
        ItemBean bean = mListBean.get(i);
        mViewHolder.image.setImageResource(bean.item_iamge);
        mViewHolder.tv1.setText(bean.item_title);
        mViewHolder.tv2.setText(bean.item_context);
        return view;
    }

    class ViewHolder {
        ImageView image;
        TextView tv1;
        TextView tv2;
    }
}