根据职位夸大ListView中的自定义视图 [英] Inflating custom View in ListView according to position

查看:254
本文介绍了根据职位夸大ListView中的自定义视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的ListView ,我想在某个位置膨胀的自定义查看。它实际上是第11项(第10位)。

I have a ListView where I would like to inflate a custom View at a certain position. It's actually the 11th item (position 10).

getCount将()方法返回11。

下面是 getView()方法的顶部:

public View getView(final int position, View convertView, ViewGroup parent) {
    final ViewHolder holder;
    if (convertView == null) {              
        if (position < 10) {
            convertView = mInflater.inflate(R.layout.standard, null);           
        } else {
            convertView = mInflater.inflate(R.layout.custom, null);
            Log.i(TAG, "Inflated custom layout");
        }
        Log.i(TAG, "getView, position = "+position);
        holder = new ViewHolder();
        holder.tv = (TextView) convertView.findViewById(R.id.tv);
        holder.radioButton = (RadioButton) convertView.findViewById(R.id.radioButton1);             
        convertView.setTag(holder);             
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    ...
    Log.i(TAG, "getView() : position = " + position);
}

这就是我看到的logcat:

Here's what I see in the logcat:

getView, position = 0
getView, position = 1
getView, position = 2

在getView日志的其余部分不显示;我会假设,这条线将出现一路攀升至 getView,位置= 10

这行: convertView = mInflater.inflate(R.layout.custom,NULL); 永远不会被调用,因为充气自定义布局没有出现在logcat中显示出来。

And this line: convertView = mInflater.inflate(R.layout.custom, null); is never called, because Inflated custom layout doesn't show up in the logcat.

这很有趣,但因为底部的logcat总是叫(我向下滚动的ListView

It's funny though, because the bottom logcat is called always (as I scroll down the ListView):

getView() : position = 0
getView() : position = 1
getView() : position = 2
getView() : position = 3
getView() : position = 4
getView() : position = 5
getView() : position = 6
getView() : position = 7
getView() : position = 8
getView() : position = 9
getView() : position = 10

为什么是我的自定义布局不膨胀?

Why is my custom layout not inflated?

推荐答案

您需要充气布局时, getView()方法查看convertView 不为空。在查看这是从返回getView()将被重用。如果选中观点是空和膨胀会膨胀,只有当它第一次返回查看

You need to inflate the layout when the getView() methods View convertView is not null. The View which is returned from getView() will be reused. If you check the view is null and inflate it will inflate only when its first time returning the View.

public View getView(final int position, View convertView, ViewGroup parent) {
    final ViewHolder holder;
    if (convertView == null) {              
        if (position < 10) {
            convertView = mInflater.inflate(R.layout.standard, null);           
        }
        Log.i(TAG, "getView, position = "+position);
        holder = new ViewHolder();
        holder.tv = (TextView) convertView.findViewById(R.id.tv);
        holder.radioButton = (RadioButton) convertView.findViewById(R.id.radioButton1);             
        convertView.setTag(holder);             
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    ...

    if(position > 10) {
         convertView = mInflater.inflate(R.layout.custom, null);
         Log.i(TAG, "Inflated custom layout");
    }

    Log.i(TAG, "getView() : position = " + position);
}

这篇关于根据职位夸大ListView中的自定义视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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