How to make imageView clickable from OnItemClickListener?

半世苍凉 提交于 2019-12-12 06:25:02

问题


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

         // My code is here. I want make my image clickable. I don't want make it clickable in my CustomAdapter getView() method because it doesn't allows me open DialogFragment from there.
         // I already setted in xml of image clickable="true" and focusable = "false"
         //I'll admit for any help and thanks in advance

        }
}

回答1:


If you take image Onclick from adapter,you cannot take setOnItemClickListener from listview event.I suggested you may take Onclick and insteadof setOnItemClickListener take Onclick from adapter.




回答2:


First you have to add OnClickListener for Imageview in Adapter class like

viewHolder.button1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
    ((ListView) parent).performItemClick(v, position, 0); // Let the event be handled in onItemClick()
}

});

Then after you can access ImageView in onItemClick like

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    long viewId = view.getId();


if (viewId == R.id.button1) {
        Toast.makeText(this, "Button 1 clicked", Toast.LENGTH_SHORT).show();
    } else if (viewId == R.id.button2) {
        Toast.makeText(this, "Button 2 clicked", Toast.LENGTH_SHORT).show();
    } else {
        Toast.makeText(this, "ListView clicked" + id, Toast.LENGTH_SHORT).show();
    }
}

Enjoy!...




回答3:


It shouldn't be a problem to show a dialog from the adapter. If your adapter needs something that it doesn't have, then you can add a field for your adapter with some listener like private final OnImageClickListener and create an interface:

public interface OnImageClickListener {
    void onImageClicked(View view, int position, int id);
}

When you create the adapter from your activity or a fragment, you can implement this interface either in your activity/fragment or inside an anonymous class, and in there you have access to the fragment manager and can do whatever you want.



来源:https://stackoverflow.com/questions/40672971/how-to-make-imageview-clickable-from-onitemclicklistener

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!