2017-08-17 77 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列中。