简体   繁体   中英

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. Please kindly help me. (The code below just for header only)

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

<?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

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

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 . You can retrieve a listview widget from xml and in that layout where your ListView lives, you can create header and footer there. But remember to add margins in the top and bottom of the ListView . Otherwise, your header and footer will not be visible. Here is a sample xml I used in my project -

 <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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM