【问题标题】:Listview - Getview called multiple timesListview - 多次调用 Getview
【发布时间】:2015-09-09 01:22:51
【问题描述】:

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

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

我认为这与在适配器中动态添加TextViews 有关,我不知道该怎么做,所以它可以正常工作。

如果有替代方案,我愿意接受,我输入TextViews 的唯一原因是我可以用不同颜色书写字母。

这是我的代码:

    @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,是固定大小的。

【问题讨论】:

    标签: android listview android-listview


    【解决方案1】:

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

    用户离开项目后,视图被适配器回收和重用。可以说,您有一个包含 10000000 项的列表,但屏幕上始终有 5 项。在这种情况下 - 为了节省电量和提高性能 - android 将创建 10 个列表项,这 10 个项目将按内容循环和刷新。

    ViewHolder 模式 请阅读此内容并使用此模式来提高您的代码性能: http://developer.android.com/training/improving-layouts/smooth-scrolling.html

    Google 关于 ListView: http://developer.android.com/guide/topics/ui/layout/listview.html

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

    【讨论】:

    • 谢谢,我会看看。 (:
    • Its my pleasure, take your time. If my links was useful , dont 犹豫是否接受并询问更多信息 :)
    猜你喜欢
    • 2012-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-22
    相关资源
    最近更新 更多