1

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

}
4

2 回答 2

3

您可能想要调查粘性列表标题。作为说明,这个问题之前已经被问过

于 2013-10-26T11:28:45.667 回答
1

还有另一种不使用任何第三方库的方法。您不需要使用ListActivity. xml您可以从您所在的布局中检索列表视图小部件ListView,您可以在那里创建页眉和页脚。但请记住在ListView. 否则,您的页眉和页脚将不可见。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>

如果要添加视图组,可以使用嵌套布局替换页眉和页脚元素。希望有帮助

于 2013-10-26T11:52:24.770 回答