自定义Adpter使用方法以及注意点

写一个ListView,里面的Item样式自定义,如下界面:



activity_white_pkg.xml

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:weightSum="1"
    tools:context="com.meitu.travelapk.activity.WhitePkgActivity">

    <ListView
        android:layout_weight="1"
        android:id="@+id/whitePkg_lsv"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"></ListView>


    <Button
        android:id="@+id/whitePkg_ok_btn"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:background="@color/colorPrimary"
        android:text="保  存"
        android:textColor="@android:color/white"
        android:textSize="@dimen/textSizeNormal" />
</LinearLayout>


编写item对应的界面item_white_pkg.xml

<?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"
    android:showDividers="end">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingBottom="5dp"
        android:paddingTop="5dp">

        <ImageView
            android:id="@+id/appIcon"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_gravity="center"
            android:src="@drawable/ic_info_black_24dp" />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="50dp"
            android:layout_weight="1"
            android:orientation="vertical">

            <TextView
                android:id="@+id/appName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="appName"
                android:textSize="@dimen/textSizeNormal" />

            <TextView
                android:id="@+id/pkgName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="appName"
                android:textSize="@dimen/textSizeSmall" />
        </LinearLayout>

        <CheckBox
            android:id="@+id/state_cbox"
            android:layout_width="40dp"
            android:layout_height="20dp"
            android:layout_gravity="center"
            android:clickable="false"
            android:focusable="false"
            android:focusableInTouchMode="false" />
    </LinearLayout>


    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="@color/lightGray"
        android:paddingTop="5dp" />

</LinearLayout>



对应的Activity,里面的ConfigData用于数据存储,AppInfo用于获取应用信息,此处不给出



import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import com.meitu.travelapk.R;
import com.meitu.travelapk.data.AppInfo;
import com.meitu.travelapk.data.ConfigData;
import com.meitu.travelapk.views.WhitePkgAdapter;

import java.util.ArrayList;

public class WhitePkgActivity extends AppCompatActivity  implements View.OnClickListener,AdapterView.OnItemClickListener{
    public static final int LOAD_LIST = 1;
    ListView whitePkg_lsv;
    Button ok_btn;
    ViewHandler viewHandler = new ViewHandler();
    //获取应用信息
    ArrayList<AppInfo> appList = AppInfo.getAllAppsInfo();

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_white_pkg);
        findViews();
        addEvents();
        initData();
    }

    void initData(){

        //        开启另一个线程加载应用数据,防止ANR
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                viewHandler.sendEmptyMessage(LOAD_LIST);
            }
        });
        thread.start();
    }

    void findViews() {
        whitePkg_lsv = (ListView) findViewById(R.id.whitePkg_lsv);
        ok_btn = (Button) findViewById(R.id.whitePkg_ok_btn);

    }

    void addEvents() {
        ok_btn.setOnClickListener(this);
        whitePkg_lsv.setOnItemClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case  R.id.whitePkg_ok_btn:

                Intent intent=new Intent();
                intent.setClass(WhitePkgActivity.this,MainActivity.class);
                startActivity(intent);
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        WhitePkgAdapter.ViewHolder vHolder = (WhitePkgAdapter.ViewHolder) view.getTag();
        AppInfo appInfo = appList.get(position);
        ArrayList<String> whiteList= ConfigData.getWhitePkgList();
        vHolder.checked.toggle();
        if (vHolder.checked.isChecked()&&!whiteList.contains(appInfo.packageName)){
                whiteList.add(appInfo.packageName);
        }else{
            whiteList.remove(appInfo.packageName);
        }

    }

    class ViewHandler extends Handler {
        public ViewHandler() {
        }

        // TODO Auto-generated constructor stub

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case LOAD_LIST:

                    //适配器
                    WhitePkgAdapter adapter = new WhitePkgAdapter(WhitePkgActivity.this, appList);
                    //加载适配器
                    whitePkg_lsv.setAdapter(adapter);

                    break;

            }
        }

    }
}


适配器


public class WhitePkgAdapter extends BaseAdapter {
    private Context mContext;
    private List<AppInfo> mList;

    public WhitePkgAdapter(Context context, List<AppInfo> list) {
        this.mContext = context;
        this.mList = list;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return mList.size();
    }

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

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

    public class ViewHolder {
        public ImageView icon;
        public TextView appName;
        public TextView pkgName;
        public CheckBox checked;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        LayoutInflater _LayoutInflater = LayoutInflater.from(mContext);
        ViewHolder holder = new ViewHolder();
        if (convertView == null) {
            convertView = _LayoutInflater.inflate(R.layout.item_white_pkg, null);
            holder.icon = (ImageView) convertView
                    .findViewById(R.id.appIcon);
            holder.appName = (TextView) convertView
                    .findViewById(R.id.appName);
            holder.pkgName = (TextView) convertView
                    .findViewById(R.id.pkgName);
            holder.checked = (CheckBox) convertView.findViewById(R.id.state_cbox);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.icon.setImageDrawable(mList.get(position).appIcon);
        holder.appName.setText(mList.get(position).appName);
        holder.pkgName.setText(mList.get(position).packageName);
        return convertView;
    }
}

《《《《注意》》》》》》

这里的getView方法的书写应遵循以下顺序:

1、如果拿到的convertView是空,则为holder赋上对应的值,并且将setTag, 以供后期holder的获取,

2、如果convertView已经有内容了,则直接使用getTag方法,

3、在确保holder内容非空了之后,统一为holder赋值,初始化item界面。






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值