Android的动态滚动列表视图 [英] android dynamic listview scroll

查看:432
本文介绍了Android的动态滚动列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开发了自定义列表视图的应用程序,它工作正常,但滚动的数据变化的布局之后。

I've developed an application with custom listview, it works fine but after scroll the layout of data change.

我的列表视图布局:

<?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="wrap_content"
    android:background="@drawable/layout_start_lesson"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textlesson"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical|center_horizontal"
            android:padding="8dp"
            android:text="Medium Text"
            android:textSize="18dp"
            android:textStyle="bold" />

        <View
            android:id="@+id/view1"
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:background="#E0E0E0" />
    </LinearLayout>

    <!-- Layout Progress -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textViewProgress"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left|center_vertical|center_horizontal"
            android:padding="5dp"
            android:text="@string/progress"
            android:textSize="16dp" />

        <ProgressBar
            android:id="@+id/progressBar"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="left|center_vertical|center_horizontal"
            android:layout_weight="0.5"
            android:max="100"
            android:progress="0"
            android:progressDrawable="@drawable/progressbar_style" />

        <TextView
            android:id="@+id/textProgress"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="5dp"
            android:paddingRight="10dp"
            android:text="Medium Text"
            android:textSize="16dp" />
    </LinearLayout>

    <!-- Layout Test Liv 1-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textViewTest"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left|center_vertical|center_horizontal"
            android:padding="5dp"
            android:text="@string/testLiv1"
            android:textSize="16dp" />

        <ProgressBar
            android:id="@+id/progressBarTest"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="left|center_vertical|center_horizontal"
            android:layout_weight="0.5"
            android:max="100"
            android:progress="0"
            android:progressDrawable="@drawable/progressbar_style" />

        <TextView
            android:id="@+id/textPercentTest"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="5dp"
            android:paddingRight="10dp"
            android:text="Medium Text"
            android:textSize="16dp" />
    </LinearLayout>

    <!-- Layout Test Liv 2-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textViewTestLiv2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left|center_vertical|center_horizontal"
            android:padding="5dp"
            android:text="@string/testLiv2"
            android:textSize="16dp" />

        <ProgressBar
            android:id="@+id/progressBarTestLiv2"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="left|center_vertical|center_horizontal"
            android:layout_weight="0.5"
            android:max="100"
            android:progress="0"
            android:progressDrawable="@drawable/progressbar_style" />

        <TextView
            android:id="@+id/textPercentTestLiv2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="5dp"
            android:paddingRight="10dp"
            android:text="Medium Text"
            android:textSize="16dp" />
    </LinearLayout>

</LinearLayout>

在这里我声明:

StartLessonAdapter adapter = new StartLessonAdapter(datalist);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() { .... }

我startlessonadapter类:

My startlessonadapter class:

public View getView(int position, View view, ViewGroup parent) {
        final StartLessonTestBean entry = (StartLessonTestBean) languageBean
                .get(position);

        int main = R.layout.layout_start_lesson;

        final ViewHolderLanguage holder;

        View convertView = null;
        if (view == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(main, null);
        } else {
            convertView = view;
        }

        holder = new ViewHolder();
        holder.txtLesson = (TextView) convertView.findViewById(R.id.textlesson);
        holder.txtLesson.setText(entry.getLesson().trim());


        // ---- START Progress -------
        holder.progressBarTraining = (ProgressBar) convertView
                .findViewById(R.id.progressBar);
        holder.progressBarTraining.setMax(100);
        holder.progressBarTraining.setProgress(entry.getProgressTraining());

        holder.textProgressTraining = (TextView) convertView
                .findViewById(R.id.textProgress);
        String pr = entry.getTxtProgressTraining().trim(); 
        holder.textProgressTraining.setText(pr);
        // ----- END Progress

        // ---- START Test Liv 1 -------
        holder.progressTestLiv1Bar = (ProgressBar) convertView
                .findViewById(R.id.progressBarTest);
        holder.progressTestLiv1Bar.setMax(100);
        holder.progressTestLiv1Bar.setProgressDrawable(convertView
                .getResources().getDrawable(R.drawable.progressbartest_style));
        holder.progressTestLiv1Bar.setProgress(entry.getProgressTestLiv1Ok());
        holder.progressTestLiv1Bar.setSecondaryProgress(entry
                .getProgressTestLiv1NotOk());

        holder.textTestLiv1Progress = (TextView) convertView
                .findViewById(R.id.textPercentTest);
        pr = entry.getTxtProgressTestLiv1().trim(); 
        holder.textTestLiv1Progress.setText(pr);
        // ----- END Test Liv 1

        // ---- START Test Liv 2 -------
        holder.progressTestLiv2Bar = (ProgressBar) convertView
                .findViewById(R.id.progressBarTestLiv2);
        holder.progressTestLiv2Bar.setMax(100);
        holder.progressTestLiv2Bar.setProgressDrawable(convertView
                .getResources().getDrawable(R.drawable.progressbartest_style)); 
        holder.progressTestLiv2Bar.setProgress(entry.getProgressTestLiv2Ok());
        holder.progressTestLiv2Bar.setSecondaryProgress(entry
                .getProgressTestLiv2NotOk());

        holder.textTestLiv2Progress = (TextView) convertView
                .findViewById(R.id.textPercentTestLiv2);
        pr = entry.getTxtProgressTestLiv2().trim(); 
        holder.textTestLiv2Progress.setText(pr);
        // ----- END Test Liv 2

        holder.position = position;

        return convertView;
    }

public int getCount() {
        return languageBean.size();
    }

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

    public long getItemId(int position) {
        return position;
    }

为什么数据都在滚动后改变?
在布局我有3 TextView的3进度条,

Why the data have changed after the scroll? In layout I've 3 textview and 3 progress bar,

滚动后,我只有3 TextView的和1个进度

after scroll I've only 3 textview and 1 progressbar

推荐答案

您应该使用findviewbyid只有当视图为空。你可以用这个替换您的getView方法。现在应该工作。

You should use findviewbyid only when the view is null. You can replace your getView method with this one. It should work now.

public View getView(int position, View view, ViewGroup parent) 
      {

StartLessonTestBean entry = (StartLessonTestBean) languageBean.get(position);
        int main = R.layout.layout_start_lesson;
        ViewHolderLanguage holder = null;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (view == null) {

            holder = new ViewHolderLanguage();
            view = inflater.inflate(main, null);
            holder.txtLesson = (TextView) convertView.findViewById(R.id.textlesson);
            holder.progressBarTraining = (ProgressBar) view.findViewById(R.id.progressBar);
            holder.textProgressTraining = (TextView) view.findViewById(R.id.textProgress);
            holder.progressTestLiv1Bar = (ProgressBar) view.findViewById(R.id.progressBarTest);
            holder.textTestLiv1Progress = (TextView) view.findViewById(R.id.textPercentTest);
            holder.progressTestLiv2Bar = (ProgressBar) view.findViewById(R.id.progressBarTestLiv2);
            holder.textTestLiv2Progress = (TextView) view.findViewById(R.id.textPercentTestLiv2);
            view.setTag(holder);
        } else {
            holder = (ViewHolderLanguage)view.getTag();
        }

        holder.txtLesson.setText(entry.getLesson().trim());


        holder.progressBarTraining.setMax(100);
        holder.progressBarTraining.setProgress(entry.getProgressTraining());

        String pr = entry.getTxtProgressTraining().trim(); 
        holder.textProgressTraining.setText(pr);


        holder.progressTestLiv1Bar.setMax(100);
        holder.progressTestLiv1Bar.setProgressDrawable(view.getResources().getDrawable(R.drawable.progressbartest_style));
        holder.progressTestLiv1Bar.setProgress(entry.getProgressTestLiv1Ok());
        holder.progressTestLiv1Bar.setSecondaryProgress(entry.getProgressTestLiv1NotOk());

        pr = entry.getTxtProgressTestLiv1().trim(); 
        holder.textTestLiv1Progress.setText(pr);


        holder.progressTestLiv2Bar.setMax(100);
        holder.progressTestLiv2Bar.setProgressDrawable(view.getResources().getDrawable(R.drawable.progressbartest_style)); 
        holder.progressTestLiv2Bar.setProgress(entry.getProgressTestLiv2Ok());
        holder.progressTestLiv2Bar.setSecondaryProgress(entry.getProgressTestLiv2NotOk());

        pr = entry.getTxtProgressTestLiv2().trim(); 
        holder.textTestLiv2Progress.setText(pr);

        return view;
    }

这篇关于Android的动态滚动列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆