我有一个有观察者的活动,在这个观察者里面有5个片段。在第二个片段(FRAGMENT_GALLERY)中,我有一个另一个VIEWPAGER。在这个观察者内部,我有一个片段名为FRAGMENT_PHOTOS,在这片中(FRAGMENT_PHOTOS)有一个GRIDVIEW,我只能在第一次使用IMAGEADAPTER打开应用程序(IMAGEADAPTER是扩展BASEADAPTER)。当我导航到非常接近的碎片或非常早期的碎片时,也会留下人群(显示图像)。但是,当我导航到其他一些片段(说#4或#5)并回到这片片段(片段照片)时,GRIDVIEW没有获得人口或没有显示图像(根据我的诊断,IMAGEADAPTER没有得到再次调用)。它只会被打开(当我们打开APP时)。请帮帮我......
但是当我导航到另一个片段(比如#4或#5)时,FragmentPhotos中的gridview不显示图像。 根据我的诊断,imageadapter没有再给予回复
公共类FragmentPhotos扩展片段{
private GridView gridView;
private Bitmap[] thumbnails;
private ImageAdapter imageAdapter;
private DisplayMetrics display;
private ArrayList<String> imagesArrayList = new ArrayList<String>();
private ArrayList<String> foldersArrayList = new ArrayList<String>();
private View rootView;
private final int REQUEST_CODE=1;
private ImageView imgEnlarged;
private GetImagesFromCard imagesFromCardTask;
private Context context;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_photos, container, false);
initUI(rootView);
return rootView;
}
public void initGallery()
{
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
requestPermissions(AppClass.INITIAL_PERMS, REQUEST_CODE);
}
else
{
if (imagesFromCardTask.getStatus().equals(AsyncTask.Status.RUNNING))
{
}
else
{
imageAdapter = new ImageAdapter(context);
gridView.setAdapter(imageAdapter);
new GetImagesFromCard().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
}
}
@Override
public void onStart() {
context = this.getActivity();
imagesFromCardTask = new GetImagesFromCard();
super.onStart();
}
public void disableGalleryTask()
{
if (imagesFromCardTask.getStatus()==GetImagesFromCard.Status.RUNNING)
{
imagesFromCardTask.cancel(true);
}
}
private void initUI(View view) {
display = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(display);
gridView = (GridView) view.findViewById(R.id.gridGallery);
imgEnlarged = (ImageView)view.findViewById(R.id.imgEnlarged);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
showImageEnlarged(position);
}
});
Log.e("","Gallery initialized");
}
private class GetImagesFromCard extends AsyncTask<Void,LoadedImage,Void>
{
Bitmap bitmap;
ArrayList<LoadedImage> bitmapArrays=new ArrayList<LoadedImage>();
@Override
protected Void doInBackground(Void... params) {
Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
String[] projection = {MediaStore.Images.Media.DATE_TAKEN,MediaStore.Images.Media._ID,MediaStore.Images.Thumbnails._ID,MediaStore.Images.Thumbnails.DATA,MediaStore.Images.Media.DATE_TAKEN};
String orderBy = MediaStore.Images.Media.DATE_TAKEN + " DESC";
Cursor cursor = getActivity().getContentResolver().query(uri, projection, null, null, orderBy);
int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);
int size = cursor.getCount();
if (size==0)
{
}
int imageId =0;
int i=0;
while (i<size && !isCancelled())
{
cursor.moveToPosition(i);
imageId = cursor.getInt(columnIndex);
uri = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "" + imageId);
publishProgress(new LoadedImage(uri.toString()));
if (i==0)
{
showImageEnlarged(uri);
}
i++;
}
cursor.close();
return null;
}
@Override
protected void onProgressUpdate(LoadedImage... values) {
super.onProgressUpdate(values);
for (LoadedImage photo: values)
{
imageAdapter.addPhoto(photo);
imageAdapter.notifyDataSetChanged();
bitmapArrays.add(photo);
}
}
@Override
protected void onCancelled() {
Log.e("","Task Cancelled");
super.onCancelled();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode)
{
case REQUEST_CODE:
{
for (int i=0;i<grantResults.length;i++)
{
if (grantResults.length > 0 && grantResults[i] == PackageManager.PERMISSION_GRANTED)
{
Log.e("Permission Granted", permissions[i]);
}
}
new GetImagesFromCard().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
break;
}
}
}
}
以下是我的ImageAdapter:
public class ImageAdapter extends BaseAdapter
{
ImageLoader imageLoader;
Context context;
LayoutInflater inflater;
private DisplayImageOptions options;
ImageLoaderConfiguration config;
ArrayList<LoadedImage> bitmapString;
public ImageAdapter(Context context)
{
Log.e("","ImageAdapter called");
DisplayImageOptions options = new DisplayImageOptions.Builder()
.cacheInMemory(true)
.cacheOnDisc(true)
.considerExifParams(true)
.build();
this.context = context;
inflater = LayoutInflater.from(context);
bitmapString = new ArrayList<LoadedImage>();
config = new ImageLoaderConfiguration.Builder(context)
.threadPriority(Thread.MAX_PRIORITY)
.denyCacheImageMultipleSizesInMemory()
.defaultDisplayImageOptions(options)
.tasksProcessingOrder(QueueProcessingType.FIFO)
.build();
}
public void addPhoto(LoadedImage photo)
{
bitmapString.add(photo);
}
public void clear()
{
bitmapString.clear();
}
public void remove(int index)
{
bitmapString.remove(index);
}
@Override
public int getCount() {
return bitmapString.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View view = convertView;
final ViewHolder holder;
if(convertView==null)
{
holder=new ViewHolder();
view = inflater.inflate(R.layout.fragment_photos_grid_items,parent,false);
holder.imageView = (ImageView)view.findViewById(R.id.imgGallery);
holder.imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
view.setTag(holder);
}
else
{
holder = (ViewHolder)view.getTag();
}
imageLoader = ImageLoader.getInstance();
imageLoader.init(config);
imageLoader.displayImage(bitmapString.get(position).getImgString(),holder.imageView,options);
return view;
}
class ViewHolder
{
ImageView imageView;
}
}
以下是我的ViewPager适配器:
public class GalleryViewPagerAdapter extends FragmentStatePagerAdapter {
private final List<Fragment> mFragments = new ArrayList<>();
private final List<String> mFragmentTitles = new ArrayList<>();
int numOfTabs;
public GalleryViewPagerAdapter(FragmentManager fm) {
super(fm);
}
public void addFragment(Fragment fragment, String title)
{
mFragments.add(fragment);
mFragmentTitles.add(title);
}
@Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
@Override
public int getCount() {
return mFragments.size();
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitles.get(position);
}
}
答案 0 :(得分:0)
请勿在{{1}}内使用getActivity()
,因为可能尚未创建活动。获取onCreateView()
中的上下文。此外,由于您的onActivityCreated()
方法使用了上下文,因此您必须在InitUI(view)
中调用该方法。
编辑1:
致电onActivityCreated()
中的initGallery()
或片段中的onStart()
。
并在onResume()
或disableGalleryTask()
片段中调用onPause()
。
因此,当用户可以看到视图时,只会调用onStop()
。
答案 1 :(得分:0)
在片段中的以下overriden方法中调用initGallery()方法:
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser){
initGallery();
}
}
答案 2 :(得分:0)
问题解决了!!!
实际上,当我添加这个片段(FragmentPhotos - 携带gridview)时,在viewpager中以错误的方式添加了 -
ViewPagerAdapter adapter = new ViewPagerAdapter(getFragmentManager());
//删除此
//添加此
ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());
非常感谢大家的帮助..