你能告诉我如何使用 v.gettag() 更改我动态创建的线性布局的图像。我想我需要使用另一个 viewHolder 来保存 LinearLayout 中的项目。请看看下面提到的一段代码我的适配器类。
这是一段代码:-
viewHolder.imgView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
viewHolder.lnrLayout.setVisibility(View.VISIBLE);
viewHolder.imgView.setVisibility(View.GONE);
for (int i = 0; i < mList.size(); i++) {
// TODO creation of buttons and image-views and views
testText = new TextView(activity);
testText.setText("TestData");
mview = new View(activity);
mImageView = new ImageView(activity);
mImageView.setImageDrawable(activity.getResources()
.getDrawable(R.drawable.check));
mImageView.setPadding(5, 5, 5, 5);
// TODO Linear-Layout in which they all are binded.
linearlayout = new LinearLayout(activity);
linearlayout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
layoutParams.setMargins(5, 5, 5, 5);
linearlayout.setLayoutParams(layoutParams);
linearlayout.setBackground(activity.getResources()
.getDrawable(R.drawable.background_shapes));
// TODO setting their respective params.
LinearLayout.LayoutParams imageParams = new LinearLayout.LayoutParams(
0, LinearLayout.LayoutParams.MATCH_PARENT,
0.23f);
mImageView.setLayoutParams(imageParams);
LinearLayout.LayoutParams viewParams = new LinearLayout.LayoutParams(
0, LinearLayout.LayoutParams.MATCH_PARENT,
0.01f);
mview.setBackgroundColor(Color.WHITE);
mview.setLayoutParams(viewParams);
LinearLayout.LayoutParams TextParams = new LinearLayout.LayoutParams(
0, LinearLayout.LayoutParams.MATCH_PARENT,
0.85f);
testText.setGravity(Gravity.CENTER);
testText.setLayoutParams(TextParams);
// TODO Adding to Dynamic Linear-layout.
linearlayout.addView(testText);
linearlayout.addView(mview);
linearlayout.addView(mImageView);
// TODO Adding to Main Linear-layout
viewHolder.lnrLayout.addView(linearlayout);
.indexOfChild(linearlayout);
linearlayout.setTag(Integer.toString(idx));
linearlayout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String idxStr = (String) v.getTag();
Toast.makeText(activity, idxStr,
Toast.LENGTH_SHORT).show();
}
});
}
}
});
第二种方法: -
static class ViewHolder {
TextView tvProductName;
ImageView imgView;
View viewRightLine;
ScrollView scrlLstItems;
LinearLayout lnrLayout;
}
static class InternalViewHolder {
TextView tvProductName;
ImageView imgViewProduct;
View viewdivider;
LinearLayout mLayout;
}
@Override
public View getView(LayoutInflater inflater, ViewGroup parent) {
final ViewHolder viewHolder;
mView = inflater.inflate(R.layout.selector_item_compare, null);
if (mView != null) {
viewHolder = new ViewHolder();
viewHolder.tvProductName = (TextView) mView
.findViewById(R.id.tv_product);
viewHolder.imgView = (ImageView) mView
.findViewById(R.id.img_product);
viewHolder.viewRightLine = (View) mView
.findViewById(R.id.view_right_line);
viewHolder.lnrLayout = (LinearLayout) mView
.findViewById(R.id.lnr_test);
viewHolder.tvProductName.setText(Name);
viewHolder.imgView.setImageResource(Image);
viewHolder.imgView.setOnClickListener(new OnClickListener() {
@SuppressWarnings("unused")
@Override
public void onClick(View v) {
viewHolder.lnrLayout.setVisibility(View.VISIBLE);
viewHolder.imgView.setVisibility(View.GONE);
for (int i = 0; i < mList.size(); i++) {
InternalViewHolder holder = null;
if (holder == null) {
holder = new InternalViewHolder();
holder.imgViewProduct = new ImageView(activity);
holder.tvProductName = new TextView(activity);
holder.viewdivider = new View(activity);
holder.mLayout = new LinearLayout(activity);
// TODO creation of buttons and image-views and
// views
holder.imgViewProduct.setImageDrawable(activity
.getResources().getDrawable(
R.drawable.check));
holder.imgViewProduct.setPadding(5, 5, 5, 5);
// TODO Linear-Layout in which they all are binded.
holder.mLayout
.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
layoutParams.setMargins(5, 5, 5, 5);
holder.mLayout.setLayoutParams(layoutParams);
holder.mLayout.setBackground(activity
.getResources().getDrawable(
R.drawable.background_shapes));
// TODO setting their respective params.
LinearLayout.LayoutParams imageParams = new LinearLayout.LayoutParams(
0, LinearLayout.LayoutParams.MATCH_PARENT,
0.23f);
holder.imgViewProduct.setLayoutParams(imageParams);
holder.imgViewProduct.setVisibility(View.GONE);
LinearLayout.LayoutParams viewParams = new LinearLayout.LayoutParams(
0, LinearLayout.LayoutParams.MATCH_PARENT,
0.01f);
holder.viewdivider.setBackgroundColor(Color.WHITE);
holder.viewdivider.setLayoutParams(viewParams);
holder.viewdivider.setVisibility(View.GONE);
LinearLayout.LayoutParams TextParams = new LinearLayout.LayoutParams(
0, LinearLayout.LayoutParams.MATCH_PARENT,
0.85f);
holder.tvProductName.setGravity(Gravity.CENTER);
holder.tvProductName.setLayoutParams(TextParams);
// TODO Adding to Dynamic Linear-layout.
holder.mLayout.addView(holder.tvProductName);
holder.mLayout.addView(holder.viewdivider);
holder.mLayout.addView(holder.imgViewProduct);
// TODO Adding to Main Linear-layout
holder.tvProductName.setText(mCategories.get(i)
.getName());
viewHolder.lnrLayout.addView(holder.mLayout);
viewHolder.lnrLayout.setTag(holder);
} else {
holder = (InternalViewHolder) viewHolder.lnrLayout
.getTag();
}
// int idx = viewHolder.lnrLayout
// .indexOfChild(holder.mLayout);
// holder.mLayout.setTag(Integer.toString(idx));
holder.mLayout
.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// String idxStr = (String) v.getTag();
// Toast.makeText(activity, idxStr,
// Toast.LENGTH_SHORT).show();
holder.imgViewProduct
.setVisibility(View.VISIBLE);
holder.viewdivider
.setVisibility(View.VISIBLE);
}
});
}
}
});
viewHolder.tvProductName.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (activity.getResources().getBoolean(
R.bool.landscape_only)) {
viewHolder.lnrLayout.removeAllViews();
viewHolder.lnrLayout.setVisibility(View.GONE);
viewHolder.imgView.setVisibility(View.VISIBLE);
}
}
});
mView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) mView.getTag();
}
return mView;
}
谢谢