android - Implement click only on image not on text in custom listview -
i have custom listview
contains image
& text
, implemented onitemclicklistener
list should work image not text. onitemclick
working fine image there fatal exception when click on text. additionally image visible in list if exists else hide.
tried android:focusable="false"
, android:clickable="false"
still getting below exception
java.lang.nullpointerexception: attempt invoke virtual method 'android.graphics.bitmap com.bumptech.glide.load.resource.bitmap.glidebitmapdrawable.getbitmap()' on null object reference
onitemclick:
@override public void onitemclick(adapterview<?> parent, view view, int position, long id) { final imageview imageview1 = (imageview) view.findviewbyid(r.id.imagelist); final glidebitmapdrawable bitmapdrawable = (glidebitmapdrawable) imageview1.getdrawable(); final bitmap yourbitmap = bitmapdrawable.getbitmap(); dialog builder = new dialog(this); builder.requestwindowfeature(window.feature_no_title); builder.getwindow().setbackgrounddrawable( new colordrawable(android.graphics.color.transparent)); builder.setondismisslistener(new dialoginterface.ondismisslistener() { @override public void ondismiss(dialoginterface dialoginterface) { //nothing; } }); imageview imageview = new imageview(this); imageview.setimagebitmap(yourbitmap); builder.addcontentview(imageview, new relativelayout.layoutparams( viewgroup.layoutparams.match_parent, viewgroup.layoutparams.match_parent)); builder.show(); }
how can implement click on image?
in custom layout xml, set
android:descendantfocusability="blocksdescendants"
to root layout.
edit check custom listview.
listitem.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:gravity="center" android:descendantfocusability="blocksdescendants" android:layout_height="60dp"> <imageview android:id="@+id/m_imageview" android:layout_width="0dp" android:layout_weight="1" android:src="@drawable/ic_launcher" android:layout_height="50dp" /> <textview android:id="@+id/m_textview" android:layout_width="0dp" android:layout_weight="3" android:layout_height="50dp" android:gravity="center|center" android:textcolor="#ffffff" android:text="hello"/> <button android:id="@+id/m_buttonview" android:layout_width="0dp" android:layout_weight="2" android:text="ok" android:layout_height="50dp" /> </linearlayout>
activity_main.xml
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <listview android:id="@+id/lv_items" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/list_selector" android:dividerheight="2dp" android:divider="#ffffff" tools:context=".mainactivity" /> </relativelayout>
customlistviewadapter.java
public class customlistviewadapter extends arrayadapter<rowitem> { context context; public customlistviewadapter(context context, int resourceid, list<rowitem> items) { super(context, resourceid, items); this.context = context; } /*private view holder class*/ private class viewholder { imageview m_pic; textview m_title; button m_btn; } public view getview(final int position, view convertview, viewgroup parent) { viewholder holder = null; rowitem rowitem = getitem(position); layoutinflater minflater = (layoutinflater) context.getsystemservice(activity.layout_inflater_service); if (convertview == null) { convertview = minflater.inflate(r.layout.listitem, null); holder = new viewholder(); holder.m_title = (textview) convertview.findviewbyid(r.id.m_textview); holder.m_btn = (button)convertview.findviewbyid(r.id.m_buttonview); holder.m_pic = (imageview) convertview.findviewbyid(r.id.m_imageview); convertview.settag(holder); } else holder = (viewholder) convertview.gettag(); holder.m_title.settext(rowitem.gettxt()); holder.m_pic.setimageresource(rowitem.getimage()); holder.m_btn.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { toast.maketext(context, position + " clicked" , toast.length_long).show(); } }); return convertview; } }
rowitem.java
public class rowitem { private int image; private string txt; public rowitem(int imageview , string textview) { this.image = imageview; this.txt = textview; } public int getimage() { return image; } public string gettxt() { return txt; } public void setimage(int image) { this.image = image; } public void settxt(string txt) { this.txt = txt; } }
mainactivity.java
public class mainactivity extends appcompatactivity implements adapterview.onitemclicklistener { string m_txt[] = {"one" , "two" , "three" , "four", "five"}; int m_img [] = {r.drawable.ic_launcher , r.drawable.ic_launcher , r.drawable.ic_launcher , r.drawable.ic_launcher , r.drawable.ic_launcher}; listview m_list; list<rowitem> rowitems; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); m_list = (listview)findviewbyid(r.id.lv_items); rowitems = new arraylist<rowitem>(); (int = 0; < m_txt.length; i++) { rowitem item = new rowitem(m_img[i],m_txt[i]); rowitems.add(item); } customlistviewadapter adapter = new customlistviewadapter(this, r.layout.listitem, rowitems); m_list.setadapter(adapter); m_list.setonitemclicklistener(mainactivity.this); } @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { toast toast = toast.maketext(getapplicationcontext(), "item " + (position) + ": " + rowitems.get(position).gettxt(), toast.length_short); toast.setgravity(gravity.bottom | gravity.center_horizontal, 0, 0); toast.show(); } }
this should helps you.
happy coding.!!!
Comments
Post a Comment