カスタム ListView を記述する必要はありません。パーソナライズされたレイアウトとカスタム アダプターを使用する必要があります。
まず、各行の外観を定義するレイアウトを記述します。基本的な例を次に示します。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp" />
<TextView
android:id="@+id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp" />
<TextView
android:id="@+id/link"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp" />
</LinearLayout>
(名前を付けてフォルダーに保存しlist_item.xml
ますres/layout
。)
次に、レイアウトを効率的に表示するカスタム アダプターを作成することをお勧めします。
public class ItemAdapter extends BaseAdapter {
private LayoutInflater inflater;
private List<Item> objects;
public ItemAdapter(Context context, List<Item> objects) {
this.objects = objects;
inflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null) {
convertView = inflater.inflate(R.layout.list_item, parent, false);
holder = new ViewHolder();
holder.title = (TextView) convertView.findViewById(R.id.title);
// Do the same for description and link
convertView.setTag(holder);
}
else
holder = (ViewHolder) convertView.getTag();
Item item = objects.get(position);
holder.title.setText(item.getTitle());
// Same for description and link
return convertView;
}
// Override the other required methods for BaseAdapter
public class ViewHolder {
TextView title;
TextView description;
TextView link;
}
}
カスタム アダプター、ViewHolder、および効率性について詳しくは、Android の Romain Guyによるこのテーマに関する講演をご覧ください。
それが役立つことを願っています!