I'm working on a list of news items. My list is an Activity
that extends ListActivity
. I've also created an ArrayAdapter
that I use to inflate the elements of my list. The ArrayAdapter
uses a simple layout with an ImageView
and two TextView
s. Everything works but when I override the onListItemClick()
method of the and try to resize the list elements when I click on them nothing happens.
protected void onListItemClick(ListView l, View v, int position, long id) {
v.getLayoutParams().height=300;
}
I've tried the above with LayoutParams
as well
Here is the layout of each row that I use to populate my list:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:background="@color/white"
>
<ImageView
android:id="@+id/icon"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="10dp"
android:layout_marginTop="4dp"
android:src="@drawable/ic_launcher" >
</ImageView>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/title1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+id/label"
android:textColor="@color/black"
android:textColorHint="@color/black"
android:textSize="10dp"
android:textStyle="bold" >
</TextView>
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+id/label"
android:textColor="@color/black"
android:textColorHint="@color/black"
android:textSize="10dp" >
</TextView>
</LinearLayout>
</LinearLayout>
Has anyone faced the same issue? Can anyone cast some light for a beginner in Android?
I finally found a solution to my problem and I would like to share it with the rest of the community.
I'm using an arrayAdapter to inflate my rowlayout following is it's code:
public class MySimpleArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
private final Bitmap[] images;
private final String[] names;
public MySimpleArrayAdapter(Context context, String[] values, Bitmap[] images, String [] names) {
super(context, R.layout.rowlayout, values);
this.context = context;
this.values = values;
this.images=images;
this.names=names;
}
View rowView;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//View rowView;
rowView = inflater.inflate(R.layout.rowlayout, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
TextView title=(TextView) rowView.findViewById(R.id.title1);
textView.setText(values[position]);
imageView.setImageBitmap(images[position]);
title.setText(names[position]);
return rowView;
}
}
My main activity extends ListActivity where the items for the list are fetched from some json files in an asynctask. In the onPostExecute method of AsyncTask my list is created with the fetched items.
Following is the code for the onListItemClick method:
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
int hei=v.getLayoutParams().height;
System.out.println("Height "+hei);
if (hei==LayoutParams.WRAP_CONTENT) {
v.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,105));
v.requestLayout();
}
else {
v.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
v.requestLayout();
}
}
The trick is to call the requestLayout method so that your layout is refreshed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With