嗨我在项目视图对齐方面遇到问题我想要在网格中实现前四项,其余在线性视图项目中,有一个图形视图和线性动态视图。
我想要实现的目标
|Grid 0 | Grid 1 |
------------------
|Grid 2 | Grid 3 |
------------------
| LinearItem 0 |
------------------
| LinearItem 1 |
我遇到的问题
|Grid 0 | Grid 1 |
------------------
|Grid 2 | Grid 3 |
------------------
| Linear Item 0 |
------------------
|Line1|
在网格项视图中返回的最后一项,而不是使用线性项
的fullWidth从MainActivity开始,使用带有spanize的GridLayout Manager
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开始的卡片适配器
@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());
}
}
此处设置获取项目计数和位置返回
@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;
}
答案 0 :(得分:1)
您的问题是,您的大小跨度查找需要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。