似乎我和这个问题有同样的问题:here。 但是我的listview已经有了fill_parent的高度。我做了一个调试,它在4行后停止,并且它保持循环相同的。
这是我的代码:
public class NewsAdapter extends BaseAdapter{
List<News> news;
private Context context;
public NewsAdapter(Context context, List<News> news) {
this.context = context;
this.news = news;
}
@Override
public int getCount() {
return news.size();
}
@Override
public Object getItem(int position) {
return news.get(position);
}
@Override
public long getItemId(int arg0) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if(v == null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.adapter_news, null);
ViewHolder holder = new ViewHolder();
holder.tv_title = (TextView) v.findViewById(R.id.tv_adapter_news_title);
holder.tv_description = (TextView) v.findViewById(R.id.tv_adapter_news_description);
holder.tv_date = (TextView) v.findViewById(R.id.tv_adapter_news_date);
//
holder.siv_picture = (SmartImageView) v.findViewById(R.id.siv_adapter_news_picture);
//
holder.view_color = (View) v.findViewById(R.id.v_adapter_news_color);
///
String type = news.get(position).getNewsType();
String title = news.get(position).getNewsTitle();
String description = news.get(position).getNewsDescription();
String picture = news.get(position).getNewsPicture();
String date = news.get(position).getNewsDate();
String link = news.get(position).getNewsLink();
String id = news.get(position).getNewsId();
if(title != null){
holder.tv_title.setText(title);
}
if(description != null){
holder.tv_description.setText(description);
}
if(picture != null){
holder.siv_picture.setImageUrl(picture);
}else{
holder.siv_picture.setImageUrl("http://aovivo.slbenfica.pt/Portals/3/noticias/Epoca2012_13/Futebol%20Profissional/CampeonatoNacional_2012_13/Benfica_fcPorto/SLB_Futebol_Matic_Festejos_Benfica_FCPorto_13Janeiro2013_V.jpg?w=227");
}
if(date != null){
holder.tv_date.setText(date);
}
if(type.equalsIgnoreCase(News.CATEGORY_ARTICLE)){
holder.view_color.setBackgroundColor(context.getResources().getColor(R.color.red));
if(date != null){
holder.tv_date.setTextColor(context.getResources().getColor(R.color.red));
}
}else{
holder.view_color.setBackgroundColor(context.getResources().getColor(R.color.blue));
}
}
System.out.println("getView " + position + " " + convertView);
return v;
}
static class ViewHolder {
public SmartImageView siv_picture;
public TextView tv_title;
public TextView tv_description;
public TextView tv_date;
public View view_color;
}
}
这是xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="100"
>
<!-- Photo part -->
<RelativeLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="30"
android:layout_marginTop="@dimen/dim20dp"
android:layout_marginBottom="@dimen/dim20dp"
>
<View
android:id="@+id/v_adapter_news_color"
android:layout_width="5dp"
android:layout_height="100dp"
android:layout_centerVertical="true"/>
<com.loopj.android.image.SmartImageView
android:id="@+id/siv_adapter_news_picture"
android:layout_width="150dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
android:scaleType="fitCenter"
/>
</RelativeLayout>
<!-- Text Part -->
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="70"
android:orientation="vertical"
android:layout_marginTop="@dimen/dim20dp"
android:layout_marginBottom="@dimen/dim20dp"
>
<TextView
android:id="@+id/tv_adapter_news_date"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="date"
android:layout_marginBottom="@dimen/dim5dp"
/>
<TextView
android:id="@+id/tv_adapter_news_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"
style="@style/news_text_title"
android:layout_marginBottom="@dimen/dim15dp"/>
<TextView
android:id="@+id/tv_adapter_news_description"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="@style/news_text_description"
android:text="description" />
</LinearLayout>
</LinearLayout>
答案 0 :(得分:1)
getView()实现不处理convertView不为null的情况。确保使用setTag()和getTag()将ViewHolder绑定到该视图。
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
View v = convertView;
if(v == null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.adapter_news, null);
ViewHolder holder = new ViewHolder();
holder.tv_title = (TextView) v.findViewById(R.id.tv_adapter_news_title);
// etc...
v.setTag(holder);
} else {
holder = v.getTag();
}
News item = getItem(position);
holder.tv_title.setText(item.getNewsTitle());
// etc...
return v;
}
答案 1 :(得分:0)
问题出在getView
,你在那里查看v == null
哪个好,但如果v不为null会发生什么......你不会处理它,所以它重用最后一个视图是为什么你看到倍数
你应该这样做
if(v == null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.adapter_news, null);
}
//then set the views after you do the check so it is always done