HolderView 与 ViewHolder做Android的伙伴都不会陌生吧,都是为了提升ListView、gridView等这类View的adapter性能,
把多个属性封装到一个类中,也就是面向对象的思想。
ViewHolder实在adapter中建立一个对象,但是没有做任何的业务逻辑,所有的逻辑还是在adapter的getView中实现,这样就显得
adapter特别臃肿。
HolderView就是为了解决adapter的臃肿问题,把逻辑处理方到HolderView中实现,在adapter中只是调用HolderView就ok了
实现代码:
- public class HolderView extends GridLayout {
- private TextView mDigitDigit;
- private TextView mDigitHex;
- private TextView mDigitOct;
- private TextView mDigitEven;
- private TextView mDigitPrime;
- private TextView mDigitFibonacci;
- private String mDigitText;
- private String mDigitHexText;
- private String mDigitOctText;
- private String mDigitEvenText;
- private String mDigitPrimtText;
- private String mDigitFibonacciText;
- public HolderView(Context context) {
- super(context);
- initlialize(context);
- }
- public HolderView(Context context, AttributeSet attrs) {
- super(context, attrs);
- initlialize(context);
- }
- public HolderView(Context context, AttributeSet attrs, int defStyle) {
- super(context, attrs, defStyle);
- initlialize(context);
- }
- public void initlialize(Context context) {
- View v = LayoutInflater.from(context).inflate(R.layout.list_detail, this);
- mDigitDigit = (TextView) v.findViewById(R.id.list_detail_digit);
- mDigitHex = (TextView) v.findViewById(R.id.list_detail_digit_hex);
- mDigitOct = (TextView) v.findViewById(R.id.list_detail_digit_oct);
- mDigitEven = (TextView) v.findViewById(R.id.list_detail_digit_even);
- mDigitPrime = (TextView) v.findViewById(R.id.list_detail_digit_prime);
- mDigitFibonacci = (TextView) v.findViewById(R.id.list_detail_digit_fibonacci);
- mDigitText = context.getResources().getString(R.string.list_detail_digit);
- mDigitHexText = context.getResources().getString(R.string.list_detail_digit_hex);
- mDigitOctText = context.getResources().getString(R.string.list_detail_digit_oct);
- mDigitEvenText = context.getResources().getString(R.string.list_detail_digit_even);
- mDigitPrimtText = context.getResources().getString(R.string.list_detail_digit_prime);
- mDigitFibonacciText = context.getResources().getString(R.string.list_detail_digit_fibonacci);
- }
- public void bind(Digit digit) {
- mDigitDigit.setText(String.format(mDigitText, digit));
- mDigitOct.setText(String.format(mDigitOctText, digit.toOctalString()));
- mDigitHex.setText(String.format(mDigitHexText, digit.toHexString()));
- mDigitEven.setText(String.format(mDigitEvenText, digit.isEven()));
- mDigitPrime.setText(String.format(mDigitPrimtText, digit.isPrime()));
- mDigitFibonacci.setText(String.format(mDigitFibonacciText, digit.isFibonacci()));
- }
- }
adapter:
- public class ItemAdapter extends BaseAdapter {
- private final Context mContext;
- private int count = 256;
- public ItemAdapter(Context context) {
- mContext = context;
- }
- @Override
- public int getCount() {
- return count;
- }
- @Override
- public Object getItem(int i) {
- return i;
- }
- @Override
- public long getItemId(int i) {
- return i;
- }
- @Override
- public View getView(int i, View convertView, ViewGroup viewGroup) {
- HolderView holderView;
- // Important to not just null check, but rather to a instanceof
- // since we might get any subclass of view here.
- if (convertView instanceof HolderView) {
- holderView = (HolderView) convertView;
- } else {
- holderView = new HolderView(mContext);
- }
- holderView.bind(new Digit(i));
- return holderView;
- }
- }
list_dateil.xml
- <?xml version="1.0" encoding="utf-8"?>
- <merge xmlns:android="http://schemas.android.com/apk/res/android" >
- <TextView
- android:id="@+id/list_detail_digit"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_column="0"
- android:layout_gravity="left|top"
- android:layout_row="0"
- android:padding="8dip" />
- <TextView
- android:id="@+id/list_detail_digit_oct"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_column="1"
- android:layout_gravity="left|top"
- android:layout_row="0"
- android:padding="8dip" />
- <TextView
- android:id="@+id/list_detail_digit_hex"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_column="2"
- android:layout_gravity="left|top"
- android:layout_row="0"
- android:padding="8dip" />
- <TextView
- android:id="@+id/list_detail_digit_even"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_column="0"
- android:layout_gravity="left|top"
- android:layout_row="1"
- android:padding="8dip" />
- <TextView
- android:id="@+id/list_detail_digit_prime"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_column="1"
- android:layout_gravity="left|top"
- android:layout_row="1"
- android:padding="8dip" />
- <TextView
- android:id="@+id/list_detail_digit_fibonacci"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_column="2"
- android:layout_gravity="left|top"
- android:layout_row="1"
- android:padding="8dip" />
- </merge>