這是我的網格視圖片段,我打算在網格中添加50個可繪製項目,我的問題是onclick方法。如何能夠爲這50個項目創建新的Activity類。我確實有一個工作正常的onClick方法,但添加50個case語句是痛苦的,有沒有什麼辦法根據他們的類名動態地使用50個activity類。動態使用Android意圖
公共類片段1擴展片段{
@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.gridview,container,false);
GridView gridView = (GridView) view.findViewById(R.id.gridview);
gridView.setAdapter(new ImageAdapter(getActivity())); // uses the view to get the context instead of getActivity().
gridView.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View v, int position,
long arg3) {
// TODO Auto-generated method stub
Intent n = null;
switch(position){
case 0:
n = new Intent(getActivity(), abc.class);
getActivity().startActivity(n);
break;
case 1:
n = new Intent(getActivity(), bcd.class);
getActivity().startActivity(n);
break;
case 2:
// n = new Intent(getActivity(), cbe.class);
getActivity().startActivity(n);
break;
}
}
});
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(500, 500));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
private Integer[] mThumbIds = {
R.drawable.car,
R.drawable.dabangg,
R.drawable.dabangg,
R.drawable.car,
R.drawable.car,
R.drawable.dabangg
};
}
}
如果你有50周以上的活動,你應該可能重新考慮你的設計。 –
每個項目都鏈接到YouTube的片段。如果您在我的YouTube類中看到它在onInitializationsucess方法中有ArrayList。不知道如何傳遞這些數組列表值,因爲每個值都有不同的值。 – Mihir