我正在尝试使用此实现材料视图寻呼机。寻呼机工作正常。但我需要的是视图寻呼机下的卡片网格布局,例如 .
但是我正在使用GridLayoutManager
并最终得到类似的结果。谁能帮我重新调整一下。
这是我的代码:内部 recyclerview 片段:
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// Calling the RecyclerView
mRecyclerView = (RecyclerView)view.findViewById(R.id.recyclerView);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new GridLayoutManager(context, 2);
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter = new RecyclerViewMaterialAdapter(new GridAdapter(context, mContentItems));
mRecyclerView.setAdapter(mAdapter);
MaterialViewPagerHelper.registerRecyclerView(getActivity(), mRecyclerView, null);
}
在 GridAdapter 内部:
public class GridAdapter extends RecyclerView.Adapter<GridAdapter.ViewHolder> {
List<CommonCategoryItem> mItems;
Context context;
public GridAdapter(Context context,ArrayList<CommonCategoryItem> list) {
super();
mItems = new ArrayList<>();
mItems = list;
this.context = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
final View view= LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.list_item_card_small, viewGroup, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
CommonCategoryItem item = mItems.get(i);
viewHolder.title.setText(item.getTitle());
int resId = ImageHelper.getResourceId(context,mItems.get(i).getImage());
if(resId!=0)
{viewHolder.imageView.setImageResource(resId);}
}
@Override
public int getItemCount() {
return mItems.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public ImageView imageView;
public TextView title;
public CardView cardView;
public ViewHolder(View itemView) {
super(itemView);
imageView = (ImageView)itemView.findViewById(R.id.img_thumbnail);
title = (TextView)itemView.findViewById(R.id.title);
cardView = (CardView)itemView.findViewById(R.id.smallCardView);
}
}
}
还需要一个 onclick 侦听器来移动到每个 gridview 项目单击的不同片段。我无法获得childposition。有人可以帮我吗。