I'm reading this tutorial http://android.amberfog.com/?p=296 . I'd like to create a Listview weith different types of rows. I understand how to create the adapter, but what about the xml layout? So I definire an xml layout like this one:
<ListView/>
<TextView android:id="@+id/id1" />
<TextView android:id="@+id/id2" />
<ImageView android:id="@+id/id3" />
<TextView android:id="@+id/id4" />
Will it be a problem (for performance) if maybe one row uses just some elements (only some textviews) of the layout and another row maybe uses other elements? I don't understand if mine is the right way to define the xml or if I have to create different layout for each type of row.
Thank you in advance
EDIT: now I'm having a null point exception.
java code from the adapter:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
int type = getItemViewType(position);
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.listview_main, null);
holder.textView_title = (TextView)convertView.findViewById(R.id.listview1);
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}
**holder.textView_title.setText("aaaa");** //NULL POINT EXCEPTION HERE
return convertView;
}
class ViewHolder {
public TextView textView_title;
}
xml 1:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="left"
android:layout_margin="0dp">
<!-- android:background="#0094ff" -->
<ListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fastScrollEnabled="true"
android:scrollbarStyle="insideInset"
android:textFilterEnabled="false"
android:divider="@null"
android:layout_margin="0dp"
android:paddingTop="0dp"
android:paddingBottom="0dp"
android:paddingLeft="15dp"
android:paddingRight="22dp"/>
</LinearLayout>
xml2
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="left"
android:layout_margin="0dp">
<TextView
android:id="@+id/listview1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="7dp"
android:paddingBottom="0dp"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:textSize="18sp"
android:textColor="#000000"
android:lines="1">
</TextView>
</LinearLayout>
You need to override getViewItemType
and getViewTypeCount
. You will also need to have custom layouts.
getItemViewType(int position)
- returns information which layout type you should use based on position.
You should have a look at the video in the link.
http://www.youtube.com/watch?v=wDBM6wVEO70
private static final int TYPE_ITEM1 = 0;
private static final int TYPE_ITEM2 = 1;
private static final int TYPE_ITEM3 = 2;
Then
int type;
@Override
public int getItemViewType(int position) {
if (position== 0){
type = TYPE_ITEM1;
} else if (position == 1){
type = TYPE_ITEM2;
}
else
{
type= TYPE_ITEM3 ;
}
return type;
}
@Override
public int getViewTypeCount() {
return 3;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
LayoutInflater inflater = null;
int type = getItemViewType(position);
// instead of if else you can use a case
if (row == null) {
if (type == FIRST_TYPE) {
//infalte layout of type1
}
if (type == SECOND_TYPE) {
//infalte layout of type2
} else {
//infalte layout of normaltype
}
}
@Override
public int getItemViewType(int position) {
if (position % 4 == 0) {
return 0;
} else if (position % 4 == 1) {
return 1;
} else if (position % 4 == 2) {
return 2;
}
return 3;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
int type = getItemViewType(position);
if (convertView == null) {
switch (type) {
case 0: {
convertView = mInflater.inflate(R.layout.element1, null);
break;
}
case 1: {
convertView = mInflater.inflate(R.layout.element2, null);
break;
}
case 2: {
convertView = mInflater.inflate(R.layout.element3, null);
break;
}
case 3: {
convertView = mInflater.inflate(R.layout.element4, null);
break;
}
}
holder = new ViewHolder();
holder.txt1 = (TextView) convertView.findViewById(R.id.text1);
holder.txt2 = (TextView) convertView.findViewById(R.id.text2);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
String rowItem = null;
rowItem = rowItems[position];
holder.txt1.setText(rowItem);
rowItem = rowItems[position+1];
holder.txt1.setText(rowItem);
return convertView;
}
private class ViewHolder {
TextView txt1, txt2;
}
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