简体   繁体   English

我应该使用哪个android控件来获取android中的2 x 2可滚动网格?

[英]Which android control should i use to get 2 by 2 scrollable grid in android?

How can i have 2 by 2 scrollable Grid? 我怎么能有2乘2可滚动的网格? Which control should i use? 我应该使用哪种控制?

I tried Grid-View and as Grid-View can not have fixed rows so should i use Table-Layout or Grid-Layout ? 我尝试过网格视图,因为网格视图不能有固定的行,所以我应该使用表格布局还是网格布局? or use linear-Layout in Grid-View ? 或在网格视图中使用线性布局?

I want image to be displayed without being scaled down or scaled up so i am using AUTO_FIT and i get three rows with the below code but i want only two rows and two columns 我希望图像显示而不缩小或放大,所以我使用AUTO_FIT,我得到三行与下面的代码,但我只想要两行和两列

@Override public View getView(int position, View convertView, ViewGroup parent) 
{
    ImageView view;

    if (convertView == null) 
    {

        view = (ImageView) convertView;
        view = new ImageView(context);
        view.setAdjustViewBounds(true);
        view.setLayoutParams(new GridView.LayoutParams(GridView.AUTO_FIT, GridView. AUTO_FIT));

    }
    else
         view = (ImageView) convertView;

    String url = getItem(position);

    Picasso.with(context).load(url).error(R.mipmap.no_photo).fit().into(view);

    return view;
}

Below is the sample attached image 以下是附加图像示例

在此输入图像描述

I would recommend you use a RecyclerView with a GridLayoutManager . 我建议你使用带有GridLayoutManagerRecyclerView This will allow you to specify the number of columns: 这将允许您指定列数:

recyclerView.setLayoutManager(new GridLayoutManager(context, 2));

If you would like to set the row height so that two rows always match the height of your screen, you could set it inside onBindView() of your adapter. 如果要设置行高以使两行始终与屏幕高度匹配,则可以在适配器的onBindView()内设置它。 You can ascertain the relative row height by subtracting the screen height from the height of your Toolbar : 您可以通过从Toolbar的高度减去屏幕高度来确定相对行高:

//Could calculate this in your `Activity` class and pass the value to your Adapter
public int getProprtionalRowHeight() {

    DisplayMetrics displaymetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    int height = displaymetrics.heightPixels;

    return ((height - toolbar.getHeight()) / 2);

}

In your adapter: 在您的适配器中:

@Override
public void onBindViewHolder(ItemViewHolder holder, int position) {

    //...

        ViewGroup.LayoutParams params = holder.itemView.getLayoutParams();
        params.height = valueReturnedFromGetProprtionalRowHeight;
        holder.itemView.setLayoutParams(params);

    }

you can use StaggeredGrid for this purpose its stable and easy to use. 你可以使用StaggeredGrid来稳定和易于使用。 you can also use StaggeredGridLayoutManager with recycler view. 您还可以使用带有回收站视图的StaggeredGridLayoutManager

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM