我正在尝试在片段中创建图片库,但它不起作用。当我运行应用程序时,它在我的模拟器中显示白色屏幕,但我的logcat中没有错误。我使用本教程作为参考tutorial。这是我的代码
public View onCreateView(LayoutInflater inflater, ViewGroup container, AttributeSet attrs, Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.imagegallery, container, false);
Gallery gallery = (Gallery) rootView.findViewById(R.id.gallery1);
gallery.setAdapter(new ImageAdapter(getActivity()));
gallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView <? > parent, View v, int position, long id) {
Toast.makeText(rootView.getContext(), "pic" + (position + 1) + " selected",
Toast.LENGTH_SHORT).show();
// display the images selected
ImageView imageView = (ImageView) rootView.findViewById(R.id.image1);
imageView.setImageResource(imageIDs[position]);
}
});
return rootView;
}
public class ImageAdapter extends BaseAdapter {
private Context context;
private int itemBackground;
public ImageAdapter(Activity activity) {
// sets a grey background; wraps around the images
TypedArray a = getActivity().obtainStyledAttributes(R.styleable.MyGallery);
itemBackground = a.getResourceId(R.styleable.MyGallery_android_galleryItemBackground, 0);
a.recycle();
}
// returns the number of images
public int getCount() {
return imageIDs.length;
}
// returns the ID of an item
public Object getItem(int position) {
return position;
}
// returns the ID of an item
public long getItemId(int position) {
return position;
}
// returns an ImageView view
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(context);
imageView.setImageResource(imageIDs[position]);
imageView.setLayoutParams(new Gallery.LayoutParams(100, 100));
imageView.setBackgroundResource(itemBackground);
return imageView;
}
}
}
我做错了什么?
答案 0 :(得分:1)
return rootView;
Fragment
onCreateView(...)
{@ 1}}
return rootView;
并删除
super.onInflate(attrs, savedInstanceState);
并且还要改变
gallery.setAdapter(new ImageAdapter(this));
到
gallery.setAdapter(new ImageAdapter(getActivity());
答案 1 :(得分:0)
试试这个
public class HomeFragment extends Fragment {
public HomeFragment(){}
private Gallery gallery;
private ListView listview;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
gallery = (Gallery) rootView.findViewById(R.id.gallery1);
listview = (ListView) rootView.findViewById(R.id.listView1);
gallery.setAdapter(new GalleryViewAdapter(getActivity()));
String values[] ={"item1","item2","item1","item2"};
ArrayList<String> listValue = new ArrayList<String>();
for(int i=0;i<values.length;i++){
listValue.add(values[i]);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, values);
listview.setAdapter(adapter);
Animation anim = AnimationUtils.loadAnimation(getActivity(), R.anim.fly_in_from_center1);
gallery.setAnimation(anim);
anim.start();
gallery.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//view.setBackgroundResource(R.drawable.list_item_bg_pressed);
Animation vanish =AnimationUtils.loadAnimation(getActivity(),R.anim.vanish);
view.startAnimation(vanish);
Log.i("pos :: ","pos :: "+position);
//Toast.makeText(gridView.getContext(), "pos :: "+position, Toast.LENGTH_SHORT).show();
}
});
return rootView;
}
}
这是适配器
public class GalleryViewAdapter extends BaseAdapter {
private Context context;
public GalleryViewAdapter(Context c) {
context = c;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 15;
}
@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;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v;
if (convertView == null) {
v = LayoutInflater.from(context).inflate(R.layout.row,null);
v.setLayoutParams(new Gallery.LayoutParams(250,250));
}
else {
v = convertView;
}
ImageView imageview = (ImageView)v.findViewById(R.id.row_img);
TextView txtview = (TextView)v.findViewById(R.id.row_title);
txtview.setText("Hello"+position);
imageview.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageview.setPadding(6, 6, 6, 6);
return v;
}
}
答案 2 :(得分:0)
getActivity()在pageadapter内部工作。
GalleryAdapter pageAdapter = new GalleryAdapter(getActivity(), proName, proImage);
ViewPager pager = (ViewPager)getView().findViewById(R.id.galery);
pager.setAdapter(pageAdapter);