【问题标题】:OnClickListener for each cell in a tableLayout?TableLayout中每个单元格的OnClickListener?
【发布时间】:2014-02-27 13:47:48
【问题描述】:

我有一个大小为 3X3 的表格布局,如下图所示。

每个单元格都包含一个图像视图。我尝试将 onClickListener 用于单个图像,但我认为这不是一个非常聪明的方法。如果您有任何解决方案可以以更简单的方式完成,那么我想请您提出您的想法。 I also need to operlay a transparent layer to the imageview when the image view is selected.

提前谢谢你!

【问题讨论】:

    标签: android android-layout onclicklistener android-tablelayout


    【解决方案1】:

    您可以在您的适配器上为转换视图制作 onClickListener 以用于网格视图

    >  public class GridAdapter extends BaseAdapter {  
    >      private Context mContext;  
    >       
    >      // Gets the context so it can be used later  
    >      public GridAdapter(Context c) {  
    >       mContext = c;  
    >      }  
    >       
    >      // Total number of things contained within the adapter  
    >      public int getCount() {  
    >       return imagesUrlArray size or what you use here;  
    >      }  
    >       
    > 
    >      public Object getItem(int position) {  
    >       return null;  
    >      }  
    >       
    >       
    >      public long getItemId(int position) {  
    >       return position;  
    >      }  
    >       
    >      public View getView(int position,View convertView, ViewGroup parent) { 
    > 
    >  if (convertview == null) {
    >             convertview = activity.getLayoutInflater().inflate(layoutRes, null);
                  // your xml cell here
    >             holder = new ViewHolder(convertview);
    >             convertview.setTag(holder);
    >         } else {
    >             holder = (ViewHolder) convertview.getTag();
    >         } 
    >     
    >       holder.image.setImageBitmap(...) or what did yuu use Image adapter ... convertview.setOnclickListener....{do your job here}
    > 
    >       return convertview;  
    >      }  
    >     }
    

    活动中:

    yourGridview 定义在 xml 上而不是 tableLayout.setAdapter(GridAdapter);

    【讨论】:

    • 你会怎么做呢?可以给个代码吗?
    • 我已经找到了一个与您建议的完全不同的解决方案。所以我不接受这个作为答案。不管怎样,谢谢你的回答。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-15
    • 2011-03-18
    • 2023-04-02
    • 2015-07-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多