当滚动listview调整listviewitems的大小时

时间:2012-09-07 12:33:32

标签: android android-listview

我有一个问题,我已经实现了自定义列表视图与布局infleter。当我滚动列表视图时,我面临一个问题,它的项目调整为小项目。我无法跟踪问题..请帮助。

class ListDownloadedAdapter extends BaseAdapter

{

 ArrayList<View> data=null;

 Context mContext=null;

 LayoutInflater mInflater;



    public ListDownloadedAdapter(ArrayList<View> arg,Context context)

    {

          mInflater = LayoutInflater.from(context);

          data=arg;



          mContext=context;

    }

    public void reverse()

    {

       Collections.reverse(data);

    }



 @Override

 public int getCount()

 {

 // TODO Auto-generated method stub

 return data.size();

}



@Override

public Object getItem(int arg0)

{



 return data.get(arg0);

 }



 @Override

 public long getItemId(int arg0) {



  return 0;

 }



  public View getView(int position, View convertView, ViewGroup parent)

  {

    ListContent holder;

    holder=new ListContent();

        //convertView=mInflater.inflate(R.layout.download_progress, parent, false);

    convertView=(View)getItem(position);

    holder.pbar=(ProgressBar)convertView.findViewById(R.id.status_progress);

    holder.statustext=(TextView)convertView.findViewById(R.id.status_text);

    holder.filesize=(TextView)convertView.findViewById(R.id.txtkbps);

    return convertView;

}

class ListContent

{

    ProgressBar pbar=null;

    TextView statustext=null,filesize=null;

}

}

以下是listview实现的xml文件

  <ListView android:id="@+id/downloadsegmentdownloadecomplete" android:divider="@drawable/transparent"  android:layout_width="fill_parent" android:layout_height="wrap_content" android:cacheColorHint="#00000000" android:dividerHeight="0px" ></ListView>

以下是膨胀细胞的xml

  <?xml version="1.0" encoding="utf-8"?>

  <RelativeLayout

   android:layout_width="match_parent" android:paddingTop="5dp" android:paddingBottom="5dp"  android:layout_height="match_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_gravity="center|fill">







    <RelativeLayout

        android:id="@+id/completed_progresslayout"

        android:layout_width="match_parent"

        android:layout_height="wrap_content" >



        <TextView

            android:id="@+id/completed_status_text"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_alignParentTop="true"

            android:layout_marginLeft="10dp"

            android:layout_toRightOf="@+id/txtpercent"

            android:focusable="false"

            android:focusableInTouchMode="false"

            android:text="filename"

            android:textColor="#686870" />



        <TextView

            android:id="@+id/completed_txtkbps"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_alignParentRight="true"

            android:layout_below="@+id/completed_status_text"

            android:layout_marginRight="10dp"

            android:text="kbps"

            android:textColor="#b0b0b8" />

    </RelativeLayout>

1 个答案:

答案 0 :(得分:0)

对不起,但是你的整个适配器实现都是错误的。

适配器应该只存储数据(字符串,整数,可能是游标),但不存储视图本身。 对于您要实现的目标,我建议您扩展ArrayAdapter并仅覆盖getView()

类似的东西:

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

    if (convertView == null)
        convertView = createNewView();
    Holder h = (Holder)convertView.getTag();

    h.textView1.setText( .... // from here you put the data into the existing views.

    return convertView;

    // then the new view
    private View createNewView() {

    // Instantiate view
    View v = getLayoutInflater().inflate(R.layout.myView, null);
    Holder h = new Holder();
    v.setTag(h);

    // point to the views
    h.textView1 = (TextView) findViewById(R.id.title);
    // carry on for the other views....

    return v;
    }

这对你有意义吗?