[英]How to freeze header and footer in ListView
I have added header and footer in a ListView, I just don't know how to freeze them when the list is being scrolled. 我在ListView中添加了页眉和页脚,我只是不知道如何在滚动列表时冻结它们。 Please kindly help me.
请帮助我。 (The code below just for header only)
(以下代码仅适用于标题)
listview.xml listview.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/Relativelayout01"
android:paddingBottom="4dip"
android:paddingLeft="12dip">
<TextView
android:text="Name"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="@+id/name"
android:textSize="30sp"
>
</TextView>
<TextView
android:text="Age"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/age"
android:layout_toRightOf="@+id/name"
android:layout_below="@+id/name"
android:textSize="30sp"
android:layout_alignTop="@id/name">
</TextView>
</RelativeLayout>
listview_header.xml listview_header.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/Relativelayout01"
android:paddingBottom="4dip"
android:paddingLeft="12dip">
<TextView
android:text="Name"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="@+id/nameheader"
android:textSize="30sp"
>
</TextView>
<TextView
android:text="Age"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/ageheader"
android:layout_toRightOf="@+id/nameheader"
android:layout_below="@+id/nameheader"
android:textSize="30sp"
android:layout_alignTop="@id/nameheader">
</TextView>
</RelativeLayout>
DbHelper.java DbHelper.java
public class DbHelper extends SQLiteOpenHelper{
public static final String DATABASE_NAME ="bebook_db";
public DbHelper(Context context){
super(context,DATABASE_NAME,null,1);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE mytable(_id INTEGER PRIMARY KEY AUTOINCREMENT, Name TEXT,Age INTEGER); ");
ContentValues cv = new ContentValues();
cv.put("Name", "Anna");
cv.put("Age", 19);
db.insert("mytable", "Name", cv);
cv.put("Name", "Jane");
cv.put("Age", 21);
db.insert("mytable", "Name", cv);
cv.put("Name", "Mary");
cv.put("Age", 17);
db.insert("mytable", "Name", cv);
//We can add more data here to test the scrolling effect.
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS mytable");
onCreate(db);
}
}
MainActivity.java MainActivity.java
public class MainActivity extends ListActivity {
private SQLiteDatabase db = null;
private Cursor cursor = null;
private SimpleCursorAdapter adapter;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View header = getLayoutInflater().inflate(R.layout.listview_header, null);
ListView listView = getListView();
listView.addHeaderView(header);
db= (new DbHelper (getApplicationContext())).getWritableDatabase();
cursor =db.rawQuery("SELECT _id,Name, Age from mytable ORDER BY Age", null);
adapter = new SimpleCursorAdapter(this,
R.layout.listview,
cursor,
new String[]{"Name","Age"},
new int[]{R.id.name, R.id.age},1);
setListAdapter(adapter);
}
protected void onDestroy() {
super.onDestroy();
cursor.close();
db.close();
}
}
You probably want to investigate sticky list headers . 您可能想要研究粘性列表标题 。 As a note, this question has been asked before .
作为一个说明, 之前已经提出过这个问题。
There is another approach without using any third-party library. 没有使用任何第三方库的另一种方法。 You don't need to use a
ListActivity
. 您不需要使用
ListActivity
。 You can retrieve a listview widget from xml
and in that layout where your ListView
lives, you can create header and footer there. 您可以从
xml
检索listview小部件,并在ListView
所在的布局中,您可以在那里创建页眉和页脚。 But remember to add margins in the top and bottom of the ListView
. 但请记住在
ListView
的顶部和底部添加边距。 Otherwise, your header and footer will not be visible. 否则,您的页眉和页脚将不可见。 Here is a sample
xml
I used in my project - 这是我在项目中使用的示例
xml
-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView //header
android:id="@+id/sTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:textSize="30sp"/>
<ListView
android:id="@+id/iDlockedAppListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/sTextView1"
android:layout_below="@+id/sTextView1"
android:layout_marginTop="5dp" //remember to add margin top
android:layout_marginBottom="70dp" // and bottom
>
</ListView>
<Button // footer
android:id="@+id/btnAdd"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="16dp"
android:text="Add" />
</RelativeLayout>
If you want to add a viewgroup, you can replace header and footer elements with nested layouts. 如果要添加视图组,可以使用嵌套布局替换页眉和页脚元素。 Hope that helps
希望有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.