由ArrayAdapter

时间:2017-07-28 11:04:24

标签: android listview onclicklistener

我有一个ListView,其内容由自定义ArrayAdapter处理。我正在使用的布局有一个TextView和一个ImageView

布局:

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

    <TextView
        android:id="@+id/string"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/default"
        android:textSize="20sp"/>

    <ImageView
        android:id="@+id/delete_row"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:src="@drawable/ic_cancel"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:contentDescription="@string/delete_row"/>

</RelativeLayout>

主Java类:

ListView listView = (ListView) findViewById(R.id.listView);
ArrayAdapter<String> arrayAdapter = new StringList(this, strings);
listView.setEmptyView(findViewById(R.id.empty));
listView.setAdapter(arrayAdapter);

现在我为洞项目宣布onClickListener,这很容易:

listView.setOnItemClickListener((parent, view, position, id) -> {
....
});

但是我知道我也想要onClickListener

ImageView

我不想将onClickListener放在自定义ArrayAdapter文件中,因为在点击图片时,应该会发生很多事情(不仅仅是删除行) 。

那么如何在由onClickListener管理的ImageView内为ListView设置ArrayAdapter

6 个答案:

答案 0 :(得分:1)

  

我还想为ImageView提供一个onClickListener

我们无法在onClickListener

中实现
  

我不想将onClickListener放在自定义ArrayAdapter中   文件

我们只能在Adapter

中获取ImageView点击监听器
  

点击图片时,应该会发生很多事情(不是   只删除行)

这可以通过Adapterview中的接口实现

如何..吗

在你的适配器中创建一个接口,如下所示。

public interface OnAdapterItemClickListener {
    public void onClickImage(String value, int position);
    public void onViewClicked(String value, int position);
}

在您的活动中实施此界面。

然后在适配器构造函数中添加此侦听器,如下所示,

public StringList(this, ArrayList<String> strings, OnAdapterItemClickListener onAdapterItemClickListener) {
    mOnAdapterItemClickListener = onAdapterItemClickListener;

    //.. Your code here
}

getView中添加点击监听器,如下所示

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // Your code here

    final String value = mStrings.get(position);

    imageView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            mOnAdapterItemClickListener.onImageClicked(value, position);
        }
    });

    convertView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            mOnAdapterItemClickListener.onViewClicked(value, position);
        }
    });

    return convertView;
}

最后,在你的活动中,你将获得这样的回调,你已经实现了。

@Override
public void onImageClicked(String value, int position) {
    // Your actions for Image
}

@Override
public void onViewClicked(String value, int position) {
  // Your actions for view click
}

希望这会对你有所帮助。如果您有任何疑问,请告诉我。

答案 1 :(得分:0)

在适配器内部,getView为图像视图实现OnClickListener

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if(convertView == null){
        convertView = inflater.inflate(R.layout.list_row, parent, false);
    }

    ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView);

    imageView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
           // do your staff
        }
    });

    return convertView;
}

答案 2 :(得分:0)

在适配器类中设置ImageView final并将onclicklistner设置为该imageview。像

final ImageView imageView = (ImageView) convertView.findViewById(R.id.imageViewId);

imageView.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View view) {
       //your code
    }
});

答案 3 :(得分:0)

您必须将事件jst点击到ImageView;

 ImageView iv= (ImageView) convertView.findViewById(R.id.iv);

iv.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View view) {
       // do your operation

    }
});

您也可以像下面的代码一样使用listview.setOnItemClickListener;

 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long viewId) {

            ImageView iv= (ImageView) view.findViewById(R.id.iv);

          iv.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View view) {
   // do your operation

  }
 });


 }
  });

答案 4 :(得分:0)

假设StringList是您自己的适配器实现,只需添加侦听器方法即可为图像注册自定义OnImageClickListener

public class ListenableStringList extends StringList {

    private final int imageId;

    // if listener is not registered via setOnImageClickListener, use do-nothing implementation...
    private OnImageClickListener imageClickListener = new OnImageClickListener() {
        @Override public void onImageClick(ImageView v, int position) {/*do-nothing*/}
    };

    public ListenableStringList(..., int imageId) {
        super(...);
        this.imageId = imageId;
    }        

    public void setOnImageClickListener(OnClickListener imageClickListener) {
        this.imageClickListener  = imageClickListener;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);
        final ImageView image = (ImageView) view.findViewById(imageId);
        image.setOnClickListener(new OnClickListener() {
            @Override void onClick(View v) {
                // delegate event to your custom listener
                imageClickListener.onImageClick(image, position);
            }
        });
        return view;
    }

    public interface OnImageClickListener {
         void onImageClick(ImageView v, int position);
    }
}

在您的活动/片段中:

ListView listView = (ListView) findViewById(R.id.listView);
ListenableStringList adapter = new ListenableStringList(this, strings, R.id.image);
adapter.setOnImageClickListener(new OnImageClickListener() {
    @Override
    public void onImageClick(ImageView v, int position) {
        // handle click event...
    }
});
listView.setEmptyView(findViewById(R.id.empty));
listView.setAdapter(adapter);

答案 5 :(得分:0)

好吧我没有尝试过arayadapter但是,这就是我在recyclerview中所做的,你可能会有一个想法

1 I created an interface like below

public interface Yourinterface {
public void yourmethodname(int position, int id, String anyvalue);
}


2 ,in onBindViewHolder (inside Recycler adapter)

Yourinterface yourinterface;

@Override
public void onBindViewHolder(final Viewholder holder, final int position) {

    final Group group = GroupList.get(position);


        holder.imgview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                yourinterface.yourmethodname(position, data.get_id(), data.getdataName());
            }
        });


}


3, At last in Activity

class extends Activity{

Yourinterface yourinterface;

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.drawer_activity_addmember_list);

    Adapter adapter=new Addmember_group_list_adapter(this,yourinterface,GroupList);
    GrpList_recyclerview.setAdapter(adapter);
    adapter.notifyDataSetChanged();

    yourinterface=new Yourinterface() {

        @Override
        public void youmethod(int position, int id, String value) {
         // DO ANYTHING HERE    
        }
    };

}