2015-06-23 36 views
0

我有一个ListView和一个自定义适配器。现在我有关于getView()多次调用方法的红色帖子,其中大部分都与ListViewwrap_content功能有关。Listview - 多次调用Getview

我已经改变了我的身高,所以它可以是match_parent但仍然调用该方法。

我认为它与在适配器中动态添加TextView s有关,并且我不知道如何去做,所以它可以正常工作。

如果有另一种选择,我打开它,我把TextView s的唯一原因是我可以用不同颜色书写字母。

这里是我的代码:

@Override 
public View getView(int position, View convertView, ViewGroup parent) 
{ 
    View v = convertView; 
     Row current = mList.get(position); 

     /* if the given channel row view is not being updated*/ 
     if (v == null) 
     { 
     /* inflate layout */ 
      LayoutInflater vi = LayoutInflater.from(getContext()); 
      v = vi.inflate(R.layout.list_item2, null,false); 
     } 

     v = setLinearLayout(v,current); 

    return v; 
} 

这里是setLinearLayout()方法:

private View setLinearLayout(View v,Row current) { 

    ArrayList<Integer> winResults = current.checkNumbers(mResult,mType); 
    int numbers[] = current.getNumbers(); 

    int N = Global.getNumberOfNumbers(mType); 
    boolean FLAG_SET = false; 

    final TextView[] myTextViews = new TextView[N]; // create an empty array; 

    for (int i = 0; i < N; i++) { 
     Log.d("PAVLE",""+i+" row is: "+current.getStringNumbers()); 
     // create a new textview 
     final TextView rowTextView = new TextView(v.getContext()); 

     LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,75); 
     rowTextView.setTextSize(24.0f); 
     rowTextView.setPadding(8, 8, 8, 8); 
     rowTextView.setGravity(Gravity.CENTER); 
     rowTextView.setBackgroundColor(Color.BLACK); 
     rowTextView.setTypeface(null, Typeface.BOLD); 
     rowTextView.setLayoutParams(lp); 

     if(mType == R.string.sans_topu && i == 5 && !FLAG_SET){ 
      i--; 
      rowTextView.setTextColor(Color.WHITE); 
      rowTextView.setText("+"); 
      FLAG_SET = true; 
     } 
     else { 
      // set some properties of rowTextView or something 
      if (mWin == Global.WIN || mWin == Global.LOSE) { 
       if (winResults.contains(numbers[i])) 
        rowTextView.setTextColor(Color.GREEN); 
       else rowTextView.setTextColor(Color.RED); 
      } else { 
       rowTextView.setTextColor(Color.WHITE); 
      } 
      if (numbers[i] < 10) { 
       rowTextView.setText("0" + numbers[i]); 
      } else rowTextView.setText("" + numbers[i]); 
     } 
     // add the textview to the linearlayout 
     LinearLayout ll = (LinearLayout) v.findViewById(R.id.ll_item); 
     ll.addView(rowTextView); 

     // save a reference to the textview for later 
     myTextViews[i] = rowTextView; 
    } 

    final TextView prize = new TextView(v.getContext()); 
    LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,75); 
    prize.setTextSize(24.0f); 
    prize.setPadding(8, 8, 8, 8); 
    prize.setGravity(Gravity.CENTER); 
    prize.setBackgroundColor(Color.BLACK); 
    prize.setTypeface(null, Typeface.BOLD); 
    prize.setTextColor(Color.WHITE); 
    prize.setLayoutParams(lp); 

    try { 
     String cash = findCorretAmmount(winResults, numbers); 
     prize.setText(cash); 
     mTotal.append(" "+cash); 
    } 
    catch (Exception e){ 
     e.printStackTrace(); 
    } 

    LinearLayout ll = (LinearLayout) v.findViewById(R.id.ll_item); 
    ll.addView(prize); 

    return v; 
} 

和XML的一点点:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

<TextView 
    android:id="@+id/tvRandomTextView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="16dp" 
    android:paddingRight="8dp" 
    android:text="@string/your_ticket_numbers_" 
    android:textColor="@android:color/black" 
    android:textSize="28sp" 
    android:textStyle="bold" 
    /> 

<View 
    android:id="@+id/divider4" 
    android:layout_width="match_parent" 
    android:layout_height="1dp" 
    android:layout_below="@id/tvRandomTextView" 
    android:layout_marginBottom="4dp" 
    android:layout_marginTop="4dp" 
    android:background="@android:color/darker_gray"/> 


<ListView 
    android:id="@+id/lvRowList" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_below="@id/divider4" 
    /> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:paddingBottom="16dp" 
    android:paddingLeft="16dp" 
    android:paddingRight="16dp" 
    > 

    <Button 
     android:id="@+id/btnDelete" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:text="@string/delete"/> 

    <Button 
     android:id="@+id/btnShare" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:text="@string/btn_share"/> 


    <Button 
     android:id="@+id/btnDone" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:text="@string/done"/> 
</RelativeLayout> 

<TextView 
    android:id="@+id/tvResultLink" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginBottom="4dp" 
    android:layout_marginTop="4dp" 
    android:autoLink="web" 
    android:gravity="center" 
    android:linksClickable="true" 
    /> 

还值得我说明list_item2(LinearLayout)的高度为100dp,它的大小是固定的。

回答

1

我认为它不是关于textview。 getView总是被多次调用。如果屏幕上显示10个项目,getView将被调用15次,因为android会创建不在屏幕上的视图。这很好,因为当用户开始滚动时,它不会落后。

用户离开物品后,视图将被回收并由适配器重新使用。比方说,你有一个10000000个项目的列表,但你的屏幕上一直有5个项目。在这种情况下 - 为了节省电力并提高性能 - android将创建10个列表项目,并且这10个项目将按内容进行recylce和刷新。

ViewHolder模式 请仔细阅读这一点,并使用该图案来提高你的代码性能: http://developer.android.com/training/improving-layouts/smooth-scrolling.html

谷歌约的ListView: http://developer.android.com/guide/topics/ui/layout/listview.html

教程http://developer.xamarin.com/guides/android/user_interface/working_with_listviews_and_adapters/

+0

谢谢我将采取 看看。 (: –

+0

这是我的荣幸,请花时间。如果我的链接有用,请不要犹豫,接受并询问更多:) – Karoly