我正在顯示17個CardViews。RecyclerView適配器在錯誤的地方重複值
我正在使用RecyclerView來實現相同。
每個CardView顯示一個通用數據(格式)。
根據從JSON文件接收到的數據,卡可能會顯示一些額外的行。 。下圖顯示了2個顯示附加數據的CardView(焦點對準),其餘顯示了常用數據。
它顯示正確。但是當我向下滾動到視圖的底部時,最後一個CardView不會顯示任何行,但它會複製上面鏈接中顯示的2中的一個。 [可以發佈僅2個鏈接時間]
然後我滾動到頂部,並將其複製從被正確地顯示兩個CardViews行和分別重複這裏頂兩張牌:
我想什麼?
- 如果,否則
許多類似的問題表現出一個共同的解決方案。其中一個答案就在這裏。事情是我不知道什麼條件適用和在哪裏。這是我的代碼:
@Override
public void onBindViewHolder(DataObjectHolder holder, final int position) {
HashMap<Integer, ShoppingCartSummaryObject> dataItem = new HashMap<Integer, ShoppingCartSummaryObject>();
dataItem = mDataset.get(position);
TextView textViewComment = holder.textViewComment;
ShoppingCartSummaryObject obj = new ShoppingCartSummaryObject();
obj = (ShoppingCartSummaryObject) dataItem.get(position);
holder.textViewPackageType.setText(obj.getPackageType());
holder.textViewPackageCode.setText(obj.getPackageCode());
holder.textViewPackageDesc.setText(obj.getPackageDesc());
params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
try {
for (int i = 0; i < obj.getArrayListPart().size(); i++) {
View view2 = LayoutInflater.from(mContext).inflate(R.layout.detailrow, null);
holder.advPartDesc = (TextView) view2.findViewById(R.id.advPartDesc);
holder.advPartnum = (TextView) view2.findViewById(R.id.advPartNum);
cv = holder.cv;
advisoryLayout = holder.advisoryLayout;
empty = holder.empty;
layoutPackageSummary = holder.layoutPackageSummary;
holder.detailRow = (RelativeLayout) view2.findViewById(R.id.detailRow);
rallyLayout = new RelativeLayout(mContext);
rallyLayout.addView(holder.detailRow);
cnt+=1;
rallyLayout.setId(cnt);
holder.layoutpartList.addView(rallyLayout);
if (i == 0) { //for adding the first row below standard data
params.addRule(RelativeLayout.BELOW, empty.getId());
holder.advPartnum.setText(obj.getArrayListPart().get(i).partCode);
holder.advPartDesc.setText(obj.getArrayListPart().get(i).partDesc);
rallyLayout.setLayoutParams(params);
} else { //for more than one rows, 2nd row appearing below 1st and 3rd below 2nd and so on
params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, cnt - 1);
holder.advPartnum.setText(obj.getArrayListPart().get(i).partCode);
holder.advPartDesc.setText(obj.getArrayListPart().get(i).partDesc);
rallyLayout.setLayoutParams(params);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
,這是我多努力,從共享的鏈接實現的答案:
if(obj.getArrayListPart().size()>0) { /*added the if() condition*/
if (i == 0) {
params.addRule(RelativeLayout.BELOW, empty.getId());
holder.advPartnum.setText(obj.getArrayListPart().get(i).partCode);
holder.advPartDesc.setText(obj.getArrayListPart().get(i).partDesc);
rallyLayout.setLayoutParams(params);
} else {
params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, cnt - 1);
holder.advPartnum.setText(obj.getArrayListPart().get(i).partCode);
holder.advPartDesc.setText(obj.getArrayListPart().get(i).partDesc);
rallyLayout.setLayoutParams(params);
}
}
else
{
holder.advPartnum.setText("abc");
holder.advPartDesc.setText("abc");
}
}
} catch (Exception e) {
e.printStackTrace();
}
我還補充說:
@Override
public long getItemId(int position) {
return super.getItemId(position);
}
毫無效果。
我檢查了JSON數據,從列表中刪除了一個數組,並且不管我刪除的卡是什麼,它還影響從頂部/底部行(cardviews)中刪除重複行值,具體取決於位於卡之間的卡額外的細節和卡上/下。還嘗試過:
mAdapter.setHasStableIds(true);
但這只是略過了所有額外的行數據出現在公共標準以下。
請幫忙。我提供了我所嘗試的任何東西。
http://stackoverflow.com/questions/34487921/recycler-view-not-giving-the-滾動後的正確位置 –
@IntelliJAmiya:我沒有單獨使用GridView。我還應該嘗試這個鏈接嗎? P.S:[Code](http://pastie.org/10656753) –
@IntelliJAmiya:我仍然無法正確理解。看來setOnClickListener()解決了他的問題。但是,我需要嗎?我不認爲我需要setOnClickListener。我將上述鏈接放在setOnClickListener()中的代碼解決了我猜測的問題。但我的情況是不同的,雖然問題似乎很相似。 –