listview可以添加两个头部布局吗
1个回答
展开全部
第一个头布局我 设置了一下 位置,目的是为了证明,在自定义下拉刷新时,当listview已经添加了一个headerview时候,可以继续添加一个headerview,不影响下拉刷新
代码如下:
MainActivity
package com.ss.hsx;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MyListView listView = (MyListView) findViewById(R.id.listView);
String[] data = new String[] { "第一个", "第二个", "第三个" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, data);
listView.setAdapter(adapter);
}
}
MyListView
package com.ss.hsx;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;
public class MyListView extends ListView {
View one;
public MyListView(Context context) {
super(context);
initHeaderView(context);
}
public MyListView(Context context, AttributeSet attrs) {
super(context, attrs);
initHeaderView(context);
}
public MyListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initHeaderView(context);
}
private void initHeaderView(Context context) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
one = inflater.inflate(R.layout.one, null);
View two = inflater.inflate(R.layout.two, null);
addHeaderView(one);
addHeaderView(two);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//控制第一个头部布局的位置
one.setPadding(0, -1 * one.getHeight()/2, 0, 0);
}
}
代码如下:
MainActivity
package com.ss.hsx;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MyListView listView = (MyListView) findViewById(R.id.listView);
String[] data = new String[] { "第一个", "第二个", "第三个" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, data);
listView.setAdapter(adapter);
}
}
MyListView
package com.ss.hsx;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;
public class MyListView extends ListView {
View one;
public MyListView(Context context) {
super(context);
initHeaderView(context);
}
public MyListView(Context context, AttributeSet attrs) {
super(context, attrs);
initHeaderView(context);
}
public MyListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initHeaderView(context);
}
private void initHeaderView(Context context) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
one = inflater.inflate(R.layout.one, null);
View two = inflater.inflate(R.layout.two, null);
addHeaderView(one);
addHeaderView(two);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//控制第一个头部布局的位置
one.setPadding(0, -1 * one.getHeight()/2, 0, 0);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询