我可以让我的回收站视图响应屏幕尺寸吗?

can I make my recycler view responsive with respect to screen sizes?

这是我的代码,我每行显示 2 个项目。

extends Fragment {

private RecyclerView recyclerView;
private AlbumsAdapter adapter;
private List<Album> albumList;
protected View view;
private FloatingActionButton fab;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    view = inflater.inflate(R.layout.fragment_ebooks, container, false);
    fab = (FloatingActionButton) view.findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(getActivity(), ProductsActivity.class);
            startActivity(intent);
        }
    });
    initCollapsingToolbar();

    recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);

    albumList = new ArrayList<>();
    adapter = new AlbumsAdapter(getActivity(), albumList);

    RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getActivity(), 2);
    recyclerView.setLayoutManager(mLayoutManager);
    recyclerView.addItemDecoration(new GridSpacingItemDecoration(2, dpToPx(10), true));
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setAdapter(adapter);

    prepareAlbums();

    try {
        Glide.with(this).load(R.drawable.cover).into((ImageView) view.findViewById(R.id.backdrop));
    } catch (Exception e) {
        e.printStackTrace();
    }
return view;
}


/**
 * Initializing collapsing toolbar
 * Will show and hide the toolbar title on scroll
 */
private void initCollapsingToolbar() {
    final CollapsingToolbarLayout collapsingToolbar =
            (CollapsingToolbarLayout) view.findViewById(R.id.collapsing_toolbar);
    collapsingToolbar.setTitle(" ");
    AppBarLayout appBarLayout = (AppBarLayout) view.findViewById(R.id.appbar);
    appBarLayout.setExpanded(true);

    // hiding & showing the title when toolbar expanded & collapsed
    appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
        boolean isShow = false;
        int scrollRange = -1;

        @Override
        public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
            if (scrollRange == -1) {
                scrollRange = appBarLayout.getTotalScrollRange();
            }
            if (scrollRange + verticalOffset == 0) {
                collapsingToolbar.setTitle(getString(R.string.app_name));
                isShow = true;
            } else if (isShow) {
                collapsingToolbar.setTitle(" ");
                isShow = false;
            }
        }
    });
}

/**
 * Adding few albums for testing
 */
private void prepareAlbums() {
    int[] covers = new int[]{
            R.drawable.album1,
            R.drawable.album2,
            R.drawable.album3,
            R.drawable.album4,
            R.drawable.album5,
            R.drawable.album6,
            R.drawable.album7,
            R.drawable.album1,
            R.drawable.album2,
            R.drawable.album3,
            R.drawable.album4};

    Album a = new Album("Management..", "Donald", covers[0]);
    albumList.add(a);

    a = new Album("Officers search", "John.A", covers[1]);
    albumList.add(a);

    a = new Album("Court Security", "Carter", covers[2]);
    albumList.add(a);

    a = new Album("Officer DUI", "John", covers[3]);
    albumList.add(a);

    a = new Album("Criminal Evedence", "Larry", covers[4]);
    albumList.add(a);

    a = new Album("Criminal Procedure", "Larry", covers[5]);
    albumList.add(a);

    a = new Album("Officers DUI", "John", covers[6]);
    albumList.add(a);

    a = new Album("K9 Officers..", "Ken", covers[7]);
    albumList.add(a);

    a = new Album("Officers search", "John", covers[8]);
    albumList.add(a);

    a = new Album("Tactical Spanish", "Larry", covers[9]);
    albumList.add(a);

    adapter.notifyDataSetChanged();
}

/**
 * RecyclerView item decoration - give equal margin around grid item
 */
public class GridSpacingItemDecoration extends RecyclerView.ItemDecoration {

    private int spanCount;
    private int spacing;
    private boolean includeEdge;

    public GridSpacingItemDecoration(int spanCount, int spacing, boolean includeEdge) {
        this.spanCount = spanCount;
        this.spacing = spacing;
        this.includeEdge = includeEdge;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        int position = parent.getChildAdapterPosition(view); // item position
        int column = position % spanCount; // item column

        if (includeEdge) {
            outRect.left = spacing - column * spacing / spanCount; // spacing - column * ((1f / spanCount) * spacing)
            outRect.right = (column + 1) * spacing / spanCount; // (column + 1) * ((1f / spanCount) * spacing)

            if (position < spanCount) { // top edge
                outRect.top = spacing;
            }
            outRect.bottom = spacing; // item bottom
        } else {
            outRect.left = column * spacing / spanCount; // column * ((1f / spanCount) * spacing)
            outRect.right = spacing - (column + 1) * spacing / spanCount; // spacing - (column + 1) * ((1f /    spanCount) * spacing)
            if (position >= spanCount) {
                outRect.top = spacing; // item top
            }
        }
    }
}

/**
 * Converting dp to pixel
 */
private int dpToPx(int dp) {
    Resources r = getResources();
    return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics()));
}

使用这行代码,我连续显示了 2 个项目。

RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getActivity(), 2);

我想让我的回收站视图响应迅速,这样我就可以在 android 的不同屏幕尺寸下使用我的应用程序。

我使用卡片视图完成了此操作,其中我将卡片视图放置在线性布局中,其中卡片的数量是固定的我通过创建不同的布局来完成此操作。现在我想在回收站视图中更改它,我的项目可以动态增加。

请帮助我使回收站视图动态响应。

解决问题的最简单方法是定义一个您可以从代码访问的资源整数。 因此,例如,如果您希望在普通屏幕上有 2 行,请将以下行添加到 res\values[=29= 中的 integers.xml ] 文件夹在 res:

<integer name="number_of_grid_items">2</integer>

如果您想在 xxxhdpi 屏幕上显示 3 个项目,请将以下行添加到 integers.xml 文件中的 res\values-xxxhdpi文件夹:

<integer name="number_of_grid_items">3</integer>

如果这些文件中的任何一个不存在,只需创建它们,使其看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer name="number_of_grid_items">2</integer>
</resources>

要在代码中访问这些资源,只需将创建 GridLayoutManager 的行更改为:

RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getActivity(), getActivity().getResources().getInteger(R.integer.number_of_grid_items));

希望对您有所帮助,如果您还有其他问题,请告诉我..

虽然回答晚了,但你可以关注一下看看是否有帮助:

How can I make the Android RecyclerView responsive to fit the screen initially?