在适配器中单击动态线性布局时更改图像

Change image On click in dynamic Linear-Layout in adapter

你能告诉我如何使用 v.gettag() 更改动态创建的线性布局的图像吗?我想我需要使用另一个 viewHolder 来保存 LinearLayout.please 中的项目看看下面提到的我的适配器代码 class.

这是一段代码:-

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;
 }

谢谢

我尝试了你的第二种方法,所以如果我理解你的意思,你想让你检查图像在点击时可见,并在我点击同一个按钮时再次使它不可见 again.So,我在这里做了一些必要的更改您的代码...请查看它并告诉我它是否有效.........

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++) {
      final InternalViewHolder holder;

      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);

      holder.tvProductName.setText(mCategories.get(i)
        .getName());
      // TODO Adding to Main Linear-layout
      viewHolder.lnrLayout.addView(holder.mLayout);
      viewHolder.lnrLayout.setTag(holder);

      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();

          if (holder.imgViewProduct
            .getVisibility() == View.VISIBLE) {
           holder.imgViewProduct
             .setVisibility(View.GONE);
           holder.viewdivider
             .setVisibility(View.GONE);
          } else {
           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;
 }