我需要在我的viewHolder對象中設置標籤的幫助。我有一個比屏幕更長的列表(在電話上),我需要爲每個視圖設置不同的標籤。正如你可以在我的預覽應用程序(下面的鏈接)中看到的外部片段中的第一項和最後一項綁定在一起。我不'那樣想,我希望每一個觀點都是獨一無二的。我怎樣才能做到這一點。指定唯一標籤列表中的項目列表
https://drive.google.com/file/d/0B2xlu23GjnfvTEgtbkprOThtMmM/edit?usp=sharing
感謝
我InspectionListAdapter
public class InspectionListAdapter extends ArrayAdapter<String> {
private final Activity context;
private final String[] inspectionTitles;
private final String[] description;
public InspectionListAdapter(Activity context, String[] inspectionTitles,
String[] description) {
super(context, R.layout.inspection_item_layout, inspectionTitles);
this.context = context;
this.inspectionTitles = inspectionTitles;
this.description = description;
}
public static class ViewHolder {
public ImageView imageView, expandButton;
public TextView txtTitle;
public TextView txtDescription;
public RelativeLayout expandLayout;
public EditText ownDesc, estCost;
public LayerDrawable listItem;
public GradientDrawable color;
public CheckBox checkRepair, checkDone;
public RelativeLayout mainLayout;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
final ViewHolder viewHolder;
View rowView = view;
if (rowView == null) {
LayoutInflater inflater = context.getLayoutInflater();
rowView = inflater.inflate(R.layout.inspection_item_layout, null,
true);
viewHolder = new ViewHolder();
viewHolder.mainLayout = (RelativeLayout) rowView
.findViewById(R.id.item_layout_main);
viewHolder.txtTitle = (TextView) rowView
.findViewById(R.id.textViewItemTitle);
viewHolder.txtDescription = (TextView) rowView
.findViewById(R.id.textViewTip);
viewHolder.listItem = (LayerDrawable) rowView.getResources()
.getDrawable(R.drawable.layer_car_background);
viewHolder.color = (GradientDrawable) (viewHolder.listItem
.findDrawableByLayerId(R.id.item_list_background));
viewHolder.imageView = (ImageView) rowView
.findViewById(R.id.imageViewItem);
viewHolder.expandButton = (ImageView) rowView
.findViewById(R.id.imageViewTitle);
viewHolder.ownDesc = (EditText) rowView
.findViewById(R.id.editTextDescription);
viewHolder.estCost = (EditText) rowView
.findViewById(R.id.editTextEstCost);
viewHolder.checkRepair = (CheckBox) rowView
.findViewById(R.id.checkBox2);
viewHolder.checkDone = (CheckBox) rowView
.findViewById(R.id.checkBox1);
viewHolder.checkRepair
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton view,
boolean arg1) {
// TODO Auto-generated method stub
if (view.isChecked()) {
viewHolder.checkDone.setChecked(false);
}
}
});
viewHolder.checkDone
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton view,
boolean arg1) {
// TODO Auto-generated method stub
if (view.isChecked()) {
viewHolder.checkRepair.setChecked(false);
}
}
});
viewHolder.expandLayout = (RelativeLayout) rowView
.findViewById(R.id.relativeLayoutExpandable);
viewHolder.expandLayout.setVisibility(View.GONE);
viewHolder.expandButton
.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if (viewHolder.expandLayout.getVisibility() == View.VISIBLE) {
viewHolder.expandButton
.setImageResource(R.drawable.ic_action_expand);
viewHolder.expandLayout
.setVisibility(View.GONE);
if (viewHolder.checkDone.isChecked()) {
viewHolder.mainLayout
.setBackgroundResource(R.drawable.layer_car_background_green);
}
if (viewHolder.checkRepair.isChecked()) {
viewHolder.mainLayout
.setBackgroundResource(R.drawable.layer_car_background_red);
}
} else {
viewHolder.expandButton
.setImageResource(R.drawable.ic_action_collapse);
viewHolder.expandLayout
.setVisibility(View.VISIBLE);
viewHolder.mainLayout
.setBackgroundResource(R.drawable.layer_car_background);
}
}
});
rowView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) rowView.getTag();
}
viewHolder.txtTitle.setText(inspectionTitles[position]);
viewHolder.txtDescription.setText("Tip: \n" + description[position]);
viewHolder.imageView.setImageResource(R.drawable.ic_launcher);
viewHolder.expandButton
.setImageResource(R.drawable.ic_action_expand);
return rowView;
}
}
我建議你學習getView的工作原理。意見將得到回收。而且你不應該使「每個視圖都是唯一的」 – 2014-09-10 16:16:00
我的列表不是一個動態列表,它是由string.xml文件中的字符串數組生成的預設列表。我希望我的所有觀點都保留 其onClickListeners和onCheckChangedListeners – AbsoluteHeero 2014-09-10 16:34:09
請解釋您試圖爲每個視圖設置不同標記的位置。我所看到的是,您將視圖持有者設置爲視圖標記。他們在哪個方面有所不同?什麼'保持點擊聽衆等'這樣做呢?當然還有更多的物品可以放在屏幕上。而您的Google文檔在我的平板電腦上無法讀取,因此我不知道那裏出了什麼問題。 – greenapps 2014-09-10 20:00:37