(转)为ListView增加Header (可动态修改其中的内容)
2018-01-29 16:41
387 查看
原地址:http://que2010.iteye.com/blog/1335798
为ListView增加Header (可动态修改其中的内容)
1.新建一个Layout:
demo_list_item_header_view.xml
Xml代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:layout_height="30sp"
android:layout_width="wrap_content"
android:textSize="20sp" android:id="@+id/headerTextView"
android:text="TestListViewHeader" />
</LinearLayout>
2.然后新建一个类,继承自LinearLayout用来显示上面的Layout:
DemoListHeaderView.java
Java代码
package com.zhang.test.view;
import com.zhang.test.R;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
public class DemoListHeaderView extends LinearLayout {
private static final String TAG = "DemoListHeaderView";
private Context context;
private TextView textView;
public DemoListHeaderView(Context context) {
super(context);
this.context = context;
View view = LayoutInflater.from(this.context).inflate(R.layout.demo_list_item_header_view, null);
//以下两句的顺序不能调换,要先addView,然后才能通过findViewById找到该TextView
addView(view);
textView = (TextView) view.findViewById(R.id.headerTextView);
}
public void setTextView(String text) {
textView.setText(text);
}
}
3.之后在ListView设置setAdapter之前,一定要在setAdapter之前
加上代码:
Java代码
DemoListHeaderView headerView = new DemoListHeaderView(context);
headerView.setTextView("Header : ");
listView.addHeaderView(headerView);
为ListView增加Header (可动态修改其中的内容)
1.新建一个Layout:
demo_list_item_header_view.xml
Xml代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:layout_height="30sp"
android:layout_width="wrap_content"
android:textSize="20sp" android:id="@+id/headerTextView"
android:text="TestListViewHeader" />
</LinearLayout>
2.然后新建一个类,继承自LinearLayout用来显示上面的Layout:
DemoListHeaderView.java
Java代码
package com.zhang.test.view;
import com.zhang.test.R;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
public class DemoListHeaderView extends LinearLayout {
private static final String TAG = "DemoListHeaderView";
private Context context;
private TextView textView;
public DemoListHeaderView(Context context) {
super(context);
this.context = context;
View view = LayoutInflater.from(this.context).inflate(R.layout.demo_list_item_header_view, null);
//以下两句的顺序不能调换,要先addView,然后才能通过findViewById找到该TextView
addView(view);
textView = (TextView) view.findViewById(R.id.headerTextView);
}
public void setTextView(String text) {
textView.setText(text);
}
}
3.之后在ListView设置setAdapter之前,一定要在setAdapter之前
加上代码:
Java代码
DemoListHeaderView headerView = new DemoListHeaderView(context);
headerView.setTextView("Header : ");
listView.addHeaderView(headerView);
相关文章推荐
- 为ListView增加Header (可动态修改其中的内容)
- 安卓重要组件#4--动态的增加和删除listView中的内容
- 在ListView中动态添加EditText并对其中的数据进行保存和由于复用引起的Editext中的内容显示异常的解决方案
- ListView视图动态增加与删除控件条目,内容
- 动态修改ListView
- javascript动态增加、删除、填充表格内容
- ScrollView嵌套listview 时根据内容动态设置listview高度
- Android 学习记录-ListView动态布局内容
- Android动态修改ListView中指定Item的组件属性
- [HTML]js动态修改表格里面的内容
- Android--listView长按修改ListView对象内容
- JS动态增加删除UL节点LI及相关内容示例
- 如何实现可动态调整隐藏header的listview
- asp.net 使用HttpModule对全站输出的动态页面的HTML内容进行修改,不会错乱
- Delphi动态的修改WebBrowser加载页面的内容
- android 动态显示隐藏listview的headerview和headerview里的控件
- 动态修改actionbar的Menu菜单中的显示内容
- ListView添加动态悬浮header的另类方式
- Android;sql数据库:关于删除数据库数据无法刷新listview并且新listview的修改增加功能出现bug的问题
- ListView内容变化后的动态刷新