在我的Android应用中,我有一个像这样的ViewPager:
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="300dp"
android:visibility="visible"
android:id="@+id/single_product_details_view_pager"/>
我正在设置像这样的FragmentStatePagerAdapter:
productHeroPagerAdapter = new ProductHeroPagerAdapter(
fragmentManager, imagesArray, videosArray,
singleProductDetailsSlidingLayer.getId(),
productImagesViewPager.getId());
我的ProductHeroPagerAdapter是这样的:
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/**
* Pager adapter for hero images and videos of the product
* This pager adapter is responsible to do necessary calculations to handle separate images and videos array at one place
* Created by ishan on 22/1/16.
*/
public class ProductHeroPagerAdapter extends FragmentStatePagerAdapter {
private static final String TAG = ProductHeroPagerAdapter.class.getSimpleName();
private JSONArray videosArray, imagesArray;
private final int slidingLayerId;
private final int viewPagerId;
public ProductHeroPagerAdapter(FragmentManager fragmentManager, JSONArray imagesArray, JSONArray videosArray, int slidingLayerID, int viewPagerId) {
super(fragmentManager);
this.imagesArray = imagesArray;
this.videosArray = videosArray;
this.slidingLayerId = slidingLayerID;
this.viewPagerId = viewPagerId;
}
@Override
public int getCount() {
return imagesArray.length() + videosArray.length();
}
@Override
public Fragment getItem(int position) {
// First Fragments will be images
if (position < imagesArray.length()) {
// Sending Fragment containing image
String imageUrl;
try {
JSONObject imageObject = (JSONObject) imagesArray.get(position);
imageUrl = imageObject.getString("img_600");
} catch (JSONException e) {
e.printStackTrace();
// imageUrl is blank making the error cases to have default error case images
imageUrl = "";
}
return ProductHeroImageFragment.newInstance(imageUrl, position, imagesArray, videosArray);
} else {
// Sending Fragment containing video
String videoUrl;
try {
JSONObject videoObject = (JSONObject) videosArray.get(position - imagesArray.length());
videoUrl = videoObject.getString("link");
} catch (JSONException e) {
e.printStackTrace();
// videoUrl is blank making the error cases to have default error case screen
videoUrl = "";
}
return ProductHeroVideoFragment.newInstance(videoUrl, position, imagesArray, videosArray, slidingLayerId, viewPagerId);
}
}
}
我遇到的问题是,当我在ViewPager上来回扫描片段时,应用程序的内存使用量不断增加,大约128 MB(在我测试的手机上)它完全停止工作
除了快速扫描碎片之外,我没有做任何其他事情。 片段并不复杂,要么只包含图像,要么包含视频。目前,我正在测试空白的videoJsonArray,以便只显示图像。我似乎无法弄清楚为什么RAM用得那么多,为什么它们会不断增加?
更新:
我似乎无法弄清楚是什么让我的记忆力不足。这是我的ProductHeroImageFragment:
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.ishan1608.utility.ColoredProgressBar;
import com.weddingonclick.customer.R;
import org.json.JSONArray;
/**
* Product hero image display fragment
* Created by ishan on 22/1/16.
*/
public class ProductHeroImageFragment extends Fragment {
private static final String IMAGE_URL = "IMAGE_URL";
public static final String POSITION = "POSITION";
public static final String IMAGES_ARRAY_STRING = "IMAGES_ARRAY_STRING";
public static final String VIDEOS_ARRAY_STRING = "VIDEOS_ARRAY_STRING";
private String imageUrl;
private int position;
private String imagesArrayString, videosArrayString;
public ProductHeroImageFragment() {
// Required Empty Constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param imageUrl Url of the image to be displayed
* @param position position in the image and video array
* @param imagesArray JSONArray for the images
* @param videosArray JSONArray for the videos
* @return A new instance of fragment ProductHeroImageFragment.
*/
public static ProductHeroImageFragment newInstance(String imageUrl, int position, JSONArray imagesArray, JSONArray videosArray) {
ProductHeroImageFragment fragment = new ProductHeroImageFragment();
Bundle args = new Bundle();
args.putString(IMAGE_URL, imageUrl);
args.putInt(POSITION, position);
args.putString(IMAGES_ARRAY_STRING, imagesArray.toString());
args.putString(VIDEOS_ARRAY_STRING, videosArray.toString());
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
this.imageUrl = getArguments().getString(IMAGE_URL);
this.position = getArguments().getInt(POSITION);
this.imagesArrayString = getArguments().getString(IMAGES_ARRAY_STRING);
this.videosArrayString = getArguments().getString(VIDEOS_ARRAY_STRING);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_product_hero_image, container, false);
final ColoredProgressBar loadingProgressBar = (ColoredProgressBar) rootView.findViewById(R.id.loading_progress_bar);
loadingProgressBar.setVisibility(View.VISIBLE);
final ImageView productHeroImageView = (ImageView) rootView.findViewById(R.id.product_hero_image_view);
Glide.with(getContext())
.load(this.imageUrl)
.fitCenter()
.into(new SimpleTarget<GlideDrawable>() {
@Override
public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
loadingProgressBar.setVisibility(View.GONE);
productHeroImageView.setImageDrawable(resource);
}
});
rootView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!getActivity().getClass().equals(ProductHeroImageVideoActivity.class)) {
final Intent productHeroImageVideoIntent = new Intent(getActivity(), ProductHeroImageVideoActivity.class);
productHeroImageVideoIntent.putExtra(POSITION, position);
productHeroImageVideoIntent.putExtra(IMAGES_ARRAY_STRING, imagesArrayString);
productHeroImageVideoIntent.putExtra(VIDEOS_ARRAY_STRING, videosArrayString);
startActivity(productHeroImageVideoIntent);
}
}
});
return rootView;
}
@Override
public void onDestroy() {
super.onDestroy();
Glide.get(getContext()).clearMemory();
}
}
这里是fragment_product_hero_image.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/pure_black">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/product_hero_image_view"/>
<com.ishan1608.utility.ColoredProgressBar
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_centerInParent="true"
android:id="@+id/loading_progress_bar"/>
</RelativeLayout>