android - How to make ListView items expand on click -


right have listview, custom arrayadapter populate items. right items each show little bit of information, want able tap on item, , have 'expand', showing rest of information. there animation when expands. want there 1 item 'expanded' @ once (ie if 1 open, , tap on another, first 1 goes normal). have expanded items stay expanded, if scrolled out of view.

this code have now

single_row.xml

<?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="match_parent">     <textview         android:layout_width="wrap_content"         android:layout_height="wrap_content"          android:text="first info"         android:id="@+id/first"/>       <space         android:layout_width="match_parent"         android:layout_height="15dp"         android:id="@+id/space"/>      <textview         android:layout_width="wrap_content"         android:layout_height="wrap_content"          android:text="second info"         android:id="@+id/hidden"/>  </relativelayout> 

my_cards.xml

<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"               android:orientation="vertical"               android:layout_width="match_parent"               android:layout_height="match_parent"               android:background="#191919"                android:id="@+id/card_layout"               android:weightsum="1">       <space         android:layout_width="match_parent"         android:layout_height="29dp"         android:layout_gravity="center_horizontal"/>      <listview         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:overscrollmode="never"         android:id="@+id/list_view"          android:divider="#191919"         android:layout_marginright="23dp"         android:layout_marginleft="23dp"         android:dividerheight="12dp"         android:layout_gravity="center_horizontal"/> </linearlayout> 

mycards.java

public class mycards extends android.support.v4.app.fragment{      private listview mlistview;      @override     public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) {          //inflate view         view view = inflater.inflate(r.layout.my_cards, container, false);          //arrays test information         string[] names = {"tim", "kevin", "bob", "john","evan", "roy", "tristan", "ronald"};         string[] company = {"apple", "microsoft", "china", "xiaomi","facebook", "unemployed", "ccp", "government"};          //get reference listview         mlistview = (listview) view.findviewbyid( r.id.list_view );          //initialize adapter, , set listview         customadapter adapter = new customadapter(getcontext(),names,company);         mlistview.setadapter(adapter);          return view;     } } class customadapter extends arrayadapter<string>{     context mcontext;     string[] mname;     string[] mcompany;      customadapter(context c, string[] names, string[] companies){         super(c, r.layout.single_row, r.id.name, names);         this.mcontext = c;         this.mname = names;         this.mcompany = companies;     }        @override     public view getview(int position, view convertview, viewgroup parent) {          layoutinflater inflater = (layoutinflater) mcontext.getsystemservice(context.layout_inflater_service);          view mrow = inflater.inflate(r.layout.single_row, parent, false);          textview mnametext = (textview) mrow.findviewbyid(r.id.name);         textview mcompanytext = (textview) mrow.findviewbyid(r.id.company);          mnametext.settext(mname[position]);         mcompanytext.settext(mcompany[position]);          mrow.setbackgroundresource(r.drawable.custom_listview_shape);            return mrow;     } } 

now, have tried have go @ getting work. tried adding oncreateview method

  mlistview.setonitemclicklistener(new adapterview.onitemclicklistener() {             @override             public void onitemclick(adapterview<?> parent, view view, int position, long id) {                 textview hidden = (textview) view.findviewbyid(r.id.hidden);                   if ( hidden.getvisibility() == view.gone)                 {                     //expandedchildlist.set(arg2, true);                     hidden.setvisibility(view.visible);                 }                 else                 {                     //expandedchildlist.set(arg2, false);                     hidden.setvisibility(view.gone);                 }             }         }); 

now, while technically want (cause item expand on click), doesn't way to. there no animation, multiple items can expanded @ once, , goes normal scrolled out of view.

any appreciated.


Comments