I have created a simple list view with two different text views (shown below). I want to have a series of rows that will display more information when clicked--one text view hidden that can be toggled by clicking the other text view. I think my problem might be the linear layout encapsulating the two text views, but I'm not sure. The slide_up and slide_down animations were basic ones taken from a website.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:textSize="20sp"
android:clickable="true"
android:onClick="toggle_contents"/>
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="example"
android:textSize="20sp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"/>
</LinearLayout>
The list view is populated from a content provider as follows:
cursor.moveToFirst();
ArrayList<String> listItems = new ArrayList<String>();
ArrayList<String> listItems2 = new ArrayList<String>();
// listItems = new ArrayList<ListItem>();
ListView mylist=(ListView) findViewById(R.id.list);
do{
String id=cursor.getString(cursor.getColumnIndex("id"));
String name=cursor.getString(cursor.getColumnIndex("name"));
// String name_str = id +" : "+ name;
listItems2.add(id);
listItems.add(name);
cursor.moveToNext();
}while(!cursor.isAfterLast());
CustomListAdapter adapter=new CustomListAdapter(this, listItems2, listItems);
mylist.setAdapter(adapter);
I want to toggle between visible and invisible with the slide up and slide down animations:
public void toggleContents(View v){
if(txt_hidden.isShown()){
slide_up(this, txt_hidden);
txt_hidden.setVisibility(View.GONE);
}
else{
txt_hidden.setVisibility(View.VISIBLE);
slide_down(this, txt_hidden);
}
//txt_hidden.setVisibility(txt_hidden.isShown()? View.GONE : View.VISIBLE);
}
@Override
public void onDestroy() {
super.onDestroy();
}
public static void slide_down(Context ctx, View v){
Animation a = AnimationUtils.loadAnimation(ctx, R.anim.slide_down);
if(a != null){
a.reset();
if(v != null){
v.clearAnimation();
v.startAnimation(a);
}
}
}
public static void slide_up(Context ctx, View v){
Animation a = AnimationUtils.loadAnimation(ctx, R.anim.slide_up);
if(a != null){
a.reset();
if(v != null){
v.clearAnimation();
v.startAnimation(a);
}
}
}
How do you set the txt_hidden
variable? I believe it might be pointing to a view that is different than the one you are actually trying to change the visibility for.
I would suggest that you don't set the click listener in the xml file, but rather in getView() of your adapter when you are building your list item view:
private class ViewHolder
{
TextView mTextView1;
TextView mTextView2
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.list_view_item_1, parent, false);
viewHolder = new ViewHolder();
viewHolder.mTextView1 = (TextView)convertView.findViewById(R.id.text1);
viewHolder.mTextView2 = (TextView)convertView.findViewById(R.id.text2);
viewHolder.mTextView1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// do your animation here
}
});
convertView.setTag(viewHolder);
}
else
{
viewHolder = (ViewHolder)convertView.getTag();
}
viewHolder.mTextView1.setText(mElements1.get(position));
viewHolder.mTextView2.setText(mElements2.get(position));
return convertView;
}