android - 如何将按钮添加到自定义适配器?

标签 android android-layout android-adapter android-imagebutton

我正在使用 FunDapter对于我的项目。当我尝试将 imagebutton 添加到我的布局时,其他文本变得不可点击。当我单击它们时什么都不做。我如何将按钮添加到此适配器?

下面是 list_layout,一切正常

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Isimsiz"
    android:id="@+id/Housename"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginLeft="2dp"
    android:clickable="false"
    android:textColor="#241d1d"
    android:layout_row="0"
    android:layout_column="0"
    android:layout_marginRight="2dp"
    android:layout_marginTop="2dp"
    android:layout_marginBottom="2dp" />



    <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Aciklama Yok"
    android:id="@+id/Desc"
    android:layout_below="@+id/Housename"
    android:clickable="false"
    android:layout_alignStart="@+id/Housename"
    android:textColor="#241d1d"
    android:layout_row="1"
    android:layout_column="0"
    android:layout_marginLeft="2dp"
    android:layout_marginRight="2dp"
    android:layout_marginTop="2dp"
    android:layout_marginBottom="2dp"
    android:layout_alignEnd="@+id/Housename" />

    </RelativeLayout>

当我添加图像按钮时,除了图像按钮之外没有任何东西是可点击的

 <ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageButton"
    android:layout_alignTop="@+id/Housename"
    android:layout_alignParentEnd="true"
    android:layout_alignBottom="@+id/Desc"
    android:clickable="true"
    android:onClick="berk"
    />

这是我创建适配器的类的一部分

   public void processFinish(String s) {
    productList = new JsonConverter<Product>().toArrayList(s, Product.class);
    BindDictionary<Product> dict = new BindDictionary<Product>();
    dict.addStringField(R.id.Housename, new StringExtractor<Product>() {
        @Override
        public String getStringValue(Product product, int position) {
            return product.HouseID;
        }
    });

    dict.addStringField(R.id.Desc, new StringExtractor<Product>() {
        @Override
        public String getStringValue(Product product, int position) {
            return product.Desc;
        }
    });


    FunDapter<Product> adapter = new FunDapter<>(
    ListActivity.this, productList, R.layout.layout_list, dict);

    lvProduct = (ListView)findViewById(R.id.lvProduct);
    lvProduct.setAdapter(adapter);
    lvProduct.setOnItemClickListener(this);

}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    //do sth
}
public void berk(View v) {
    //do sth
}

注意:另外,当我将可点击属性赋予 xml 中的文本时,它们会停止工作。就像当我使用布局 XML 时,一切都会停止工作。

最佳答案

在适配器类中

public View getView(final int position, View convertView, ViewGroup parent) { LayoutInflater inflater = getLayoutInflater(); View row = inflater.inflate(R.layout.vehicals_details_row, parent, false); 按钮 deleteImageView = (Button) row.findViewById(R.id.DeleteImageView); deleteImageView.setOnClickListener(new OnClickListener() { public void onClick(View v) { //... } }); }

但是您可能会遇到一个问题 - listView 行不可点击。解决方案:

  • 使 ListView 可聚焦 android:focusable="true"

  • 按钮不可聚焦 android:focusable="false"

关于android - 如何将按钮添加到自定义适配器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39365107/

相关文章:

android - 是否可以为 Android Camera Preview 提供 Vertex Shader 和 Fragment Shader 等效果,并使用 OpenGLES 保存捕获的图像?

android - TextView 作为带有文本颜色操作的进度条?

android - 这个 Android 组件叫什么?

java - 图库 View 中的空白图像 – Android

android - 如何通过忽略大小写来过滤 ListView 适配器?

java - 如何使周期性事件在 AsyncTask 的 doInBackground 中的每个周期性间隔运行

javascript - 在 React Native 回调后重定向

Android Wear 隐藏 "Debugging over bluetooth"卡片

android - 我可以设置android :layout_toLeftOf programmatically?吗

android - 弹出窗口中的微调器错误,单击时崩溃