我试图使用HorizontalGridView
。但是,让它水平滚动并不容易。我不知道如何准确使用HorizontalGridView
。因此,像RecyclerView
一样使用它如下。
private class GridViewAdaptor extends RecyclerView.Adapter<GridViewAdaptor.ViewHolder> {
private Context mContext;
private ArrayList<String> mPathList;
GridViewAdaptor(Context context, ArrayList<String> pathList) {
mContext = context;
mPathList = pathList;
}
@Override
public int getItemCount() {
return mPathList.size();
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_top_menu_sub, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
GlideApp.with(mContext).load(R.raw.image_1).diskCacheStrategy(DiskCacheStrategy.RESOURCE).apply(new RequestOptions().fitCenter()).into(holder.mImage);
}
class ViewHolder extends RecyclerView.ViewHolder {
ImageView mImage;
ViewHolder(View view) {
super(view);
mImage = (ImageView) view.findViewById(R.id.image_top_menu_sub);
}
}
}
并且......此代码位于onCreate()
。为了简化测试,我将假人添加到pathList
并仅使用一个图像加载。
RelativeLayout.LayoutParams params = new
RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
mTopMenuSubGrid = new HorizontalGridView(this);
mTopMenuSubGrid.setLayoutParams(params);
mTopMenuSubGrid.setNumRows(1);
mTopMenuSubGrid.setHorizontalSpacing(space);
mTopMenuSubGrid.setVerticalSpacing(space);
ArrayList<String> pathList = new ArrayList<>();
pathList.add("0");
pathList.add("1");
pathList.add("2");
pathList.add("3");
pathList.add("4");
pathList.add("5");
pathList.add("6");
pathList.add("7");
GridLayoutManager manager = new GridLayoutManager(this, 4);
mTopMenuSubGrid.setLayoutManager(manager);
mTopMenuSubGrid.setAdapter(new GridViewAdaptor(this, pathList));
mTopMenuSubLayout.addView(mTopMenuSubGrid);
此代码在HorizontalGridView
上显示8个相同的图像。但是,该网格视图垂直滚动。我想水平滚动。请让我知道错误是什么。
答案 0 :(得分:1)
使用此构造函数
new GridLayoutManager(this, 1, GridLayoutManager.HORIZONTAL, false)
您可以根据需要设置spanCount的数量。