2013-05-27 195 views
0

我已經開發了一個自定義listview應用程序,它工作正常,但滾動佈局的數據更改。android動態列表視圖滾動

我的列表視圖佈局:

<?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類:

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個進度條,

滾動後,我只有3 TextView的和1個進度

+0

您能告訴我們您的StartLessonAdapter的代碼嗎? –

+0

用StartLessonAdapter類改變了主要風格 – shuttle1978

+0

基本上當你滾動列表視圖適配器刷新和爲什麼它會這樣做!我很早就面臨這個問題 – UnderGround

回答

0

你只應在視圖爲空時使用findviewbyid。你可以用這個替換你的getView方法。它現在應該工作。

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; 
    } 
+0

好吧,我已經改變了代碼(copyng你的代碼),但我有同樣的問題 – shuttle1978

+0

好吧,現在它的工作原理,我已經移動了行: holder.progressTestLiv1Bar.setProgressDrawable(view.getResources()。getDrawable(R.drawable.progressbartest_style)); holder.progressTestLiv2Bar.setProgress(entry.getProgressTestLiv2Ok()); 進入if ...非常感謝 – shuttle1978

+0

很高興知道:) –

0
public Object getItem(int arg0) { 
     // TODO Auto-generated method stub 
     //return null; Return Here Object Or Something like 
     retrun arraylist[arg0] 
    } 

可能會解決你的問題不知道,但嘗試

+0

對不起,它不起作用 – shuttle1978