2017-08-17 43 views
-3

我有6个图像下载as shown here,但我的画廊中的GridView只显示那些images中的5个。Android:GridView显示几乎所有的图像

我试图复制Instagram如何显示其画廊,选定的图像占用了屏幕的60%,而图库图像则占据了其余部分。

fragment_gallery.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/relLayoutl"> 
     <!--toolbar--> 
     <include layout="@layout/snippet_top_gallerybar"/> 

    </RelativeLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     android:weightSum="100" 
     android:layout_below="@+id/relLayoutl"> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="60"> 

      <ImageView 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:id="@+id/galleryImageView" 
       android:scaleType="centerCrop"/> 

      <ProgressBar 
       android:layout_width="100dp" 
       android:layout_height="100dp" 
       android:id="@+id/progressBar" 
       android:layout_centerInParent="true"/> 
     </RelativeLayout> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="40"> 
     <GridView 
      android:layout_width="match_parent" 
      android:layout_height="fill_parent" 
      android:numColumns="5" 
      android:verticalSpacing="1.5dp" 
      android:horizontalSpacing="1.5dp" 
      android:gravity="center" 
      android:layout_marginTop="1dp" 
      android:stretchMode="none" 
      android:id="@+id/gridView"> 

     </GridView> 
     </RelativeLayout> 
    </LinearLayout> 
</RelativeLayout> 

我创建了一个正方形视图,以产生平方细胞

layout_grid_imageview.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 

    <com.example.sheldon.instagramclone.Util.SquareImageView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/gridViewImage" 
     android:adjustViewBounds="true" 
     android:scaleType="centerCrop"/> 

    <ProgressBar 
     android:layout_width="50dp" 
     android:layout_height="50dp" 
     android:layout_centerInParent="true" 
     android:id="@+id/gridProgressBar"/> 
</RelativeLayout> 

GalleryFragment.java

public class GalleryFragment extends Fragment { 

private static final int NUM_COLUMNS = 4; 
private ImageView mExit; 
private Spinner mSpinner; 
private TextView mNext; 
private ProgressBar mProgressBar; 
private List<String> directories; 
private GridView mGridView; 
private ImageView mGalleryImage; 
private HashMap<String, ArrayList<String>> directoryToImage; 
private String append = "file:/"; 
private String mSelectedImage; 

@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.fragment_gallery, container, false); 
    mExit = (ImageView) view.findViewById(R.id.exitShare); 
    mSpinner = (Spinner) view.findViewById(R.id.shareSpinner); 
    mNext = (TextView) view.findViewById(R.id.shareNext); 
    mProgressBar = (ProgressBar) view.findViewById(R.id.progressBar); 
    mGridView = (GridView) view.findViewById(R.id.gridView); 
    mGalleryImage = (ImageView) view.findViewById(R.id.galleryImageView); 
    mExit.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      getActivity().finish(); 
     } 
    }); 

    mNext.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Log.d(TAG, "onClick: Navigating to next step in sharing photo"); 
      Intent intent = new Intent(getActivity(), NextActivity.class); 
      intent.putExtra("selected_image", mSelectedImage); 
      startActivity(intent); 
     } 
    }); 
    init(); 

    return view; 
} 

private void init() { 
    ImageFinder imageFinder = new ImageFinder(); 
    imageFinder.getImages(getActivity()); 
    directoryToImage = imageFinder.getImageMapping(); 
    directories = new ArrayList<>(directoryToImage.keySet()); 
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.spinner_item, directories); 
     adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     mSpinner.setAdapter(adapter); 
     mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
     @Override 
     public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
      Log.d(TAG, "onItemSelected: " + directories.get(position)); 
      setUpGridView(directories.get(position)); 
     } 

     @Override 
     public void onNothingSelected(AdapterView<?> parent) { 

     } 
    }); 
} 

private void setUpGridView(String directory) { 
    final ArrayList<String> imgURLS = directoryToImage.get(directory); 
    Log.d(TAG, "setUpGridView: Displaying " + directory + " with " + imgURLS.size() + " images"); 
    int gridWidth = getResources().getDisplayMetrics().widthPixels; 
    int imageWidth = gridWidth/NUM_COLUMNS; 
    Log.d(TAG, "setUpGridView: Image Width is " + imageWidth); 
    mGridView.setColumnWidth(imageWidth); 
    GridImageAdapter adapter = new GridImageAdapter(getActivity(), R.layout.layout_grid_imageview, append, imgURLS); 
    mGridView.setAdapter(adapter); 
    UniversalImageLoader.setImage(imgURLS.get(0),mGalleryImage, mProgressBar, append); 
    mSelectedImage = imgURLS.get(0); 
    mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
     UniversalImageLoader.setImage(imgURLS.get(position), mGalleryImage, mProgressBar, append); 
     mSelectedImage = imgURLS.get(0); 
    } 
});} 

我显示使用一种称为通用图像装载机

GridImageAdapter.java

public class GridImageAdapter extends ArrayAdapter<String>{ 

private Context mContext; 
private LayoutInflater mInflater; 
private int layoutResource; 
private String mAppend; 
private ArrayList<String> imgURLs; 

public GridImageAdapter(Context context, int layoutResource, String append, ArrayList<String> imgURLs) { 
    super(context, layoutResource, imgURLs); 
    mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    mContext = context; 
    this.layoutResource = layoutResource; 
    mAppend = append; 
    this.imgURLs = imgURLs; 
} 

private static class ViewHolder{ 
    SquareImageView image; 
    ProgressBar mProgressBar; 
} 

@NonNull 
@Override 
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { 

    final ViewHolder holder; 
    if(convertView == null){ 
     convertView = mInflater.inflate(layoutResource, parent, false); 
     holder = new ViewHolder(); 
     holder.mProgressBar = (ProgressBar) convertView.findViewById(R.id.gridProgressBar); 
     holder.image = (SquareImageView) convertView.findViewById(R.id.gridViewImage); 

     convertView.setTag(holder); 
    } 
    else{ 
     holder = (ViewHolder) convertView.getTag(); 
    } 

    String imgURL = getItem(position); 
    Log.d(TAG, "getView: Loading position " + position + ", displaying " + imgURL + ", with image " + holder.image); 

    ImageLoader imageLoader = ImageLoader.getInstance(); 

    imageLoader.displayImage(mAppend + imgURL, holder.image, new ImageLoadingListener() { 
     @Override 
     public void onLoadingStarted(String imageUri, View view) { 
      if(holder.mProgressBar != null){ 
       holder.mProgressBar.setVisibility(View.VISIBLE); 
      } 
     } 

     @Override 
     public void onLoadingFailed(String imageUri, View view, FailReason failReason) { 
      if(holder.mProgressBar != null){ 
       holder.mProgressBar.setVisibility(View.GONE); 
      } 
     } 

     @Override 
     public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { 
      if(holder.mProgressBar != null){ 
       holder.mProgressBar.setVisibility(View.GONE); 
      } 
     } 

     @Override 
     public void onLoadingCancelled(String imageUri, View view) { 
      if(holder.mProgressBar != null){ 
       holder.mProgressBar.setVisibility(View.GONE); 
      } 
     } 
    }); 

    return convertView; 
} 

第一次发帖库的图片,所以我道歉,如果有什么不对这个职位。

+0

欢迎使用Stack Overflow,当您提出问题时,我们希望您找到打破该功能的通用代码,我们可以从那里帮助您。因此请检查您的项目并查看哪些部分无法正常工作。在屏幕上显示之前是否收到所有图像?网格中是否存在第六个图像但不显示?等等 – MinistryofChaps

回答

0

对不起,在我的文章中如此普遍和不清楚,我不太确定哪部分代码是问题区域。再次查看代码后,我注意到一个愚蠢的错误。我在fragment_gallery.xml中将GridView的numColumns属性设置为5,但使用private static final int NUM_COLUMNS = 4计算了GalleryFragment.java中的列宽。我假设这导致图像显示在不存在的第5列中。