在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);
	
	}




發佈了37 篇原創文章 · 獲贊 15 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章