Java 根据位置在ListView中展开自定义视图
我有一个Java 根据位置在ListView中展开自定义视图,java,android,android-listview,android-custom-view,android-inflate,Java,Android,Android Listview,Android Custom View,Android Inflate,我有一个列表视图,我想在某个位置为自定义视图充气。这实际上是第11项(位置10) getCount()方法返回11 下面是getView()方法的顶部: public View getView(final int position, View convertView, ViewGroup parent) { final ViewHolder holder; if (convertView == null) { if (position
列表视图
,我想在某个位置为自定义视图
充气。这实际上是第11项(位置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);
}
其余的getView日志不显示;我假设这一行将一直显示到getView,position=10
这一行:convertView=mInflater.inflate(R.layout.custom,null)永远不会调用code>,因为自定义布局
不会显示在logcat中
不过这很有趣,因为底部的logcat被调用为always(当我向下滚动列表视图时)
:
为什么我的自定义布局没有充气?当getView()
方法View convertView
不为空时,需要充气布局。从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);
}
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);
}
public View getView(最终int位置、视图转换视图、视图组父视图){
最终持票人;
如果(convertView==null){
如果(位置<10){
convertView=mInflater.充气(R.layout.standard,空);
}
Log.i(标签“getView,position=“+position”);
holder=新的ViewHolder();
holder.tv=(TextView)convertView.findViewById(R.id.tv);
holder.radioButton=(radioButton)convertView.findViewById(R.id.radioButton1);
convertView.setTag(支架);
}否则{
holder=(ViewHolder)convertView.getTag();
}
...
如果(位置>10){
convertView=mInflater.充气(R.layout.custom,空);
Log.i(标签“充气自定义布局”);
}
Log.i(标记“getView():position=“+position”);
}
在外部为自定义布局充气convertView==null
。因为convertView通常会返回以前循环使用的视图,因此,您的自定义视图永远不会膨胀。但在这种情况下,每次位置超过10时,自定义视图都会膨胀,对吗?假设getView方法调用100次,位置为11,则将创建该视图的100个实例。是,这是必需的。每次listitems离开visibleView时,listview都会调用Runtime.gc()。
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
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);
}