2 erevshel erevshel 于 2014.03.18 15:52 提问

关于android中listview里面textview显示混乱

哪位大神能帮我解决下面代码中的listview里面textview的显示混乱问题 纠结好久了 谢谢 复制可以直接执行
源文件:
package com.example.listviewtest;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.NumberPicker;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

ListView list;

ArrayList<String> buf = new ArrayList<String>();

LayoutInflater inflater;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    list = (ListView)findViewById(R.id.listView1);
    inflater = getLayoutInflater();
    for (int i=0;i<100;i++)
    {
        buf.add(String.valueOf(i));
    }
    list.setAdapter(adapter);
}


BaseAdapter adapter = new BaseAdapter() {

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder viewHolder;
        if (convertView == null)
        {
            convertView = inflater.inflate(R.layout.item, null);
            viewHolder = new ViewHolder();
            viewHolder.line = (LinearLayout)convertView.findViewById(R.id.single_item);
            convertView.setTag(viewHolder);
        }
        else
        {
            viewHolder = (ViewHolder)convertView.getTag();
        }
        TextView text = (TextView)viewHolder.line.getChildAt(1);
        text.setText(buf.get(position) + "");
        if (Integer.parseInt(buf.get(position)) % 5 == 0)
        {
            text.setTextColor(20);
            viewHolder.line.setClickable(false);
        }

        viewHolder.line.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (Integer.parseInt(buf.get(position)) % 5 == 0)
                {
                    return;
                }
                Toast.makeText(MainActivity.this, "position==" + position, Toast.LENGTH_SHORT).show();
            }
        });

        return convertView;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return buf.size();
    }

    class ViewHolder 
    {
        public LinearLayout line;
    }
};

}

布局文件
1.activity_main.xml
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>


2.item.xml
<?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="horizontal" >

<LinearLayout
    android:id="@+id/single_item"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:src="@drawable/ic_launcher"/>

    <TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="20sp"
    android:padding="15dp"
    android:text="点我"
    android:textColor="#003399"/>

</LinearLayout>

2个回答

qq_27578023
qq_27578023   2016.05.27 23:17

你适配器中的复用出现了问题 。 照你这么写 根本没复用到 。不符合逻辑

u013791878
u013791878   2016.07.01 23:54

这涉及到listview里面的服用机制,其实gridview也是用这一套服用机制,如果你看过android源码就会知道,其实不管你怎么滑动,开启了服用机制,你的view就只有一套,这一套view大size就是你的屏幕可以现实view的数量,你往下往上滑都是服用之前用过的view,所以你在现实时候要做好判读有if就要有else这样是最保险的

Csdn user default icon
上传中...
上传图片
插入图片