imagesview(RecyclerView)之间的大空间

时间:2017-05-02 16:09:03

标签: android android-recyclerview imageview

我试图创建一个壁纸应用程序,但我遇到了recyclerView这个问题。照片之间有一个很大的空间。 照片

enter image description here

并且我不知道为什么以这种格式显示宽度大于高度

enter image description here

java:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    List<String> movieList =  new ArrayList<>();
    movieList.add("https://lh3.ggpht.com/-yKfQjOKJq9HcsOqzdCfpP-POQgEtJb43RZAQjEn10XiOEgM_CBgndV7stWe67wXiCq5=h900");
    movieList.add("http://www.botherland.com/wp-content/uploads/2017/04/Samsung-Galaxy-S4-Black-Edition-Wallpaper-16.jpg");
    movieList.add("http://www.botherland.com/wp-content/uploads/2017/04/Samsung-Galaxy-S4-Black-Edition-Wallpaper-5.jpg");
    movieList.add("http://www.androidhive.info/wp-content/uploads/2016/01/android-recycler-view-example.png");
    movieList.add("https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcTygBxBtnlvqcQR5GmFCvD1rvPUJJeHLm6lH2zckeYQ6XScoutwN0NgaG0");
    movieList.add("http://www.botherland.com/wp-content/uploads/2017/04/Samsung-Galaxy-S4-Black-Edition-Wallpaper-14.jpg");
    View v =  inflater.inflate(R.layout.fragment_art, container, false);
    recyclerView = (RecyclerView) v.findViewById(R.id.recycler_view);
    mAdapter = new MoviesAdapter(movieList,getContext());
    final StaggeredGridLayoutManager stg= new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
    recyclerView.setLayoutManager(stg);
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setAdapter(mAdapter);
    return v;
}

public class MoviesAdapter extends RecyclerView.Adapter<MoviesAdapter.MyViewHolder> {
    private List<String> moviesList;
    class MyViewHolder extends RecyclerView.ViewHolder {
        public ImageView pic ;

        public MyViewHolder(View view) {
            super(view);
           pic = (ImageView)view.findViewById(R.id.imageView);
        }
    }
    public Context context;
    int screenWidth,screenHeight;
    MoviesAdapter(List<String> moviesList, Context context) {
        this.moviesList = moviesList;
        this.context=context;
        WindowManager wm = (WindowManager) getActivity().getSystemService(getActivity().WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        screenWidth = size.x;
        screenHeight = size.y;
    }
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.picture_item, parent, false);
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
       String pic  = moviesList.get(position);
        Glide.with(context).load(pic).placeholder(R.drawable.j).override(screenWidth/2,screenHeight/4).centerCrop().into(holder.pic);
}
    @Override
    public int getItemCount() {
        return moviesList.size();
    }
}

xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#fff"
tools:context="maa.a4kandhdwallpapers.Art">
<!-- TODO: Update blank fragment layout -->
   <android.support.v7.widget.RecyclerView
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:id="@+id/recycler_view"/>
 </LinearLayout>

picture_item.xml:

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

   <ImageView
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:id="@+id/imageView" />
</LinearLayout>

1 个答案:

答案 0 :(得分:3)

picture_item.xml 更改为:

<LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     android:orientation="vertical" 
     android:layout_width="match_parent"
     android:layout_height="wrap_content">

   <ImageView
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:id="@+id/imageView" />
</LinearLayout>

但是如果您的图像高度很大,您将遇到同样的问题。如果这不会造成问题,我建议你修正高度。