【问题标题】:Get to know device screen size programmatically in Android?在 Android 中以编程方式了解设备屏幕尺寸?
【发布时间】:2015-10-27 00:07:15
【问题描述】:

请查看相关Question。我从 cmets 那里得到了一些提示,现在用一种新的方式来表达它。

我正在使用带有StaggeredGridView 适配器的RecyclerView 来显示数据。我如何知道当前设备需要加载多少项目才能适应整个屏幕?

进程

  • 确定屏幕尺寸
  • 确定需要加载多少项目以适应全屏
  • 从服务器获取包含项目数的数据
  • 显示它们
  • 当用户向下滚动设备时,获取相同数量的项目等等。

问题

我无法理解如何完成前两点。

适配器代码

public class StaggeredGridAdapter extends RecyclerView.Adapter<StaggeredGridAdapter.StaggeredGridView> {

private Context context;
private String INTENT_VALUE = "warehouse";
private List<Warehouse> warehouses = new ArrayList<Warehouse>();
private AppDelegate app;
int size;

public StaggeredGridAdapter(Context context) {
    this.context = context;
    app = (AppDelegate) context;
}

public void addItems(List<Warehouse> response) {
    size = response.size();
    warehouses = response;
}

@Override
public StaggeredGridView onCreateViewHolder(ViewGroup parent, int viewType) {
    View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.grid_item, parent, false);
    StaggeredGridView staggeredGridView = new StaggeredGridView(layoutView);
    return staggeredGridView;
}

@Override
public void onBindViewHolder(StaggeredGridView holder, int position) {
    holder.textView.setText(warehouses.get(position).getFace());
}

@Override
public int getItemCount() {
    return size;
}

class StaggeredGridView extends RecyclerView.ViewHolder implements View.OnClickListener {
    TextView textView;

    public StaggeredGridView(View itemView) {
        super(itemView);
        textView = (TextView) itemView.findViewById(R.id.img_name);
        itemView.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(context, WareHouseDetailActivity.class);
        intent.putExtra(INTENT_VALUE, warehouses.get(getAdapterPosition()));
        v.getContext().startActivity(intent);
    }
}
}

向适配器填充数据的代码

mRecyclerView = (RecyclerView) mActivity.findViewById(R.id.staggering_grid);
mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
mAdapter = new StaggeredGridAdapter(mContext);
mAdapter.addItems(response);
mRecyclerView.setAdapter(mAdapter);

【问题讨论】:

  • 您是否主要关心知道何时从服务器加载更多项目?
  • 看看WindowManager,这是您确定屏幕大小的第一个停靠点。
  • @ElliotM 实际上不是,因为它是第一个活动。我只想知道我需要从服务器获取多少项目,因为每个请求只返回 10 个项目
  • @t0mm13b 知道了,但是有什么方法可以确定项目的数量吗?
  • @Def 你知道数据是如何在适配器中呈现的,即。一个项目的高度/宽度,然后将视图的客户端高度除以呈现项目的高度?

标签: java android android-layout android-recyclerview


【解决方案1】:

要查找高度和宽度,可以使用DisplayMetrics

final DisplayMetrics displayMetrics = this.getApplicationContext().getResources().getDisplayMetrics();

这个displayMetrics 将给出高度、宽度和密度。使用单个View(或行)的高度和总高度来计算项数。

为什么要Exact 的项目数,如果是服务器调用,您可以根据设备大小粗略估计项目数。另外,我认为最好将the number of items sent by server 保持不变,而不依赖于设备高度。如果您收到的商品数量超过了屏幕可以容纳的数量,那么用户无论如何都可以滚动查看它们。

【讨论】:

  • 太棒了!但是我如何确定需要从displaymetrics获取的项目数
  • 使用你的方式,宽度是准确的,但高度不完全是设备屏幕高度。因为它没有考虑状态栏的高度。
  • 是的,我更喜欢将DisplayMetrics 用于density。使用它和根布局的height,我们可以获得以像素为单位的高度。
【解决方案2】:

与其尝试计算需要加载多少,不如将其设置为在接近列表末尾时自动加载更多。

考虑这种方法:

@Override
public void onBindViewHolder(StaggeredGridView holder, int position) {
    holder.textView.setText(warehouses.get(position).getFace());
}

当项目准备好在屏幕上显示时调用。通过执行if(position &gt; getItemCount() - 5{ // load more } 之类的操作,您可以在需要时自动加载更多内容。

【讨论】:

  • 问题是对服务器的一个请求只返回 10 个项目。
猜你喜欢
  • 1970-01-01
  • 2015-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多