Zhao Yazhi _androidui_android Folding telescopic imitation accordion effect in the ListView (dynamic)

Source: Internet
Author: User

Before writing a static data ListView display Accordion effect, today's blog is written to dynamically implement the accordion effect of the ListView

Static Link: ListView static accordion Effect

Data is stored in the database, and if you want to get the data, query the database

and realized: Click to see the name of the business card you want to see, the last open can be closed

The core code is as follows:

Package Com.cards.activity;import Java.util.list;import Android.app.activity;import android.app.alertdialog;import Android.app.alertdialog.builder;import Android.content.context;import Android.content.dialoginterface;import Android.content.intent;import Android.net.uri;import Android.os.bundle;import Android.view.LayoutInflater;import Android.view.view;import Android.view.viewgroup;import Android.view.view.onclicklistener;import Android.widget.adapterview;import Android.widget.adapterview.onitemclicklistener;import Android.widget.baseadapter;import Android.widget.button;import Android.widget.imageview;import Android.widget.linearlayout;import Android.widget.listview;import Android.widget.relativelayout;import Android.widget.textview;import Com.cards.r;import Com.cards.commom.common;import Com.cards.commom.MyApp;import Com.cards.dao.carddao;import Com.cards.dao.carddaoimp;import Com.cards.entity.card;public class CardsListAct Extends Activity implements Onitemclicklistener {private FlexListAdapter adapter;private list<card> list;private boolean[] iscurrentitems;private ListView lv_cards;private Button btnregistered;private TextView tvtitle;protected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.act_cardlist); lv_cards = (ListView) Findviewbyid (r.id.list_cards); lv_ Cards.setselector (r.color.transparent);//Initialize Data init ();} /** * * @explaination initialization data * @author Yazhizhao * @time 2014-7-23 a.m. 11:30:12 */private void Init () {//Find database Data Carddao card Dao = new Carddaoimp (this); list = Carddao.findall ();//Whether the list set is opened Iscurrentitems = new Boolean[list.size ()];// When I first entered, all entries show a closed state for (int i = 0; i < iscurrentitems.length; i++) {iscurrentitems[i] = false;} Set adapteradapter = new Flexlistadapter (); Lv_cards.setadapter (adapter); Lv_cards.setonitemclicklistener (this);} @Overridepublic void Onitemclick (adapterview<?> parent, view view, int Position,long ID) {/* * open only one */for (int i = 0; i < iscurrentitems.length; i++) {iF (i! = position) {Iscurrentitems[i] = false;}} Open or close iscurrentitems[position] =!iscurrentitems[position];//Instant refresh adapter.notifydatasetchanged ();} /** * * @explaination Custom Adapter * @author Yazhizhao * @time 2014-7-23 morning 11:33:39 */class Flexlistadapter extends Baseadap ter {public int getcount () {return list.size ();} Public Object getItem (int pos) {return pos;} public long Getitemid (int pos) {return pos;} Public View getView (int pos, view V, ViewGroup p) {flexlinearlayout view = null;if (null = = v) {view = new flexlinearlayou T (Cardslistact.this, List.get (POS), POS, false);} else {view = (flexlinearlayout) v;view.setworktitlelayout (List.get (POS), POS, Iscurrentitems[pos]);} return view;}} /** * * @explaination Custom Linear layout * @author Yazhizhao * @time 2014-7-23 a.m. 11:25:07 */public class Flexlinearlayout extends Lin Earlayout {public static final int BULE = 0xff3d8cb8;private linearlayout layout;private relativelayout rlname;private Rel Ativelayout rlcards;private TextView tvcardsphonenuminfo;privateTextView tvcardsaddressinfo;private TextView tvcardsmailinfo;private TextView tvcardsotherinfo;private TextView Tvcardsname;private TextView tvcardname;private ImageView imgcardsdel;private layoutinflater minflater;/** *            Create a linearlayout * * @param context * Contextual Object * @param card * Content Details * @param position * The location of the list where the column is located * @param iscurrentitem * is stretched */public flexlinearlayout (final context context, final Card card,f inal int position, Boolean Iscurrentitem) {super (context); minflater = (layoutinflater) context.getsystemservice ( Context.layout_inflater_service); LAYOUT = (linearlayout) minflater.inflate (r.layout.act_cards_list,null); rlName = ( Relativelayout) Layout.findviewbyid (r.id.rl_name); rlcards = (relativelayout) Layout.findviewbyid (R.id.rl_cards); Tvcardsphonenuminfo = (TextView) Layout.findviewbyid (r.id.tv_mc_phonenum); tvcardsname = (TextView) Layout.findviewbyid (r.id.tv_cards_name); tvcardname = (TextView) Layout.findviewbyid (r.id.tv_card_name); tvcardsaddressinfo = (TextView) Layout.findviewbyid (r.id.tv_mc_addressinfo); tvcardsmailinfo = (TextView) Layout.findviewbyid (r.id.tv_mc_mail); tvcardsotherinfo = (TextView) Layout.findviewbyid (r.id.tv_mc_otherinfo); Imgcardsdel = (ImageView) Layout.findviewbyid (R.id.iv_cards_del); Tvcardsphonenuminfo.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {String phone_num = Tvcardsphonenuminfo.gettext (). toString (). Trim (); Intent Intent = new Intent (Intent.action_call, Uri.parse ("Tel:" + phone_num)); startactivity (Intent);}); Imgcardsdel.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {Builder builder = new Alertdialog.builder (Cardslistact.this); Builder.setmessage ("OK to delete this business card?" "). Setcancelable (False). Setpositivebutton (" Ack ", new Dialoginterface.onclicklistener () {public void OnClick ( Dialoginterface dialog, int id) {Carddao Carddao = new Carddaoimp (context); Carddao.delete (Card.getid ()); init ();}}). Setnegativebutton ("Cancel", new DIALOGINTERFACE.onclicklistener () {public void OnClick (Dialoginterface dialog, int id) {dialog.cancel ();}}); Builder.create (). Show ();}); This.addview (layout); Setworktitlelayout (card, position, Iscurrentitem);}            /** * Set the status and style of the column * * @param card * Content Details * @param position * location of the list of the column * @param iscurrentitem * Whether to stretch */public void Setworktitlelayout (final card, final int position,boolean iscurrentitem) {rlname.setbackgr Oundresource (r.drawable.title_2); Tvcardsname.settext (Card.gettruename ()); Tvcardsname.settextcolor (BULE); if ( Iscurrentitem) {Tvcardname.settext (Card.gettruename ()); Tvcardsotherinfo.settext (Card.getmeno ()); Tvcardsphonenuminfo.settext ("Tel:" + card.getmobile1 ()); Tvcardsmailinfo.settext ("Email:" + card.getemail ()); Tvcardsaddressinfo.settext ("Address:" + card.getaddress ());} Rlcards.setvisibility (Iscurrentitem? Visible:gone);}} @Overrideprotected void OnStart () {Super.onstart (); init ();//Set Adaptermyapp.getinstance () for the linear layout. Addactivity (this);} @Overrideprotected void OndesTroy () {common.destroyloading (cardslistact.this); finish (); Super.ondestroy ();}} 


Knowledge Point Analysis:

Set adapter to be placed in Init, when the data is deleted, it is not error to call init.

If you just put it in OnCreate, you'll get an error when you delete the data and refresh it.

Set adapteradapter = new Flexlistadapter (); Lv_cards.setadapter (adapter); Lv_cards.setonitemclicklistener (this);


This code is the control to open only one item when opening one item and other close

for (int i = 0; i < iscurrentitems.length; i++) {if (i! = position) {Iscurrentitems[i] = false;}}


To determine whether to open or close the optimization code:


if (Iscurrentitems[position]) {              iscurrentitems[position] = false;          } else {              iscurrentitems[position] = True ;          }  
Optimized for
Iscurrentitems[position] =!iscurrentitems[position];






Zhao Yazhi _androidui_android Folding telescopic imitation accordion effect in the ListView (dynamic)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.