android - 在 imageview 上设置 onclicklistener

标签 android imageview expandablelistview

我正在使用可扩展 ListView 。因为我需要使用可点击的 ImageView 。我试过关注但没有用。它给出了空指针异常。

这是我的 xml 和 ListView 适配器。我需要在 ImageView 上放置点击事件。来自正常 Activity 的 onclicklistner 工作正常,但如果将其放在 ExpandableListActivity 中,则会出错。

提前致谢..

public class ExpList extends ExpandableListActivity
{
//    static final String parent_node[] = { "grey", "blue", "yellow", "red" };

String parent_node[];

static final String shades[][] = {
  {
    "lightgrey","#D3D3D3",
    "dimgray","#696969",
    "sgi gray 92","#EAEAEA"
  }, {},
  {
    "dodgerblue 2","#1C86EE",
    "steelblue 2","#5CACEE",
    "powderblue","#B0E0E6"
  }, {}
};

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle)
{
    super.onCreate(icicle);
    setContentView(R.layout.main);   


    LayoutInflater group_inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
     View group_layout = group_inflater.inflate(R.layout.group_row, null);

    ImageView img_call = (ImageView)group_layout.findViewById(R.id.imgcall);

    img_call.setOnClickListener(new OnClickListener() 
    {           
  @Override
  public void onClick(View v) 
      {
        Toast.makeText(getBaseContext(), "clicked...", Toast.LENGTH_SHORT).show();
     }); 

SimpleExpandableListAdapterWithEmptyGroups expListAdapter =
            new SimpleExpandableListAdapterWithEmptyGroups(
                this,
                createGroupList(),                      
                R.layout.group_row,                     
                new String[] { "colorName" },           
                new int[] { R.id.groupname },           
                createChildList(),                      
                R.layout.child_row,                     
                new String[] { "shadeName", "rgb" },    
                new int[] { R.id.childname, R.id.rgb }  
            );
        setListAdapter( expListAdapter );}

我的组 View xml 是:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

<ImageView 
    android:src="@drawable/expander_group"
    android:id="@+id/explist_indicator"
    android:layout_width="35px"
    android:layout_height="35px"/>


<ImageView 
    android:src="@drawable/contact_thumbnail"
    android:id="@+id/img_contact_thumbnail"
    android:layout_width="50px"
    android:layout_height="50px"/>

<TextView
    android:id="@+id/groupname"
    android:layout_width="190px"
    android:layout_height="match_parent"
    android:paddingLeft="30px"
    android:textSize="16px"
    android:textStyle="bold" />

<ImageView 
    android:id="@+id/imgcall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/call"
    android:clickable="true" />
</LinearLayout>

编辑
现在 NullPointerException 问题解决了。但我想出了 onclicklistner 不工作的新问题。没有 Toast 或该部分中编写的任何其他内容不会执行。我调试程序,发现单击 imageview 时,代码不会在 onclicklistner 上返回。

有什么想法吗?

最佳答案

你可以试试这个,希望它能正常工作。只需在 imageview 中的 xml 中添加两行,然后编写一个函数来监听 Activity 类中的按钮点击:

<ImageView 
    android:id="@+id/imgcall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/call"
    android:clickable="true" 
    android:onClick="onClickHandler"/>

在你的 Activity 课上:

public void onClickHandler (View v) {
    switch (v.getId()) {
        case R.id.imgcall:
            // do what ever you want here ...
            break;
    }
}

关于android - 在 imageview 上设置 onclicklistener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9644931/

相关文章:

android - 如何获取 ExpandableListView 在 Android 上可绘制的组指示器的宽度?

java - 自定义ListView不显示

android - 为什么我的图像在横向模式下消失了?

java - scaleType和wrap_content如何确定ImageView的宽度和高度?

android - 可展开的 ListView onGroupExpanded 和 onGroupCollapsed

android - ImageView 比例类型在列表 Activity 中不起作用

安卓 SQLite : SQLiteException: near "?" syntax error

java - 创建计算器

android - TextView 未在自定义 View 中更新

android - 在运行时将项目添加到 gridview 而不是替换以前的项目