我用一个imageview做了一个gridview。当我滚动gridview时,最后一行的项目不完全可见。这是截图,其中最后两张照片不是完全可见的。
这是我的片段。
package com.secretphotosandvideoslocker.fragments;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.GridView;
import com.secretphotosandvideoslocker.R;
import com.secretphotosandvideoslocker.adapters.PhotoAlumbAdapter;
import com.secretphotosandvideoslocker.interfaces.AllImagePath;
import com.secretphotosandvideoslocker.interfaces.SupportInterface;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class PhotoFragment extends Fragment implements AllImagePath {
private View myView;
private GridView gridView;
private int size;
private PhotoAlumbAdapter adapter;
private List<String> list = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.fragment_photo, null);
new SupportInterface(this);
gridView = (GridView) myView.findViewById(R.id.photo_grid);
int value = getActivity().getResources().getConfiguration().orientation;
orientationBasedUI(value);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
}
});
return myView;
}
private void orientationBasedUI(int orientation) {
list = new ArrayList<>();
WindowManager windowManager = (WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics metrics = new DisplayMetrics();
windowManager.getDefaultDisplay().getMetrics(metrics);
size = orientation == Configuration.ORIENTATION_PORTRAIT ? metrics.widthPixels / 3 : metrics.widthPixels / 5;
gridView.setNumColumns(orientation == Configuration.ORIENTATION_PORTRAIT ? 3 : 5);
File file = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/Android/data/com.secretphotosandvideoslocker/Images");
File[] files = file.listFiles();
if (files != null) {
for (int i = 0; i < files.length; i++)
list.add(files[i].getAbsolutePath());
adapter = new PhotoAlumbAdapter(list, getActivity(), size);
}
gridView.setAdapter(adapter);
}
@Override
public void setAllImage() {
int value = getActivity().getResources().getConfiguration().orientation;
orientationBasedUI(value);
}
}
这是在这个片段中使用的xml。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<GridView
android:id="@+id/photo_grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="1dp"
android:paddingLeft="1dp"
android:paddingRight="1dp"
android:paddingTop="1dp"
android:stretchMode="columnWidth" />
</RelativeLayout>
这是此gridview的适配器。
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.secretphotosandvideoslocker.R;
import java.util.List;
public class PhotoAlumbAdapter extends BaseAdapter {
private View myView;
private LayoutInflater layoutInflater;
private List<String> list;
private Context context;
private ImageLoader imageLoader;
private int size;
private DisplayImageOptions imageOptions;
public PhotoAlumbAdapter(List<String> list, Context context, int size) {
this.list = list;
this.size = size;
this.context = context;
layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader = ImageLoader.getInstance();
imageLoader.init(ImageLoaderConfiguration.createDefault(context));
imageOptions = new DisplayImageOptions.Builder().showImageOnLoading(R.drawable.image_placeholder)
.cacheOnDisk(true).build();
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return list.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int position, View V, ViewGroup viewGroup) {
ViewHolder viewHolder;
myView = V;
if (V == null) {
viewHolder = new ViewHolder();
myView = layoutInflater.inflate(R.layout.photo_grid_item, null);
viewHolder.imageView = (ImageView) myView.findViewById(R.id.image_view);
myView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) myView.getTag();
}
viewHolder.imageView.getLayoutParams().width = size;
viewHolder.imageView.getLayoutParams().height = size;
imageLoader.displayImage("file://" + list.get(position), viewHolder.imageView, imageOptions);
return myView;
}
private class ViewHolder {
ImageView imageView;
}
}