9

我正在使用交错网格布局。以下是代码:

StaggeredGridLayoutManager glm= new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
glm.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);

GAP_HANDLING_NONE用来避免图像从一列交换到另一列。

当我启动应用程序时,屏幕的开头是:

在此处输入图像描述

向下滚动到底部后,当我返回顶部时。以下三张图片随机显示布局(不断变化)

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

4

4 回答 4

5

交错的网格很可能在布局的边缘有间隙。为了避免这些间隙,StaggeredGridLayoutManager可以独立地偏移 span 或在 span 之间移动项目。您可以通过以下方式控制此行为setGapStrategy(int)

StaggeredGridLayoutManager sGrid = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
sGrid.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS);

我希望这对朋友有帮助。

于 2020-01-10T09:23:38.077 回答
0

使用这个简单的解决方案来解决您的问题。

    StaggeredGridLayoutManager straggerGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
    straggerGridLayoutManager .setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS);
    recyclerView.setLayoutManager(straggerGridLayoutManager);

    dataList = YourDataList (Your Code for Arraylist);

    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerAdapter = new DataAdapter(dataList, recyclerView);
    recyclerView.setAdapter(recyclerAdapter);


    recyclerView.addOnScrollListener(new ScrollListener());

为 Custom RecyclerView Scroll Listener创建类。

    private class ScrollListener extends RecyclerView.OnScrollListener {
       @Override
       public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            recyclerView.invalidateSpanAssignments();
       }
    }
于 2020-01-15T12:28:40.270 回答
0

请注意我为您提供的示例代码

图像根据其尺寸以不同的比例显示,甚至一些应用程序显示图像和视频的混合视图,如 Instagram。在接下来的教程中,我们将体验这样一种视图,当我们漫步到视频视图时,视频会被加载并使用自动播放功能播放。

从技术上讲,在这种惊人的视图中显示图像的常规风格没有太大区别。

Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="staggered.android.com.staggeredview.MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/stagRecycleView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</android.support.constraint.ConstraintLayout>

Recyclerview_row.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"
    android:orientation="horizontal"
    android:padding="5dp">

    <ImageView
        android:id="@+id/imgView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitXY"/>

</RelativeLayout>

MainActivity.Java

public class MainActivity extends AppCompatActivity {

    RecyclerViewAdapter adapter;
    ArrayList<Integer> Image;
    RecyclerView recyclerView;
    private RecyclerView.LayoutManager layoutManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Image = new ArrayList<>(Arrays.asList(
                R.drawable.img1,R.drawable.img2,
                R.drawable.img3,R.drawable.img4,
                R.drawable.img5,R.drawable.img6,
                R.drawable.img7,R.drawable.img8,
                R.drawable.img9,R.drawable.img10,
                R.drawable.img1,R.drawable.img2,
                R.drawable.img3,R.drawable.img4,
                R.drawable.img5,R.drawable.img6,
                R.drawable.img7,R.drawable.img8,
                R.drawable.img9)
        );

        recyclerView = findViewById(R.id.stagRecycleView);

        layoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);

        adapter = new RecyclerViewAdapter(this, Image);
        recyclerView.setAdapter(adapter);
    }


}

最后

RecyclerViewAdapter.Java

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {

    ArrayList<Integer> Image;
    Context context;

    public RecyclerViewAdapter(Context context, ArrayList<Integer> Image) {
        super();
        this.context = context;
        this.Image = Image;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.recyclerview_row, viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(v);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int i) {
        viewHolder.imgview.setImageResource(Image.get(i));
    }

    @Override
    public int getItemCount() {
        return Image.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        public ImageView imgview;
        public ViewHolder(View itemView) {
            super(itemView);
            imgview = (ImageView) itemView.findViewById(R.id.imgView);

        }
    }
}

我希望这能帮到您 ;)

于 2020-01-16T12:42:31.690 回答
0

Try below code.

Add this Override methode in your Adapter

@Override
public long getItemId(int position) {
    return position;
}

@Override
public int getItemViewType(int position) {
    return position;
}

If this solution is not work for you!

Try below solution

I am using StaggeredGridLayout for getting all gallery images and it's working fine for me.

- Main Activity

/*Init Gallery Photos RecyclerView*/
gallerySglm = new StaggeredGridLayoutManager(2, RecyclerView.VERTICAL);
gallerySglm.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS);
binding.rvGalleryImg.setLayoutManager(gallerySglm);
galleryPhotosAdapter = new GalleryPhotosAdapter(context, galleryPhotoArrayList);
binding.rvGalleryImg.setAdapter(galleryPhotosAdapter);
galleryPhotosAdapter.notifyDataSetChanged();
/*End Init Gallery Photos RecyclerView*/ 

- Item Layout

Add ImageView inside ConstraintLayout

<?xml version="1.0" encoding="utf-8"?>
<layout>

    <androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/mainCard"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardBackgroundColor="@color/main_bg"
        app:cardCornerRadius="@dimen/_5sdp"
        app:cardPreventCornerOverlap="false"
        app:cardUseCompatPadding="false"
        android:layout_margin="@dimen/_2sdp"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/clMain"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <androidx.appcompat.widget.AppCompatImageView
                android:id="@+id/img"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:scaleType="fitXY"
                android:visibility="visible"
                app:layout_constraintDimensionRatio="1:1"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

        </androidx.constraintlayout.widget.ConstraintLayout>

    </androidx.cardview.widget.CardView>

</layout>

- Adapter

public class GalleryPhotosAdapter extends RecyclerView.Adapter<GalleryPhotosAdapter.ViewHolder> {

    private Context context;
    private ArrayList<String> arrayList;
    private ConstraintSet set = new ConstraintSet();

    public GalleryPhotosAdapter(Context context, ArrayList<String> arrayList) {
        this.context = context;
        this.arrayList = arrayList;
    }

    @NonNull
    @Override
    public GalleryPhotosAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemGalleryPhotosBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()),
                R.layout.item_gallery_photos, parent, false);
        return new ViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull final GalleryPhotosAdapter.ViewHolder holder, final int position) {

        //Set size
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(arrayList.get(position), options);

        String ratio = String.format(Locale.getDefault(), "%d:%d", options.outWidth, options.outHeight);
        set.clone(holder.binding.clMain);
        set.setDimensionRatio(holder.binding.img.getId(), ratio);
        set.applyTo(holder.binding.clMain);
        //End set size

        Glide.with(context)
                .load(arrayList.get(position))
                .into(holder.binding.img);

    }

    @Override
    public int getItemCount() {
        return arrayList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        ItemGalleryPhotosBinding binding;

        public ViewHolder(ItemGalleryPhotosBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }
}

I hope this can help You!

Thank You.

于 2020-01-09T14:09:28.650 回答