我有一个GridView作为我的MainActivity,包含11个带有Clickable Listener的项目。每个单元格都包含一个ImageView和TextView。我想给出11个不同背景颜色的每一个。
这是我的物品数组
public static String [] prgmNameList={"TownHall 1","TownHall 2",
"TownHall 3","TownHall 4","TownHall 5 ","TownHall 6",
"TownHall 7","TownHall 8","TownHall 9","TownHall 10","TownHall 11"};
public static int [] prgmImages={R.drawable.town_hall1,
R.drawable.town_hall2,R.drawable.town_hall3,
R.drawable.town_hall4,R.drawable.town_hall5,
R.drawable.town_hall6,R.drawable.town_hall7,
R.drawable.town_hall8,R.drawable.town_hall9,
R.drawable.town_hall10, R.drawable.town_hall11};
这是我的GridView适配器代码
public class CustomAdapterMainActivity extends BaseAdapter {
String [] result;
Context context;
int [] imageId;
private static LayoutInflater inflater=null;
public CustomAdapterMainActivity(MainActivity mainActivity, String[] prgmNameList, int[] prgmImages) {
// TODO Auto-generated constructor stub
result=prgmNameList;
context=mainActivity;
imageId=prgmImages;
inflater = ( LayoutInflater )context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return result.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public class Holder
{
TextView tv;
ImageView img;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Holder holder=new Holder();
View rowView;
ImageView img;
rowView = inflater.inflate(R.layout.each_item_gridview, null);
holder.tv=(TextView) rowView.findViewById(R.id.textView1);
holder.img=(ImageView) rowView.findViewById(R.id.imageView1);
holder.tv.setText(result[position]);
holder.img.setImageResource(imageId[position]);
rowView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, "You Clicked " + result[position], Toast.LENGTH_LONG).show();
Class<? extends Activity> activityToStart = null;
switch (position) {
case 0:
activityToStart = TownHallElevenActivity.class;
break;
case 1:
activityToStart = TownHallElevenActivity.class;
break;
case 2:
activityToStart = TownHallElevenActivity.class;
break;
case 3:
activityToStart = TownHallElevenActivity.class;
break;
case 4:
activityToStart = TownHallElevenActivity.class;
break;
case 5:
activityToStart = TownHallElevenActivity.class;
break;
case 6:
activityToStart = TownHallElevenActivity.class;
break;
case 7:
activityToStart = TownHallElevenActivity.class;
break;
case 8:
activityToStart = TownHallNineActivity.class;
break;
case 9:
activityToStart = TownHallTenActivity.class;
break;
case 10:
activityToStart = TownHallElevenActivity.class;
break;
default:
break;
}
Intent i = new Intent(context.getApplicationContext(), activityToStart);
i.putExtra("classFrom", activityToStart.toString());
context.startActivity(i);
}
});
return rowView;
}
}
答案 0 :(得分:1)
将此添加到你的getView():
rowView.setBackgroundColor(colors[position]));
并添加如下颜色数组:
public static int [] colors { 0x225522, 0xff00000, .... }
答案 1 :(得分:0)
使用此代码就可以了!
1)你的班级假设类别类:
public class Category extends Fragment implements View.OnClickListener {
CategoryAdapter catAdpater;
FragmentController controller;
FragmentCommunicator communicator;
ArrayList<Integer> catImages = new ArrayList<>();
GridView catGrid;
String[] gridItem = {
"Technology",
"Food & Drink",
"Catering",
"Education",
"Travel",
"Accessories",
"Cleaners",
"Cloth",
"Random"
};
String[] gridColor = {
"#3c6169",
"#30515d",
"#284b5e",
"#224156",
"#86a6a1",
"#729493",
"#3c6169",
"#30515d",
"#224156",
"#3c6169"
};
LinearLayout categoryLayout;
ImageView back;
public Category() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
final Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_category, container, false);
//controller = new FragmentController();
//communicator = (FragmentCommunicator)getActivity();
//MrVector.inflate(getResources(), R.drawable.phone);
catGrid = (GridView)view.findViewById(R.id.catGrid);
categoryLayout = (LinearLayout)view.findViewById(R.id.header);
back =(ImageView)view.findViewById(R.id.back);
categoryLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//empty
}
});
back.setOnClickListener(this);
ArrayList<String> categories = new ArrayList<>();
categories.add("Technology");
categories.add("Food & Drink");
categories.add("Catering");
categories.add("Education");
categories.add("Travel");
categories.add("Accessories");
categories.add("Cleaners");
categories.add("Cloth");
categories.add("Random");
catImages.add(R.drawable.ic_phone_iphone_white_48dp);
catImages.add(R.drawable.ic_local_drink_white_48dp);
catImages.add(R.drawable.ic_restaurant_white_48dp);
catImages.add(R.drawable.ic_school_white_48dp);
catImages.add(R.drawable.ic_airplanemode_active_white_48dp);
catImages.add(R.drawable.ic_watch_white_48dp);
catImages.add(R.drawable.ic_phone_iphone_white_48dp);
catImages.add(R.drawable.ic_action_accessibility);
catImages.add(R.drawable.ic_store_mall_directory_white_48dp);
catAdpater = new CategoryAdapter(this.getContext(),categories,catImages,gridColor);
catGrid.setAdapter(catAdpater); }
2)您的自定义适配器:
public class CategoryAdapter extends BaseAdapter {
ArrayList<String> cat_name;
ArrayList<Integer> images;
private Context mContext;
private LayoutInflater mInflater;
private final String[] gridcolor;
public CategoryAdapter(Context mContext, ArrayList<String> cat_name, ArrayList<Integer> images,String[] gridcolor)
{
this.mContext = mContext;
this.cat_name = cat_name;
this.gridcolor = gridcolor;
this.images = images;
mInflater = LayoutInflater.from(mContext);
}
@Override
public int getCount() {
return images.size();
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
CategoryHolder holder = null;
if(row == null)
{
row = mInflater.inflate(R.layout.category_item, parent, false);
holder = new CategoryHolder(row);
row.setTag(holder);
}
else
{
holder = (CategoryHolder)row.getTag();
}
try {
Picasso.with(mContext)
.load(images.get(position))
.placeholder(R.drawable.loading)
.error(R.drawable.nopicture)
.into(holder.image);
} catch (Exception e) {
Log.d("MYDEBUG3", e.getMessage());
}
holder.name.setText(cat_name.get(position));
holder.layout.setBackgroundColor(Color.parseColor(gridcolor[position]));
return row;
}