我已经找到了如何设置RecyclerView.Adapter
多个ViewHolders
以便我想要做什么,但我无法弄清楚如何在一个{{{{}}中展示多个CardView
布局1}}或Activity
。 Fragment
的布局始终显示数据库中检索的最后一组信息的onr。有没有办法在一个CardView
中为CardView
设置多个布局?
编辑:这是我的适配器
公共类TimeCardAdapter扩展了RecyclerView.Adapter {
Fragment
这是我希望用
填充每个 private ArrayList<ChecksGetterSetter> checksList = new ArrayList<>();
private static final int TYPE_CHECKS = 0;
private static final int TYPE_CHECKINFO = 1;
public class ViewHolder extends RecyclerView.ViewHolder {
public ViewHolder(View itemView) {
super(itemView);
}
}
public class ChecksViewHolder extends ViewHolder {
@InjectView(R.id.CCtextviewDate) TextView tvcPayPeriod;
@InjectView(R.id.CCtextviewHoursInfo) TextView tvcHours;
@InjectView(R.id.CCtextviewOvertimeHours) TextView cOTHours;
@InjectView(R.id.CCtextviewOvertimeHoursInfo) TextView tvcOTHours;
@InjectView(R.id.CCtextviewPayInfo) TextView tvcRate;
@InjectView(R.id.CCtextviewOvertimePay) TextView cOTRate;
@InjectView(R.id.CCtextviewOvertimePayInfo) TextView tvcOTRate;
@InjectView(R.id.CCtextviewGrossPayInfo) TextView tvcGrossPay;
@InjectView(R.id.CCtextviewAfterTaxInfo) TextView tvcAfterTax;
public ChecksViewHolder(View itemView) {
super(itemView);
ButterKnife.inject(this, itemView);
}
}
public class CheckInfoViewHolder extends ViewHolder {
@InjectView(R.id.CCItextviewSocialSecurityInfo) TextView tvSocialSecurity;
@InjectView(R.id.CCItextviewMedicareInfo) TextView tvMedicare;
@InjectView(R.id.CCItextviewFederalIncomeTaxInfo) TextView tvFederalIncomeTax;
@InjectView(R.id.CCItextviewAllowancesInfo) TextView tvAllowances;
@InjectView(R.id.CCItextviewDeductions) TextView deductions;
@InjectView(R.id.CCItextviewDeductionsInfo) TextView tvDeductions;
public CheckInfoViewHolder(View itemView) {
super(itemView);
ButterKnife.inject(this, itemView);
}
}
@Override
public int getItemViewType(int position) {
int viewType;
if(checksList.get(position).getPeriodStart() != null){
viewType = TYPE_CHECKS;
} else {
viewType = TYPE_CHECKINFO;
}
return viewType;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
LayoutInflater mInflater = LayoutInflater.from(viewGroup.getContext());
switch (viewType) {
case TYPE_CHECKS:
ViewGroup vgChecks = (ViewGroup) mInflater.inflate(R.layout.cardview_checks, viewGroup, false);
return new ChecksViewHolder(vgChecks);
case TYPE_CHECKINFO:
ViewGroup vgCheckInfo = (ViewGroup) mInflater.inflate(R.layout.cardview_check_info, viewGroup, false);
return new CheckInfoViewHolder(vgCheckInfo);
default:
ViewGroup vgTimeCard1 = (ViewGroup) mInflater.inflate(R.layout.cardview_checks, viewGroup, false);
return new ChecksViewHolder(vgTimeCard1);
}
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
switch (viewHolder.getItemViewType()) {
case TYPE_CHECKS:
ChecksViewHolder holder = (ChecksViewHolder) viewHolder;
ChecksGetterSetter pos = checksList.get(position);
if (pos.getPeriodEnd().equals("") || pos.getPeriodEnd() == null) {
holder.tvcPayPeriod.setText(pos.getPeriodStart());
} else {
holder.tvcPayPeriod.setText(pos.getPeriodStart() + " - " + pos.getPeriodEnd());
}
holder.tvcHours.setText(pos.getHours());
if (!pos.getOtHours().equals("0.0")) {
holder.tvcOTHours.setText(pos.getOtHours());
} else {
holder.cOTHours.setVisibility(View.GONE);
holder.tvcOTHours.setVisibility(View.GONE);
}
holder.tvcRate.setText(pos.getRate());
if (!pos.getOtRate().equals("$0.00")) {
holder.tvcOTRate.setText(pos.getOtRate());
} else {
holder.cOTRate.setVisibility(View.GONE);
holder.tvcOTRate.setVisibility(View.GONE);
}
holder.tvcGrossPay.setText(pos.getGrossPay());
holder.tvcAfterTax.setText(pos.getAfterTax());
break;
case TYPE_CHECKINFO:
CheckInfoViewHolder infoHolder = (CheckInfoViewHolder) viewHolder;
ChecksGetterSetter posi = checksList.get(position);
infoHolder.tvSocialSecurity.setText(posi.getSocialSecurity());
infoHolder.tvMedicare.setText(posi.getMedicare());
infoHolder.tvFederalIncomeTax.setText(posi.getFederalIncomeTax());
infoHolder.tvAllowances.setText(posi.getAllowances());
if (!posi.getDeductions().equals("$0.00")) {
infoHolder.tvDeductions.setText(posi.getDeductions());
} else {
infoHolder.deductions.setVisibility(View.GONE);
infoHolder.tvDeductions.setVisibility(View.GONE);
}
break;
}
}
@Override
public int getItemCount() {
return checksList.size();
}
public ChecksGetterSetter getItem(int position){
return checksList.get(position);
}
public void addItem(ChecksGetterSetter check) {
checksList.add(check);
notifyItemInserted(checksList.size() - 1);
}
public void deleteItem(int index){
checksList.remove(index);
notifyItemRemoved(index);
}
public TimeCardAdapter(List<ChecksGetterSetter> check) {
if(check == null){
throw new IllegalArgumentException("check must not be null");
}
this.checksList = new ArrayList<>(check);
}
}
的信息
ViewHolders
在 private void getCheckInfo() {
if (checkCursor.getCount() != 0) {
while (checkCursor.moveToNext()) {
hours = checkCursor.getDouble(checkCursor.getColumnIndex(ChecksDBAdapter.WORKED));
overtimeHours = checkCursor.getDouble(checkCursor.getColumnIndex(ChecksDBAdapter.OVERTIMEWORKED));
payrate = checkCursor.getDouble(checkCursor.getColumnIndex(ChecksDBAdapter.PAYRATE));
overtimePayrate = checkCursor.getDouble(checkCursor.getColumnIndex(ChecksDBAdapter.OVERTIMEPAYRATE));
beforeTax = checkCursor.getDouble(checkCursor.getColumnIndex(ChecksDBAdapter.BEFORETAXES));
afterTax = checkCursor.getDouble(checkCursor.getColumnIndex(ChecksDBAdapter.AFTERTAXES));
check = new ChecksGetterSetter(
"", "",
String.valueOf(decimalToTime(hours)),
String.valueOf(decimalToTime(overtimeHours)),
moneyFormat(payrate),
moneyFormat(overtimePayrate),
moneyFormat(beforeTax),
moneyFormat(afterTax)
);
adapter.addItem(check);
}
}
}
private void getAdditionalCheckInfo() {
if (checkCursor.getCount() != 0) {
while (checkCursor.moveToNext()) {
socialSecurity = checkCursor.getDouble(checkCursor.getColumnIndex(ChecksDBAdapter.SOCIALSECURITY));
medicare = checkCursor.getDouble(checkCursor.getColumnIndex(ChecksDBAdapter.MEDICARE));
federalIncomeTax = checkCursor.getDouble(checkCursor.getColumnIndex(ChecksDBAdapter.FEDERALTAXES));
deductions = checkCursor.getDouble(checkCursor.getColumnIndex(ChecksDBAdapter.DEDUCTIONS));
allowances = checkCursor.getInt(checkCursor.getColumnIndex(ChecksDBAdapter.ALLOWANCES));
checkInfo = new ChecksGetterSetter(
moneyFormat(socialSecurity),
moneyFormat(medicare),
moneyFormat(federalIncomeTax),
moneyFormat(allowances),
moneyFormat(deductions)
);
adapter.addItem(checkInfo);
}
}
}
周期之后,它会填充getCheckInfo()
就好了,但在ViewHolder
个周期后,它会替换第一个的getAdditionalCheckInfo()
和第二个{显示他们上方的空白卡片。我做错了什么?
答案 0 :(得分:0)
您可以使用唯一的视图持有者,您只需要放置一个对象列表并填入onBindView,请参阅示例:
public class CustomAdapter extends RecyclerView.Adapter<MainSearchAdapter.ViewHolder> {
private List<Object> data;
public CustomAdapter(Lis<Object> data) {
this.data = data;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_layout, parent, false);
return new ViewHolder(itemView);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Object object = data.get(position);
holder.textView.setText("bla");
}
@Override
public int getItemCount() {
return data.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView textView;
public ViewHolder(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.text_view);
}
}