a very simple implementation of collapsible list

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

a very simple implementation of collapsible list

Postby royiby » Mon Apr 26, 2010 7:51 am

When i needed to insert a collapsible list to an app i came across with the complexity of the ExpandableList implementation(the only one one the web apparently).
so, for those of you who don't have the patience\experience i bring to you - collapsible list for dummies!


[syntax="java"]
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
}

[syntax="xml"]
<?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>

hope this helps someone.

btw LISLEN - is the number of buttons in the list, for dynamic allocation->next time

Royi benyossef
Android dev
www.linnovate.net
Attachments
CList.rar
(27.68 KiB) Downloaded 139 times
royiby
Freshman
Freshman
 
Posts: 5
Joined: Sun Apr 25, 2010 8:33 am
Location: israel

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 7 guests