android:staggergridlayout同时向上移动shuffle

时间:2016-09-15 05:40:11

标签: android android-recyclerview staggeredgridlayout

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

GAP_HANDLING_NONE

我正在使用 ^[a-zA-Z](([\._\-][a-zA-Z0-9])|[a-zA-Z0-9])*[a-z0-9]$ 来避免图片从一列交换到另一列。

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

enter image description here

向下滚动到底部并返回顶部。随机地,以下三个图像显示布局(它保持变化)

enter image description here

enter image description here

enter image description here

4 个答案:

答案 0 :(得分:4)

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

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

我希望这对朋友有帮助。

答案 1 :(得分:0)

尝试下面的代码。

在您的适配器中添加此替代方法

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

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

如果此解决方案不适合您!

尝试以下解决方案

我正在使用 StaggeredGridLayout 来获取所有画廊图像,并且对我来说很好。

-主要活动

/*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*/ 

-项目布局

ConstraintLayout

中添加ImageView
<?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>

-适配器

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;
        }
    }
}

希望这对您有帮助!

谢谢。

答案 2 :(得分: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());

为自定义 RecyclerView滚动侦听器创建类。

    private class ScrollListener extends RecyclerView.OnScrollListener {
       @Override
       public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            recyclerView.invalidateSpanAssignments();
       }
    }

答案 3 :(得分: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);

        }
    }
}

我希望这会对您有所帮助;)