Android 自定义适配器

项目开发中经常需要去自定义适配器,若自定义适配器写得好整个界面就会非常的流畅,反之程序就歇菜了。
下面先贴代码,再讲讲自定义适配器的过程中需要注意的细节。

首先是Activity的代码:

package com.example.textviewtest;

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

public class MainActivity extends Activity{
    private static final String TAG = "MainActivity";
    private String[] data = {"三星","小米","魅族","苹果","华为","一加","荣耀","Mate7","努比亚","OPPO","vivo","大神","乐视","小辣椒","P8","奇酷","锤子","金立"};
    private ListView mListView;
    private Context mContext;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = this;
        mListView = (ListView) this.findViewById(R.id.mListView);
        MyAdapter mAdapter = new MyAdapter(mContext,data);
        mListView.setAdapter(mAdapter);
    }

}

自定义适配器的代码:

package com.example.textviewtest;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MyAdapter extends BaseAdapter{
    private Context mContext;
    private String[] data;  
    public MyAdapter(Context mContext, String[] data) {
        this.mContext = mContext;
        this.data = data;
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return data.length;
    }
    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }
    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup viewGroup) {
        View myView ;
        ItemViewHolder itemViewHolder;
        if(view == null) {
             myView = View.inflate(mContext, R.layout.my_adapter, null);
             itemViewHolder = new ItemViewHolder();
             itemViewHolder.mTextView = (TextView) myView.findViewById(R.id.mTextView);
             itemViewHolder.mButton = (Button) myView.findViewById(R.id.mButton);
             myView.setTag(itemViewHolder);
        } else {
            myView = view;
            itemViewHolder = (ItemViewHolder) view.getTag();
        }                   
        itemViewHolder.mTextView.setText(data[position]);       
        itemViewHolder.mButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {            
                Toast.makeText(mContext, data[position], Toast.LENGTH_SHORT).show();                
            }           
        });
        myView.setTag(itemViewHolder);
        return myView;
    }

    public class ItemViewHolder {       
        public TextView mTextView;
        public Button mButton;      
    }

}

1、在适配器的构造函数中,我们将需要的数据源传入
2、利用Android的Recycler机制,利用convertView来重新回收View,避免每次都去创建新的View。
3、使用ViewHolder将需要缓存的view封装好,convertView的setTag就是将这些缓存起来供下次调用。因此,当第二次使用的时候只需要通过getTag()方法就可以获得ViewHolder对象

你可能感兴趣的:(Android)