У меня возникла некоторая проблема для рендеринга ListView из динамического макета. Не знаю, почему getView
вызывается только с позицией 0 и несколько раз!Адаптер getView вызывается несколько раз с положением 0
Я искал через Интернет и stackoverflow, но не могу найти подходящий ответ.
Я на самом деле пытается сделать демо это: http://www.framentos.com/en/android-tutorial/2012/07/16/listview-in-android-using-custom-listadapter-and-viewcache/
Примечательно, что мой основной файл макета окружен скроллинга.
основной файл макета деятельности:
<ListView
android:id="@+id/city_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/questionsList"
android:paddingTop="20sp" >
</ListView>
Мой файл макета для просмотра списка:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="80dip" >
<ImageView
android:id="@+id/ImageCity"
android:layout_width="90sp"
android:layout_height="90sp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_toRightOf="@id/ImageCity"
android:orientation="vertical"
android:paddingLeft="10sp">
<TextView
android:id="@+id/cityName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="25sp" />
<TextView
android:id="@+id/cityLinkWiki"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:autoLink="web"
android:textSize="15sp" />
</LinearLayout>
</RelativeLayout>
класс Adapter:
import com.incidentreport.app.classes.objects.City;
public class CityListAdapter extends ArrayAdapter{
private int resource;
private LayoutInflater inflater;
private Context context;
public CityListAdapter (Context ctx, int resourceId, List objects) {
super(ctx, resourceId, objects);
resource = resourceId;
inflater = LayoutInflater.from(ctx);
context=ctx;
}
@Override
public View getView (int position, View convertView, ViewGroup parent) {
Log.v("adapter", "pos: " + position + "#" + resource);
/* create a new view of my layout and inflate it in the row */
convertView = (RelativeLayout) inflater.inflate(resource, null);
/* Extract the city's object to show */
City city = (City)getItem(position);
/* Take the TextView from layout and set the city's name */
TextView txtName = (TextView) convertView.findViewById(R.id.cityName);
txtName.setText(city.getName());
/* Take the TextView from layout and set the city's wiki link */
TextView txtWiki = (TextView) convertView.findViewById(R.id.cityLinkWiki);
txtWiki.setText(city.getUrlWiki());
/* Take the ImageView from layout and set the city's image */
ImageView imageCity = (ImageView) convertView.findViewById(R.id.ImageCity);
return convertView;
}
}
основной код деятельности snipps:
List listCity= new ArrayList();
listCity.add(new City("London","http://en.wikipedia.org/wiki/London","london"));
listCity.add(new City("Rome","http://en.wikipedia.org/wiki/Rome","rome"));
listCity.add(new City("Paris","http://en.wikipedia.org/wiki/Paris","paris"));
ListView listViewCity = (ListView) findViewById(R.id.city_list);
listViewCity.setAdapter(new CityListAdapter(this, R.layout.layout_city, listCity ));
Используйте шаблон ViewHolder. используйте 'inflater.inflate (ресурс, родительский, false);'. – Raghunandan
«Примечательно, что мой основной файл макета окружен полосой прокрутки». ? Что вы имеете в виду? Затем покажите это. – greenapps
do read http://www.doubleencore.com/2013/05/layout-inflation-as-intended/ – Raghunandan