יצירת רשימה דינאמית (Collapseable) בלי לשבור את הראש

פורום ריכוז המדריכים למפתחים חדשים או מנוסים.

מנהל: מנהלי פורומים

יצירת רשימה דינאמית (Collapseable) בלי לשבור את הראש

הודעהעל ידי royiby ב ב', 26 אפריל 2010, 08:43

לאפליקציה אחת הייתי צריך ליצור רשימה דינאמית והמימוש הרווח ברשת של ExpandableList הינו מסובך בצורה מזעזעת ולכן, למי שאין לו את הזמן\כח\נסיון לשבור את הראש - אני מביא לכם מימוש פשוט במיוחד ועובד ללא תקלות.

נ.ב. - הקבוע LISLEN הינו מספר הכפתורים ברשימה - להקצאה דינאמית-> פעם אחרת.
קוד: בחר הכל
public class CList extends Activity
{
   public final int LISLEN = 3;
   
   public FrameLayout container;
   FrameLayout.LayoutParams parpar[];
   public Button[] butt;
   public boolean[] is_col;
   
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
       
        container = new FrameLayout(this);
        parpar = new FrameLayout.LayoutParams[LISLEN];
        butt = new Button[LISLEN];
        is_col = new boolean[LISLEN];
       
        setContentView(R.layout.main);
       
        container = (FrameLayout)findViewById(R.id.con);
       
        int top = 0;
        for(int i=0;i<LISLEN;i++)
        {
           is_col[i] = true;
           butt[i] = new Button(this);
           
           String msg = Integer.toString(i) + "=>" + Boolean.toString(is_col[i]);
           butt[i].setText(msg);
           butt[i].setOnClickListener(clickt);
           butt[i].setId(i);
           
           parpar[i] = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT, Gravity.TOP);
           top += 45;
           parpar[i].topMargin = top;
           butt[i].setLayoutParams(parpar[i]);
           container.addView(butt[i], parpar[i]);
        }//for
    }//onCreate
   
    OnClickListener clickt = new OnClickListener()
    {
      @Override
      public void onClick(View v)
      {
         int vid = v.getId();
         int top = 0;
         
         if(vid+1 < LISLEN)
         {
            if(is_col[vid] == true)
            {
               is_col[vid] = false;
               String msg = Integer.toString(vid) + "=>" + Boolean.toString(is_col[vid]);
                 butt[vid].setText(msg);
               top = parpar[vid+1].topMargin;
               for(int i=vid+1; i<LISLEN; i++)
               {
                  if(is_col[i] == false)
                  {
                     top += 45;
                       parpar[i].topMargin = top;
                       butt[i].setLayoutParams(parpar[i]);
                       top += 45;
                  }
                  else
                  {
                     top += 45;
                       parpar[i].topMargin = top;
                       butt[i].setLayoutParams(parpar[i]);
                  }
               }//if for
            }//if
            else
            {
               is_col[vid] = true;
               String msg = Integer.toString(vid) + "=>" + Boolean.toString(is_col[vid]);
                 butt[vid].setText(msg);
               top = parpar[vid].topMargin;
               for(int i=vid+1; i<LISLEN; i++)
               {
                  if(is_col[i] == false)
                  {
                     top += 45;
                       parpar[i].topMargin = top;
                       butt[i].setLayoutParams(parpar[i]);
                       top += 45;
                  }
                  else
                  {
                     top += 45;
                       parpar[i].topMargin = top;
                       butt[i].setLayoutParams(parpar[i]);
                  }
               }//else for
            }//else
         }//outer if
      }//onClick
    };//Clickt
    }

קוד: בחר הכל
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:id="@+id/con" android:layout_gravity="top">
</FrameLayout>


מקווה שזה יעזור למישהו.
רועי בן יוסף Royi benyossef
מפתח אנדרואיד Android dev
מוביל טכני אנדרואיד - חברת וידמיינד
Android tech leader at vidmind
http://vidmind.com
סמל אישי של המשתמש
royiby
 
הודעות: 488
הצטרף: א', 25 אפריל 2010, 09:59
מיקום: נ"צ-ראשל"צ

מכשיר: Nexus One
גירסא: 2.3

Re: יצירת רשימה דינאמית (Collapseable) בלי לשבור את הראש

הודעהעל ידי yosi199 ב ה', 22 דצמבר 2011, 06:49

היי מה קורה?

אני כבר כמה ימים מחפש ברשת פיסת מידע שתסביר לי בצורה פשוטה איך להשתמש בExpandableListView כדי ליצור רשימות "מתרחבות", הגעתי לפה במיקרה אבל אני בהחלט יעיף מבט בדוגמא שלך. אשמח אם תהיה פה זמין לענות לי על שאלות בנושא, אני יודע שיהיו לי - SimpleExpandableListAdapter זה חומר בכלל לא פשוט לי כרגע... או שסתם לא נתקלתי בחומר כתוב בצורה ברורה מספיק
yosi199
 
הודעות: 13
הצטרף: ה', 22 דצמבר 2011, 06:45

מכשיר: Asus Transformer-Eee Pad
גירסא:


חזור אל ריכוז מדריכי פיתוח