BaseAdapter使用

一:在ListView里面添加view

ListView.setAdapter(new LabelAdapter());


private String  tvlist[] =new String[]{"精彩推荐","新闻咨询","美食旅行","生活购物","美图搞笑","社交交友"};

class  LabelAdapter extends BaseAdapter{

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return tvlist.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(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        TextView tv ;    //这里可以是任何自己定义的View
        if (convertView == null) {
            convertView = new TextView(MainActivity.this);
        }
        tv = (TextView) convertView;
        tv.setTextSize(18);
        tv.setText(tvlist[position]);  //设置view的属性
        return convertView;
    }

   }

二:在ListView里面添加自定义的view


    private class LabelAdapter extends AbsBaseAdapter {
        @Override
        public int getCount() {
            if (mDesktopGroups == null) {
                return 0;
            }
            return mDesktopGroups.size(); //分组个数
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LabelView tv;   //自定view
            if (convertView == null) {
                convertView = new LabelView(mContext);
            }
            tv = (LabelView) convertView;

            //下面设置各个view的属性

            if (position == 0) {
                tv.setText(mContext.getString(R.string.quicklinks_top_suggest));
            } else {
                tv.setText(mDesktopGroups.get(position).getLabel());
            }
            if (position == mSelectedItemPos) { //被选中后颜色发生变化
                tv.setTextColor(mContext.getResources().getColor(R.color.text_color_hilighted));
            } else {
                tv.setTextColor(mContext.getResources().getColor(R.color.list_item_text_normal_color));
            }
            tv.setPosition(position);
            return convertView;
        }
    }

//下面为listView中添加的自定义view    LabelView

   private class LabelView extends TextView implements View.OnClickListener{
        private int position = -1;
        private int height;
        private int paddingLeft;
        private int textSize = 16;
        public LabelView(Context context) {
            super(context);
            height = context.getResources().getDimensionPixelOffset(R.dimen.desktop_man_item_height);
            paddingLeft = context.getResources().getDimensionPixelOffset(R.dimen.desktop_man_paddingleft);
            this.setPadding(paddingLeft, 0, 0, 0);
            this.setGravity(Gravity.CENTER_VERTICAL);
            this.setTextSize(textSize);
            this.setBackgroundResource(R.drawable.desktopmanager_label_bg);
            this.setMaxLines(1);
            this.setOnClickListener(this);
        }
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            int width = MeasureSpec.getSize(widthMeasureSpec);
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            this.setMeasuredDimension(width, height);
        }
        public void setPosition(int pos) {
            this.position = pos;
        }
        @Override
        public void onClick(View v) {
            mSelectedItemPos = position;
            notifyDataChange();
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值