我已经在ListView中添加了页眉和页脚,但我只是不知道在滚动列表时如何冻结它们。请帮助我。 (下面的代码仅用于标题)

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();
    }

}

最佳答案

您可能想调查sticky list headers
注意,此问题为asked before

关于android - 如何卡住ListView中的页眉和页脚,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19605884/

10-15 19:02