【发布时间】:2020-09-26 03:23:57
【问题描述】:
我正在尝试使用此代码创建StaggeredGrid。
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.userprofile_photos,container,false);
setRetainInstance(true);
photosRecycler=view.findViewById(R.id.userPhotos_recycler);
layoutManager= new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
layoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS);
photosRecycler.setLayoutManager(layoutManager);
photosRecycler.setHasFixedSize(true);
adapter=new fetchPhoto_Adapter();
photosRecycler.setAdapter(adapter);
return view;
}
片段布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/userPhotos_recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll" />
</RelativeLayout>
适配器
public class fetchPhoto_Adapter extends RecyclerView.Adapter<fetchPhoto_Adapter.ViewHolder>{
@NonNull
@Override
public fetchPhoto_Adapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
LayoutInflater inflater= (LayoutInflater) viewGroup.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.photogallery,viewGroup,false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(fetchPhoto_Adapter.ViewHolder viewHolder, int i) {
Glide.with(getActivity()).load(ImageList.get(i)).apply(new RequestOptions().centerCrop()).into(viewHolder.image);
}
@Override
public int getItemCount() {
return ImageList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView image;
public ViewHolder(View itemView) {
super(itemView);
image = itemView.findViewById(R.id.UserProfile_photoThumb);
}
}
}
适配器项布局 XML
<?xml version="1.0" encoding="utf-8"?>
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/UserProfile_photoThumb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
/>
我尝试将其设为StaggeredGrid,但使用上述代码并进行了大量修改,例如
将图像比例类型设置为 fitxy、centercrop、center 并将高度更改为 wrap_content 并修改 Glide 图像加载代码都显示相同的输出。我想让我的StaggeredGrid 像下面所需的输出一样。请帮帮我。
【问题讨论】:
-
您能告诉我您在哪里创建了适配器实例吗?
-
@NitinTej
adapter=new fetchPhoto_Adapter();onCreateView 内。 -
您不会将任何列表传递给适配器吗?
-
@Nitin Tej 他从适配器内部的 ImageList 中获取了静态数据
-
@HaiHack 没错。我正在为适配器设置静态日期
标签: java android xml android-recyclerview android-gridlayout