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

How to add button to custom adapter?

我正在为我的 project.When 使用 FunDapter 我尝试将 imagebutton 添加到我的布局中其他文本变成 unclickable.Does 当我单击时什么都没有 them.How 我可以将按钮添加到这个适配器?

下面是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

 <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"
    />

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

   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 中的文本提供可点击属性时,它们会停止 working.Like 当我玩布局时 XML 一切都停止工作。

在适配器中Class

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

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

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

  • 按钮无法聚焦 android:focusable="false"

First of all if u are using button or edit text in your adapter or row file Then every thing will get stop working .
You should use

1.Set descendant Focusablity="before Descendants" in the list view.
2.Set window Soft Input Mode="adjust Pan" in the manifest
3.Set list view.set Items Can Focus(true)

您必须为所有视图提供单独的点击事件。

Your ImageButton has android:clickable="true" which makes sub view clickable. But you other views are not clickable.

解决方案

对于textview中的点击事件

textView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//... }
});
similar for Imagebutton
remove

android:clickable="true"

希望对您有所帮助。