android - When I Change visibility of a linear layout and add to list all past items visibility will be changed -
i have android program simulate chat. use list , items of list in xml
file named activity_chat_conversation
in layout have linear layout in each of them have edittext
, imageview
.
code:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/llmain" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:ignore="usecompounddrawables" > <linearlayout android:id="@+id/llfrom" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingtop="7dp" android:visibility="gone" > <imageview android:id="@+id/imgchatconversationfrom" android:layout_width="32dp" android:layout_height="32dp" android:background="@color/gray" android:contentdescription="@string/user_image" android:scaletype="fitcenter" android:src="@drawable/ic_user_image" /> <textview android:id="@+id/txtchatconversationfrom" android:layout_width="0dp" android:layout_height="45dp" android:layout_weight="1" android:background="@drawable/from" android:gravity="right|top" android:layoutdirection="rtl" android:paddingleft="45dp" android:paddingright="10dp" android:text="@string/user_name" android:textappearance="?android:attr/textappearancesmall" android:textdirection="rtl" /> </linearlayout> <linearlayout android:id="@+id/llto" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingtop="7dp" android:visibility="gone" > <textview android:id="@+id/txtchatconversationto" android:layout_width="0dp" android:layout_height="45dp" android:layout_weight="1" android:background="@drawable/to" android:gravity="right|top" android:layoutdirection="rtl" android:paddingright="48dp" android:paddingtop="5dp" android:text="@string/user_name" android:textappearance="?android:attr/textappearancesmall" android:textdirection="rtl" /> <imageview android:id="@+id/imgchatconversationto" android:layout_width="32dp" android:layout_height="32dp" android:layout_gravity="bottom" android:background="@color/gray" android:contentdescription="@string/user_image" android:scaletype="fitcenter" android:src="@drawable/ic_user_image" /> </linearlayout>
for filling list create adapter , in set linear layout should shown.
code:
globalvars.barconversation = new baseadapter() { int searchuser = globalvars.findconversationbyusername(friendid); @override public view getview(int position, view convertview, viewgroup parent) { view view = convertview; if(convertview == null) { view = getlayoutinflater().inflate(r.layout.activity_chat_conversation, null); } linearlayout llfrom = (linearlayout)view.findviewbyid(r.id.llfrom); linearlayout llto = (linearlayout)view.findviewbyid(r.id.llto); textview textviewto = (textview)view.findviewbyid(r.id.txtchatconversationto); imageview imageviewto = (imageview)view.findviewbyid(r.id.imgchatconversationto); textview textviewfrom = (textview)view.findviewbyid(r.id.txtchatconversationfrom); imageview imageviewfrom = (imageview)view.findviewbyid(r.id.imgchatconversationfrom); string pmuser = friendid; if (searchuser > -1) { if (globalvars.conversations.get(searchuser). getconversation().get(position).getsender()) { pmuser = currentuser; if (globalvars.userimage != null) { imageviewto.setimagebitmap(globalvars.userimage); } }else { try{ if(globalvars.friends.get(friendint).getimage() != null) { imageviewfrom.setimagebitmap(globalvars.friends.get(friendint).getimage()); } } catch(exception ex) { log.e("chat friend picture", ex.tostring()); } } if (!globalvars.conversations.get(searchuser). getconversation().get(position).getisread()) { globalvars.conversations.get(searchuser). getconversation().get(position).setisread(true); globalvars.newconversations--; } if(pmuser == currentuser) { llto.setvisibility(0); llfrom.setvisibility(8); textviewfrom.setvisibility(8); imageviewfrom.setvisibility(8); textviewto.settext(globalvars.conversations.get(searchuser). getconversation().get(position).gettext()); textviewto.setheight(30); } else { llfrom.setvisibility(0); llto.setvisibility(8); textviewto.setvisibility(8); imageviewto.setvisibility(8); textviewfrom.settext(globalvars.conversations.get(searchuser). getconversation().get(position).gettext()); } } return view; } @override public long getitemid(int position) { return position; } @override public object getitem(int position) { return position; } @override public int getcount() { if (searchuser > -1) { return globalvars.conversations.get(searchuser).getconversation().size(); } return 0; } };
when add new item list , set visibility previous items visibility changed.
how can solve problem ?
it happens because convertview == getview()
method. need refresh parameters new item. example:
if(pmuser == currentuser) { llto.setvisibility(0); textviewto.setvisibility(0); imageviewto.setvisibility(0); //this llfrom.setvisibility(8); textviewfrom.setvisibility(8); imageviewfrom.setvisibility(8); textviewto.settext(globalvars.conversations.get(searchuser). getconversation().get(position).gettext()); textviewto.setheight(30); } else { llfrom.setvisibility(0); textviewfrom.setvisibility(0); imageviewfrom.setvisibility(0); // llto.setvisibility(8); textviewto.setvisibility(8); imageviewto.setvisibility(8); textviewfrom.settext(globalvars.conversations.get(searchuser). getconversation().get(position).gettext()); }
Comments
Post a Comment