如何让 Recycler 在网格中查看前四个项目,然后在线性项目中查看
How to get Recycler View First Four items in Grids and then in Linear Items
您好,我在项目视图对齐方面遇到问题,我想实现网格中的前四个项目和线性视图项目中的其余项目,具有一个图形视图和线性动态视图。
我想达到的目标
|Grid 0 | Grid 1 |
------------------
|Grid 2 | Grid 3 |
------------------
| LinearItem 0 |
------------------
| LinearItem 1 |
我遇到的问题
|Grid 0 | Grid 1 |
------------------
|Grid 2 | Grid 3 |
------------------
| Linear Item 0 |
------------------
|Line1|
在 网格项目视图 中返回的最后项目而不是带有线性项目的全宽度
从 MainActivity 开始,使用带有 spanize 的 GridLayout 管理器
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(false);
cardModelList = new ArrayList<>();
adapter = new CardAdapter(this, cardModelList);
mLayoutManager = new GridLayoutManager(this, 2);
mLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
switch (adapter.getItemViewType(position)) {
case 1:
return 1;
case 2:
return 2;
case 3:
return 3;
default:
return 1;
}
}
});
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.addItemDecoration(new GridSpacingDashboard(2, dpToPx(1), true));
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adapter);
然后在 MainActivity 上准备卡片的方法
private void prepareCards() {
int[] covers = new int[]{
R.drawable.card_users,
R.drawable.card_unit,
R.drawable.card_request,
R.drawable.card_request_pending,
R.drawable.stats_main,
R.mipmap.coinsicon_cards,
};
String[] stats = new String[]{
userStatsFormated,villasStatsFormated,reqStatsFormated,pendingreqStatsFormated,"",pendingCollectionFormated,
};
CardModel c = new CardModel(stats[0], "Number Of Users", "", covers[0],null);
cardModelList.add(c);
c = new CardModel(stats[1], "Number", "", covers[1],null);
cardModelList.add(c);
c = new CardModel(stats[2], "Total", "", covers[2],null);
cardModelList.add(c);
c = new CardModel(stats[3], "Request", "", covers[3],null);
cardModelList.add(c);
c = new CardModel(stats[4], "Graph", "", covers[3],null);
cardModelList.add(c);
c = new CardModel(stats[5], "Pending", "", covers[4],null);
cardModelList.add(c);
从onCreateView Holder开始的Card Adapter
@Override
public CardViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
CardViewHolder rcv = null;
switch (viewType)
{
case 1:
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view_sec, null);
rcv = new TopGridCardView(layoutView);
break;
case 2:
View layoutView3 = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recycler_view, null);
chart = (BarChart) layoutView3.findViewById(R.id.chart1);
BARENTRY = new ArrayList<BarEntry>();
BarEntryLabels = new ArrayList<String>();
AddValuesToBARENTRY();
AddValuesToBarEntryLabels();
Bardataset = new BarDataSet(BARENTRY, "Projects");
BARDATA = new BarData(BarEntryLabels, Bardataset);
Bardataset.setColors(ColorTemplate.COLORFUL_COLORS);
chart.setData(BARDATA);
chart.animateY(3000);
rcv = new GraphCardView(layoutView3);
break;
case 3:
View layoutView2 = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view, null);
rcv = new BottomCardView(layoutView2);
break;
}
return rcv;
@Override
public void onBindViewHolder(CardViewHolder holder, final int position) {
CardModel cardModel = cardList.get(position);
if (holder.getItemViewType() == 1) {
TopGridCardView vholder = (TopGridCardView) holder;
vholder.secTitleStats.setText(cardModel.getCardTitles());
vholder.secNumStats.setText(cardModel.getNumStats());
vholder.secCardStats.setText(cardModel.getSecCartNumStats());
if (position == 0) {
vholder.secthumbnail.setBackgroundColor(Color.parseColor("#137927"));
}
if (position == 1) {
vholder.secthumbnail.setBackgroundColor(Color.parseColor("#E91E63"));
}
if (position == 2) {
vholder.secthumbnail.setBackgroundColor(Color.parseColor("#02BBD2"));
}
if (position == 3) {
vholder.secthumbnail.setBackgroundColor(Color.parseColor("#9900FF"));
}
Glide.with(mContext).load(cardModel.getThumbnail()).into(vholder.secthumbnail);
}
else if (holder.getItemViewType() == 2) {
GraphCardView vholder = (GraphCardView) holder;
vholder.BarChart.setData(BARDATA);
}
else if (holder.getItemViewType() == 3) {
BottomCardView vholder = (BottomCardView) holder;
vholder.numStats.setText(cardModel.getNumStats());
vholder.image_title.setText(cardModel.getCardTitles());
}
}
这里设置获取物品数量和位置returns
@Override
public int getItemViewType(int position) {
if (isPositionHeader(position)) {
return 2;
}
return 1;
}
@Override
public int getItemCount(){
return 6;
}
private boolean isPositionHeader(int position) {
if (position == 4) {
return position == 4;
}
return position == 6;
}
您的问题是,您的尺寸跨度查找需要 3 种不同的项目类型,而实际上您只有 2 个,即值 2 或 1。将尺寸跨度查找中的代码更改为:
switch (adapter.getItemViewType(position)) {
case 1: //this is for view type 1 (grid item)
return 1;
case 2: //this is for view type 2 (full width item)
return 2;
default:
return 1; //will default to grid item
}
private boolean isPositionHeader(int position) {
if (position == 4 || position == 5) {
return true;
}
}
大小跨度查找仅确定项目应跨越多少列,因此在您的示例中,根据视图类型,它将是 2 列或 1 列。
您好,我在项目视图对齐方面遇到问题,我想实现网格中的前四个项目和线性视图项目中的其余项目,具有一个图形视图和线性动态视图。
我想达到的目标
|Grid 0 | Grid 1 |
------------------
|Grid 2 | Grid 3 |
------------------
| LinearItem 0 |
------------------
| LinearItem 1 |
我遇到的问题
|Grid 0 | Grid 1 |
------------------
|Grid 2 | Grid 3 |
------------------
| Linear Item 0 |
------------------
|Line1|
在 网格项目视图 中返回的最后项目而不是带有线性项目的全宽度
从 MainActivity 开始,使用带有 spanize 的 GridLayout 管理器
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(false);
cardModelList = new ArrayList<>();
adapter = new CardAdapter(this, cardModelList);
mLayoutManager = new GridLayoutManager(this, 2);
mLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
switch (adapter.getItemViewType(position)) {
case 1:
return 1;
case 2:
return 2;
case 3:
return 3;
default:
return 1;
}
}
});
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.addItemDecoration(new GridSpacingDashboard(2, dpToPx(1), true));
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adapter);
然后在 MainActivity 上准备卡片的方法
private void prepareCards() {
int[] covers = new int[]{
R.drawable.card_users,
R.drawable.card_unit,
R.drawable.card_request,
R.drawable.card_request_pending,
R.drawable.stats_main,
R.mipmap.coinsicon_cards,
};
String[] stats = new String[]{
userStatsFormated,villasStatsFormated,reqStatsFormated,pendingreqStatsFormated,"",pendingCollectionFormated,
};
CardModel c = new CardModel(stats[0], "Number Of Users", "", covers[0],null);
cardModelList.add(c);
c = new CardModel(stats[1], "Number", "", covers[1],null);
cardModelList.add(c);
c = new CardModel(stats[2], "Total", "", covers[2],null);
cardModelList.add(c);
c = new CardModel(stats[3], "Request", "", covers[3],null);
cardModelList.add(c);
c = new CardModel(stats[4], "Graph", "", covers[3],null);
cardModelList.add(c);
c = new CardModel(stats[5], "Pending", "", covers[4],null);
cardModelList.add(c);
从onCreateView Holder开始的Card Adapter
@Override
public CardViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
CardViewHolder rcv = null;
switch (viewType)
{
case 1:
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view_sec, null);
rcv = new TopGridCardView(layoutView);
break;
case 2:
View layoutView3 = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recycler_view, null);
chart = (BarChart) layoutView3.findViewById(R.id.chart1);
BARENTRY = new ArrayList<BarEntry>();
BarEntryLabels = new ArrayList<String>();
AddValuesToBARENTRY();
AddValuesToBarEntryLabels();
Bardataset = new BarDataSet(BARENTRY, "Projects");
BARDATA = new BarData(BarEntryLabels, Bardataset);
Bardataset.setColors(ColorTemplate.COLORFUL_COLORS);
chart.setData(BARDATA);
chart.animateY(3000);
rcv = new GraphCardView(layoutView3);
break;
case 3:
View layoutView2 = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view, null);
rcv = new BottomCardView(layoutView2);
break;
}
return rcv;
@Override
public void onBindViewHolder(CardViewHolder holder, final int position) {
CardModel cardModel = cardList.get(position);
if (holder.getItemViewType() == 1) {
TopGridCardView vholder = (TopGridCardView) holder;
vholder.secTitleStats.setText(cardModel.getCardTitles());
vholder.secNumStats.setText(cardModel.getNumStats());
vholder.secCardStats.setText(cardModel.getSecCartNumStats());
if (position == 0) {
vholder.secthumbnail.setBackgroundColor(Color.parseColor("#137927"));
}
if (position == 1) {
vholder.secthumbnail.setBackgroundColor(Color.parseColor("#E91E63"));
}
if (position == 2) {
vholder.secthumbnail.setBackgroundColor(Color.parseColor("#02BBD2"));
}
if (position == 3) {
vholder.secthumbnail.setBackgroundColor(Color.parseColor("#9900FF"));
}
Glide.with(mContext).load(cardModel.getThumbnail()).into(vholder.secthumbnail);
}
else if (holder.getItemViewType() == 2) {
GraphCardView vholder = (GraphCardView) holder;
vholder.BarChart.setData(BARDATA);
}
else if (holder.getItemViewType() == 3) {
BottomCardView vholder = (BottomCardView) holder;
vholder.numStats.setText(cardModel.getNumStats());
vholder.image_title.setText(cardModel.getCardTitles());
}
}
这里设置获取物品数量和位置returns
@Override
public int getItemViewType(int position) {
if (isPositionHeader(position)) {
return 2;
}
return 1;
}
@Override
public int getItemCount(){
return 6;
}
private boolean isPositionHeader(int position) {
if (position == 4) {
return position == 4;
}
return position == 6;
}
您的问题是,您的尺寸跨度查找需要 3 种不同的项目类型,而实际上您只有 2 个,即值 2 或 1。将尺寸跨度查找中的代码更改为:
switch (adapter.getItemViewType(position)) {
case 1: //this is for view type 1 (grid item)
return 1;
case 2: //this is for view type 2 (full width item)
return 2;
default:
return 1; //will default to grid item
}
private boolean isPositionHeader(int position) {
if (position == 4 || position == 5) {
return true;
}
}
大小跨度查找仅确定项目应跨越多少列,因此在您的示例中,根据视图类型,它将是 2 列或 1 列。