2

我制作了自定义列表视图,它从手机存储中加载了很多东西,当我向下滚动时,列表视图文本会像这样消失:

在此处输入图像描述

那么如果我向上滚动可见的文本也会消失,经典的列表视图会显示所有文本。这是我的 getView 代码:

public View getView(int position, View convertView, ViewGroup parent) {
           View row = convertView;
           ListViewHolder holder = null ;
             String id = null;

           if(row == null)
        {
               LayoutInflater inflater = getLayoutInflater();
            row = inflater.inflate(R.layout.listview_item_row, parent, false);
         holder = new ListViewHolder();
      music_column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
      musiccursor.moveToPosition(position);
      id = musiccursor.getString(music_column_index);
      holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
      holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
      holder.txtTitle.setText(id);
      holder.imgIcon.setImageResource(R.drawable.musicc);
      row.setTag(holder);
             } else {
                   holder = (ListViewHolder)row.getTag(); 
             }
           holder.txtTitle.setVisibility(View.VISIBLE);
           holder.txtTitle.setText(id);
           holder.imgIcon.setImageResource(R.drawable.musicc);
             return row;
       }

和行:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp">

     <ImageView android:id="@+id/imgIcon"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:gravity="center_vertical"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:layout_marginRight="15dp"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp" />

     <TextView android:id="@+id/txtTitle"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center_vertical"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:textStyle="bold"
        android:textSize="22dp"
        android:textColor="#000000"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp" />

</LinearLayout>

主.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical" android:layout_width="fill_parent"
      android:layout_height="fill_parent">
    <ListView
        android:id="@+id/PhoneMusicList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:cacheColorHint="#00000000"
        android:scrollingCache="false"
        >
    </ListView>

</LinearLayout>
4

3 回答 3

0

你能试试这个:

holder = new ListViewHolder();
music_column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
musiccursor.moveToPosition(position);
id = musiccursor.getString(music_column_index);
holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
holder.txtTitle.setText(id);
holder.imgIcon.setImageResource(R.drawable.music);
row.setTag(holder);
于 2013-02-23T12:26:06.183 回答
0

holder做错了。您首先将holderView 设置为 Tag 值,然后初始化持有者。颠倒这个顺序。

holder = new ListViewHolder();
row.setTag(holder); // remove this line
holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
holder.txtTitle.setText(id);
holder.imgIcon.setImageResource(R.drawable.music);
row.setTag(holder);  // and add this one
于 2013-02-23T12:04:02.707 回答
0

只需在您的列表视图 xml 中使用

android:cacheColorHint="#00000000"
于 2013-02-23T12:11:16.023 回答