[英]I am getting error when trying to create an image gallery in android using ViewPager and GridView
Here I have created an ImageAdapter class by extending PagerAdapter 在这里,我通过扩展PagerAdapter创建了一个ImageAdapter类。
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
public class ImageAdapter extends PagerAdapter {
private Context mContext;
// Keep all Images in array
public Integer[] mThumbIds = new Integer[] {
R.drawable.a, R.drawable.b,
R.drawable.c, R.drawable.d,
R.drawable.e, R.drawable.f,
R.drawable.g, R.drawable.h,
R.drawable.i, R.drawable.j,
R.drawable.k, R.drawable.l,
R.drawable.m, R.drawable.n,
R.drawable.o, R.drawable.p,
R.drawable.q, R.drawable.r,
R.drawable.t, R.drawable.u,
R.drawable.v
};
// Constructor
public ImageAdapter(Context c){
this.mContext = c;
}
@Override
public int getCount() {
return mThumbIds.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(mContext);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setPadding(8, 8, 8, 8);
imageView.setImageResource(mThumbIds[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
This is working fine, with just some redundant warnings, that I don't care about right now. 这工作正常,仅带有一些多余的警告,我现在不在乎。
And here is Fragment class that will display images stored in ImageAdapter class 这是Fragment类,它将显示存储在ImageAdapter类中的图像
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.view.ViewGroup;
import android.widget.GridView;
public class PhotosFragment extends Fragment {
public PhotosFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_photos, container, false);
GridView gridView = (GridView)rootView.findViewById(R.id.grid_view);
gridView.setAdapter(new ImageAdapter(getActivity()));
//GridView gridView = (GridView)rootView.findViewById(R.id.grid_view);
// Instance of ImageAdapter Class
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
// Sending image id to FullScreenActivity
Intent i = new Intent (getActivity().getApplicationContext(), FullImageActivity.class);
// passing array index
i.putExtra("id", position);
startActivity(i);
}
});
return rootView;
}
}
And then here is FullImageActivity class for displaying images in fullscreen 然后是FullImageActivity类,用于全屏显示图像
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
public class FullImageActivity extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.full_image);
// get intent data
Intent i = getIntent();
// Selected image id
int position = i.getExtras().getInt("id");
ImageAdapter imageAdapter = new ImageAdapter(this);
ViewPager imageView = (ViewPager) findViewById(R.id.full_image_view);
imageView.addView(imageAdapter.mThumbIds[position]);
}
}
I am getting error in PhotoFragment class-setAdapter in GridView can not be applied to .ImageAdapter 我在GridView的PhotoFragment class-setAdapter中遇到错误,无法应用于.ImageAdapter
And in FullImageActivity class-addView in ViewGroup can not be applied to .Integer 并且在FullImageActivity中,ViewGroup中的class-addView无法应用于.Integer
And here are the layout xml files 这是布局xml文件
full_image.xml full_image.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager android:id="@+id/full_image_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
fragment_photo fragment_photo
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/grid_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:columnWidth="90dp"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:gravity="center"
android:stretchMode="columnWidth" >
</GridView>
As an explanation for the errors you are receiving: 作为您收到的错误的解释:
GridView's setAdapter method only accepts ListAdapters, not PagerAdapters. GridView的setAdapter方法仅接受ListAdapters,而不接受PagerAdapters。 PagerAdapters are intended to be used with ViewPagers, hence the name "PagerAdapter". PagerAdapters旨在与ViewPagers一起使用,因此名称为“ PagerAdapter”。 You'll probably want your ImageAdapter to extend something like an ArrayAdapter instead. 您可能希望ImageAdapter扩展类似ArrayAdapter之类的东西。
Somewhat related to the above explanation, you also need an adapter to back your ViewPager. 与上述说明有些相关,您还需要一个适配器来支持ViewPager。 I'm honestly not sure what the behavior of a ViewPager is after calling addView, but I doubt it's behavior you're looking for. 老实说,我不确定在调用addView之后ViewPager的行为是什么,但我怀疑这是您要寻找的行为。 You also are giving addView the incorrect parameter regardless. 无论如何,您还给addView提供了不正确的参数。 You are giving it an Integer (as the error states) from mThumbIds, but it expects a view . 您正在使用mThumbIds为它提供一个Integer(作为错误状态),但是它需要一个view 。
You might benefit from reading the following post if you really have your heart set on using addView with your ViewPager: dynamically add and remove view to viewpager 如果您确实有心于将addView与ViewPager一起使用,则可以从阅读以下文章中受益: 动态地向viewpager添加和删除视图
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.