0

I create a customer CompulsoryFragmentAdapter extends BaseAdapter ,but I find getview always called and I don't know why?

the getview code :

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LogUtils.i("CompulsoryFragmentAdapter", "position->"+position);
    // TODO Auto-generated method stub
    viewHolder holder=null;
    if(convertView==null){
        holder=new viewHolder();
        convertView=View.inflate(mSoftContext.get(), R.layout.course_list_compulsory_item, null);
        holder.item_ImgSrc=(ImageView) convertView.findViewById(R.id.compulsory_item_icon);
        holder.item_Title=(TextView) convertView.findViewById(R.id.compulsory_item_title);
        holder.item_startTime=(TextView) convertView.findViewById(R.id.compulsory_item_startTime);
        holder.item_endTime=(TextView) convertView.findViewById(R.id.compulsory_item_stopTime);
        holder.item_residueTime=(TextView) convertView.findViewById(R.id.compulsory_item_residueTime);
        holder.item_StudyProgress=(TextView) convertView.findViewById(R.id.compulsory_item_studyprogress);
        holder.item_progressBar=(ProgressBar) convertView.findViewById(R.id.compulsory_item_progressbar);
        convertView.setTag(holder);
    }else{
        holder=(viewHolder) convertView.getTag();
    }
    CompulsoryContentItemEntity item=indexItems.get(position);
    int progress=(int)(CommonUtil.floatRound(item.getProgress(), 2, BigDecimal.ROUND_DOWN)*100);
    holder.item_Title.setText(item.getName());
    holder.item_startTime.setText(DateUtils.datetoStr(item.getStartTime()));
    holder.item_endTime.setText(DateUtils.datetoStr(item.getEndTime()));
    holder.item_residueTime.setText(String.format("剩%s天", item.getIntervalDays()));
    holder.item_progressBar.setProgress(progress);
    holder.item_StudyProgress.setText(progress+"%");
    if(item.getImage().equals("")||item.getImage()==null){
        holder.item_ImgSrc.setImageResource(R.drawable.compulsory_item_img);
    }else{

    }
    return convertView;
}

private class viewHolder{
    ImageView item_ImgSrc;
    TextView  item_StudyProgress;
    TextView  item_Title;
    TextView  item_startTime;
    TextView  item_endTime;
    TextView  item_residueTime;
    ProgressBar item_progressBar;
}

listview.xml file:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/compulsory_bg"
android:orientation="vertical" >

<com.eceibs.xlistview.XListView 
    android:id="@+id/listview" 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:divider="@null"
    />

</LinearLayout>

the item xml file :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/compulsory_bg"
android:paddingBottom="4dp"
android:paddingTop="4dp" >

but the result is :

position->0
position->1
position->2
position->0
position->1
position->2
position->0
position->1
position->2
position->0
position->1
position->2
...

I didn't touch ListView or elsewhere, why did it show result like this?

Pragnesh Ghoda シ
  • 8,318
  • 3
  • 25
  • 40
igeek
  • 3
  • 1

1 Answers1

0

Suppose you have 5 rows in the listview and you can see 2 rows at a time. So when the listview loads, you can see row 1 and row 2. Now suppose you scroll down to the end of the list and you can see rows 4 and rows 5. At this time, android keeps only row 3 in the memory and row 1 and 2 are sent to the recycler. Now, when you scroll up to row 1 and row 2, the getView method is called again for row 1 and row 2 and row 4 and 5 are sent to the recycler.

Shivam Verma
  • 7,973
  • 3
  • 26
  • 34