在Adapter中创建方法,改变Adapter显示效果

1、用于Adapter显示的数据。即 ,MVC中的Model(这里只做声明):

private ArrayList<SubScriptionCategory> mCategoryList;


2、MVC中的View显示Adapter代码:

private class CategoryAdapter extends BaseAdapter
	{
		private int onClickPos = -1;// 选中的位置

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

		@Override
		public Object getItem(int position)
		{
			return mCategoryList.get(position);
		}

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

		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			View view = getLayoutInflater().inflate(
					R.layout.subscription_category_list_item, null);

			ViewHolder holder = new ViewHolder();

			holder.mTvCategory = (TextView) view
					.findViewById(R.id.tv_subscription_category_item);

			SubScriptionCategory category = mCategoryList.get(position);
			Drawable drawable = null;

			if (position == onClickPos)
			{
				view.setBackgroundColor(getResources().getColor(
						R.color.color_d72625));
			} else
			{
				view.setBackgroundColor(android.R.color.transparent);
			}

			if (category.categoryid.equals("1"))
			{
				drawable = getResources()
						.getDrawable(R.drawable.menu_left_mine);
				// 必须设置图片大小,否则不显示
				drawable.setBounds(0, 0, drawable.getMinimumWidth(),
						drawable.getMinimumHeight());

			} else if (category.categoryid.equals("2"))
			{
				drawable = getResources().getDrawable(
						R.drawable.menu_left_sports);
				drawable.setBounds(0, 0, drawable.getMinimumWidth(),
						drawable.getMinimumHeight());
			} else if (category.categoryid.equals("3"))
			{
				drawable = getResources().getDrawable(
						R.drawable.menu_left_female);
				drawable.setBounds(0, 0, drawable.getMinimumWidth(),
						drawable.getMinimumHeight());
			} else if (category.categoryid.equals("4"))
			{
				drawable = getResources().getDrawable(
						R.drawable.menu_left_integrate);
				drawable.setBounds(0, 0, drawable.getMinimumWidth(),
						drawable.getMinimumHeight());
			}
			holder.mTvCategory.setCompoundDrawables(drawable, null, null, null);
			holder.mTvCategory.setText(category.name);

			return view;
		}

		private class ViewHolder
		{
			private TextView mTvCategory;
		}

		/**
		 * 设置选中位置
		 * 
		 * @param pos
		 */
	public void setSelectedPos(int pos)
		{
			onClickPos = pos;
			// 更新显示
			notifyDataSetChanged();
		}

	}


3、MVC中的control(listView):

private ListView mLvCategory;
mLvCategory = (ListView) findViewById(R.id.lv_subscription_category);
// 设置分类适配器
mLvCategory.setOnItemClickListener(this);

	// 点击分类的Item项
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id)
	{
		// 更新分类Item的背景显示
		mCategoryAdapter.setSelectedPos(position);
	
	}




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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值