Android ListView自定义适配器ImageButton

如何解决Android ListView自定义适配器ImageButton

| 如果有更好的方法,请告诉我,这可能不是正确的方法。 我创建了一个自定义适配器类,并在我的getView方法中为我要使用的视图充气
public View getView(int position,View convertView,ViewGroup parent) 
    {
        View v = mInflater.inflate(R.layout.wherelayout,null);
        if (convertView != null) 
        {
            v = convertView;
        }
        HashMap<String,Object> whereHash = (HashMap<String,Object>) this.getItem(position);
        if (whereHash != null) 
        {
            TextView whereId = (TextView) v.findViewById(R.id.tvWhere);
            TextView whereDetails = (TextView) v.findViewById(R.id.tvWhereDetails);
            ImageButton ibDelWhere = (ImageButton) v.findViewById(R.id.ibDelWhere);

            whereId.setText((CharSequence) whereHash.get(\"where\"));
            whereDetails.setText((CharSequence) whereHash.get(\"details\"));
            if (ibDelWhere != null)
            {
                ibDelWhere.setId(position);
                ibDelWhere.setOnClickListener(new OnClickListener() 
                  {

                    @Override
                    public void onClick(View v) 
                    {
                        //do stuff when clicked
                    }
                  }
                );
            }
        }
        return v;
    }
该视图由向左对齐的2个TextView和向右对齐的ImageButton组成,我希望能够在单击按钮时从ListView中删除该项目。布局是这样的-
    <RelativeLayout
xmlns:android=\"http://schemas.android.com/apk/res/android\"
android:layout_width=\"fill_parent\"
android:layout_height=\"fill_parent\" android:orientation=\"horizontal\" android:clickable=\"true\">
<TextView android:layout_height=\"wrap_content\" android:layout_width=\"wrap_content\" android:textSize=\"25sp\" android:id=\"@+id/tvWhere\" android:textColor=\"#00FF00\" android:text=\"TextView\" android:gravity=\"top|left\" android:layout_alignParentTop=\"true\" android:layout_alignParentLeft=\"true\"></TextView>
<TextView android:layout_height=\"wrap_content\" android:layout_width=\"wrap_content\" android:id=\"@+id/tvWhereDetails\" android:textColor=\"#0000FF\" android:text=\"TextView\" android:textSize=\"18sp\" android:layout_below=\"@+id/tvWhere\" android:gravity=\"bottom|left\" android:layout_alignParentLeft=\"true\"></TextView>
<ImageButton android:layout_height=\"wrap_content\" android:layout_width=\"wrap_content\" android:src=\"@drawable/eraser\" android:id=\"@+id/ibDelWhere\" android:layout_alignParentRight=\"true\" android:layout_alignParentTop=\"true\"></ImageButton>
</RelativeLayout>
问题是,当ImageButton在布局中时,我可以单击它,并且onClick()会按预期触发,但是我不能单击实际的列表项本身,即单击TextView项以触发ListView.onItemClick已经分配给它了。如果我从布局中删除ImageButton,则单击该项目时将触发ListView.onItemClick事件。有什么办法可以使我同时单击ListView项和布局中的按钮? 谢谢你们和加尔斯。     

解决方法

您必须将imagebutton设置为非
focusable
和非
focusableInTouchMode
(可单击即可)。 请注意,与其他视图相反,您不能在xml中执行此操作,因为
android:focusable
ImageButton
的构造函数中被覆盖。 更准确地说,这是
ImageView
ImageButton
之间的少数差异之一。亲自看看,这是ѭ5的完整资料。
@RemoteView
public class ImageButton extends ImageView {
    public ImageButton(Context context) {
        this(context,null);
    }

    public ImageButton(Context context,AttributeSet attrs) {
        this(context,attrs,com.android.internal.R.attr.imageButtonStyle);
    }

    public ImageButton(Context context,AttributeSet attrs,int defStyle) {
        super(context,defStyle);
        setFocusable(true);
    }

    @Override
    protected boolean onSetAlpha(int alpha) {
        return false;
    }
}
要解决此问题,只需从Java调用
setFocusable(false)
。或者使用
ImageView
:)
myImageButton.setFocusable(false);
希望能帮助到你。     ,您可以将两者都设为可点击状态,但实际上并不受它的支持,Romain Guy会对您大喊大叫。另外,您将无法使用轨迹球聚焦/按下按钮。话虽如此,您可以将以下属性添加到按钮上,这两个属性都应可单击:
android:focusable=\"false\"
android:focusableInTouchMode=\"false\"
只要确保您可以承受后果即可。     ,尝试设定 相对布局上的android:clickable = \“ false \”。 我在另一个Linearlayout中的LinearLayout遇到了相同的问题。 外部LinearLayout为clickable = true,结果: ListView.OnItemClickListener不会触发。 将其设置为clickable = false后,它可以工作。     ,  即,单击TextView项目以触发已分配给它的ListView.onItemClick。 当ImageButton在那里时,单击TextView会发生什么?它是否注册了单击按钮?还是什么都不做? ImageButton在行中占用多少空间?可能是因为它足够大,您无法单击它外面的行。     ,我有同样的问题。我的解决方案是为适配器内部的视图设置onClick方法,而不是使用onItemClick。
@Override
public View getView(int position,View convertView,ViewGroup parent) {

    View v = convertView;
    if(v == null){
        LayoutInflater inflater = context.getLayoutInflater();
        v = inflater.inflate(R.layout.list_item,parent,false);
    }
    v.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            //This should replace the onListItemClick method
        }
    });

    v.findViewById(R.id.someinnerview).setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            //Write one of these for each of your inner views
        }
    });
    return v;
}
希望有帮助!根据您程序的其余部分,这可能会迫使您将更多数据交给适配器(我必须这样做),但是它可以工作。     ,这是列表视图中定制适配器的示例。
<?xml version=\"1.0\" encoding=\"utf-8\"?>  
<RelativeLayout  
    android:id=\"@+id/relativeLayout1\"  
   android:layout_width=\"fill_parent\"  
    android:layout_height=\"fill_parent\"  
    xmlns:android=\"http://schemas.android.com/apk/res/android\"  
   android:padding=\"5dip\">  

   <ImageView  
      android:layout_width=\"50dip\"  
      android:layout_height=\"50dip\"  
    android:id=\"@+id/imgViewLogo\"  
       android:src=\"@drawable/icon\"  
      android:layout_alignParentLeft=\"true\"  
      android:layout_centerInParent=\"true\"  
      android:scaleType=\"center\">  
  </ImageView>  

 <TextView  
      android:textAppearance=\"?android:attr/textAppearanceLarge\"  
       android:layout_height=\"wrap_content\"  
      android:text=\"TextView\"  
      android:layout_width=\"wrap_content\"  
       android:id=\"@+id/txtViewTitle\"  
      android:layout_toRightOf=\"@+id/imgViewLogo\"  
       android:layout_marginLeft=\"2dip\">  
   </TextView>  

  <TextView  
      android:layout_height=\"wrap_content\"  
      android:text=\"TextView\"  
       android:layout_width=\"wrap_content\"  
       android:id=\"@+id/txtViewDescription\"  
       android:layout_toRightOf=\"@+id/imgViewLogo\"  
       android:layout_below=\"@+id/txtViewTitle\"  
       android:layout_marginLeft=\"2dip\">  
  </TextView>   

       <TextView  
      android:layout_height=\"wrap_content\"  
      android:text=\"TextView\"  
       android:layout_width=\"wrap_content\"  
       android:id=\"@+id/txtViewMobile\"  
       android:layout_toRightOf=\"@+id/imgViewLogo\"  
       android:layout_below=\"@+id/txtViewDescription\"  
       android:layout_marginLeft=\"2dip\">  
  </TextView>    
   并在BaseAdapter上制作Java文件
public class ListViewCustomAdapter extends BaseAdapter {
Context context;  
String[] mobile;
String[] month;
 String[] number;
 public LayoutInflater inflater; 


public ListViewCustomAdapter(Context context,String[] month,String[] number,String[] mobile) {
    // TODO Auto-generated constructor stub
     super();  
     this.context = context;  
    this.month=month;
    this.number=number;
    this.mobile=mobile;
    Log.i(\"88888888888888888\",\"*******333********\");
    this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
}

 //ADC71C 95AA1F

public int getCount() {  
    // TODO Auto-generated method stub  
    return month.length;  

}  

public Object getItem(int position) {  
    // TODO Auto-generated method stub  
    return position;  
}  

public long getItemId(int position) {  
    // TODO Auto-generated method stub  
    return 0;  
}  

private class ViewHolder {  
    TextView txtViewTitle;  
    ImageView imgViewLogo;  
    TextView txtViewDescription;
    TextView txtViewMobile;

}  

public View getView(int position,ViewGroup parent)  
{  
    // TODO Auto-generated method stub 
     Log.i(\"88888888888888888\",\"*******444********\");

    ViewHolder holder;  
    LayoutInflater inflater =  ((Activity) context).getLayoutInflater();  

    if (convertView == null)  
    {  
         Log.i(\"88888888888888888\",\"*******555********\");
        convertView = inflater.inflate(R.layout.listitem_row,null);  
        holder = new ViewHolder();  
        holder.imgViewLogo = (ImageView) convertView.findViewById(R.id.imgViewLogo);  
        holder.txtViewTitle = (TextView) convertView.findViewById(R.id.txtViewTitle);  
        holder.txtViewDescription = (TextView) convertView.findViewById(R.id.txtViewDescription);  
        holder.txtViewMobile = (TextView) convertView.findViewById(R.id.txtViewMobile);
        convertView.setTag(holder);  
    }  
    else  
    {  
         Log.i(\"88888888888888888\",\"*******666********\");
        holder = (ViewHolder) convertView.getTag();  
    }  
    Log.i(\"888888888888\",\"Display the value of the textbox like(9856321584)other wise (TextView)\");
    holder.txtViewTitle.setText(month[position]);  
    holder.txtViewDescription.setText(number[position]); 
    holder.txtViewMobile.setText(mobile[position]);

return convertView;  
}  
}  
    

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-