ListView混乱滚动

时间:2012-05-21 11:25:42

标签: android android-layout

我已经扩展了基本适配器---我有一些奇怪的问题...当我滚动这个列表视图时 - 我的最后一个项目被替换为第一个项目和下次当我滚动 - 另一个项目和所有..有时它是正确的..为什么它会发生这个代码?

@Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // sets the view onto list view
        LinearLayout rowLayout = null;

        System.out.println("getView " + position + " " + convertView);
        rowLayout = (LinearLayout) mInflater.inflate(
                R.layout.apps_list_row_items, parent, false);
        if (convertView == null) {
            // inflating the row

            vh.mAppIcon = (ImageView) rowLayout.findViewById(R.id.icon);
            vh.mAppName = (TextView) rowLayout.findViewById(R.id.application);

            vh.mAppHint = (TextView) rowLayout.findViewById(R.id.hint);

            mDownloadButton = (Button) rowLayout.findViewById(R.id.download);
            mDownloadButton.setFocusable(false);

        } else {
            // convertView.getTag();
            rowLayout = (LinearLayout) convertView;
        }
        // convertView.getTag();

        // On Click of the download button which triggers an async to download
        // the file.


        mIcon = mAppIconMapList.get(position);
        System.out.println("Icon " + mIcon);
        mCurrentApplication = mAvaiableApps.get(position);
        System.out.println("Current App " + mCurrentApplication);
        vh.mAppIcon.setImageBitmap(mIcon.get(mCurrentApplication));
        vh.mAppName.setText(mCurrentApplication.replace(".apk", ""));
        vh.mAppHint.setText("Click here to view Description");

        return rowLayout;
    }

2 个答案:

答案 0 :(得分:2)

问题是listadapter有时会重用行。如果要解决此问题,请删除以下内容:

if (convertView == null) {   <----remove
   //code
}                            <----remove
else{                        <----remove
   ...                       <----remove
}                            <----remove

答案 1 :(得分:0)

您的 getView 方法看起来有点可疑,请尝试按以下方式进行更改:

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
        // sets the view onto list view

        ViewHolder vh = null;


        System.out.println("getView " + position);

        if (convertView == null) {
            // inflating the row
            LinearLayout rowLayout = (LinearLayout) mInflater.inflate(
                R.layout.apps_list_row_items, parent, false);

            vh.mAppIcon = (ImageView) rowLayout.findViewById(R.id.icon);
            vh.mAppName = (TextView) rowLayout.findViewById(R.id.application);

            vh.mAppHint = (TextView) rowLayout.findViewById(R.id.hint);

            mDownloadButton = (Button) rowLayout.findViewById(R.id.download);
            mDownloadButton.setFocusable(false);

            convertView.setTag(vh);

        } else {
            vh = (ViewHolder) convertView.getTag();
        }


        // On Click of the download button which triggers an async to download
        // the file.


        mIcon = mAppIconMapList.get(position);
        System.out.println("Icon " + mIcon);
        mCurrentApplication = mAvaiableApps.get(position);
        System.out.println("Current App " + mCurrentApplication);
        vh.mAppIcon.setImageBitmap(mIcon.get(mCurrentApplication));
        vh.mAppName.setText(mCurrentApplication.replace(".apk", ""));
        vh.mAppHint.setText("Click here to view Description");

        return convertView;
}