这是我的代码,我每行显示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中使用我的应用程序。
我使用卡片视图完成了这项工作,我将卡片视图放置在线性布局中,其中卡片的数量是固定的,我通过创建不同的布局来完成。现在我想在Recycler视图中更改它,我的项目可以动态增加。
请帮助我让我的回收者视图动态响应。
答案 0 :(得分:3)
解决问题的最简单方法是定义您从代码中访问的资源整数。 因此,例如,如果您希望在普通屏幕上有2行,则将以下行添加到res中的 res \ values 文件夹中的 integers.xml :
<integer name="number_of_grid_items">2</integer>
如果您想在xxxhdpi屏幕上有3个项目,请将以下行添加到 res \ values-xxxhdpi 文件夹中的 integers.xml 文件中:
<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));
希望有帮助,如果您有其他问题,请告诉我。
答案 1 :(得分:0)
尽管回答晚了,但是您可以按照下面的步骤查看是否有帮助:
How can I make the Android RecyclerView responsive to fit the screen initially?