如何在Android中将列表与标题分开?

时间:2014-06-22 11:12:05

标签: android android-layout android-listview navigation-drawer

我正在尝试在separator中为ListView添加drawerLayout标题,但我看不到separators,我只看到标题。

这是我的代码:

public class MainActivity extends ActionBarActivity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    public Cursor cursor;
    public Cursor cursorsex;
    public ArrayList<String> array;
    public ArrayList<String> arraysex;
    private ArrayAdapter<String> listAdapter ;
    private ArrayAdapter<String> listAdaptersex ;
    private ListView mDrawerList;
    private ListView mDrawerListsex;
.....
Connection to sqlite
.....
        sql = db.openDataBase();
        cursor = sql.rawQuery("SELECT Title FROM WebSite_CategoryBack WHERE ParentID=0;", null);
        array = new ArrayList<String>();
        while(cursor.moveToNext()){
            Titel_Drawer = cursor.getString(cursor.getColumnIndex("Title"));
            array.add(Titel_Drawer);
        }
        cursor.close();


        cursorsex = sql.rawQuery("SELECT Title FROM WebSite_CategoryBack WHERE ParentID=0;", null);
        arraysex = new ArrayList<String>();
        while(cursorsex.moveToNext()){
            Titel_Drawer = cursorsex.getString(cursorsex.getColumnIndex("Comment"));
            arraysex.add(Titel_Drawer);
        }
        cursorsex.close();


        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);
        mDrawerListsex = (ListView) findViewById(R.id.sex);


        listAdapter = new ArrayAdapter<String>(this, R.layout.drawer_list,R.id.textView, array);
        mDrawerList.setAdapter( listAdapter );

        listAdaptersex = new ArrayAdapter<String>(this, R.layout.xxx,R.id.xxx, arraysex);
        mDrawerListsex.setAdapter( listAdaptersex );
   }
}

这是xxx.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:id="@+id/xxx"/>

这是drawer_list.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:id="@+id/textView"/>

这是activity_main.xml

<android.support.v4.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    <!-- The main content view -->
    <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    <LinearLayout
        android:id="@+id/drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_gravity="start">
        <TextView
            android:layout_width="240dp"
            android:layout_height="25dp"
            android:textSize="20sp"
            android:gravity="center"
            android:text="@string/prof"
            android:background="@drawable/drawer_title_shape" />
    <!-- The navigation drawer -->

    <ir.makarem.extentions.ClearableEditText
        android:id="@+id/SearchEdittxt"
        android:drawableLeft="@drawable/ic_action_search"
        android:layout_width="240dp"
        android:layout_height="35dip"
        android:background="@drawable/textlines"
        android:gravity="right"
        android:hint="@string/action_search" />

    <ListView
            android:id="@+id/left_drawer"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp"
            android:background="#FFDDDDDD"/>


        <ListView
            android:id="@+id/sex"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp"
            android:background="#FFDDDDDD"/>


</LinearLayout>
</android.support.v4.widget.DrawerLayout>

例如,我需要看到这张照片。 Sample

我为Title添加了两个TextView,为ListView添加了两个。{/ p>

2 个答案:

答案 0 :(得分:-1)

您需要为ListView 设置分隔线 :(代码示例)

// white color for example
int[] clr = {0, 0xFFFFFFFF, 0};
listView.setDivider(new GradientDrawable(Orientation.RIGHT_LEFT, clr));
listView.setDividerHeight(1); // set your desired height

或者在.xml中设置分隔符:

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:divider="#FFFFFF"
        android:dividerHeight="2px" >
    </ListView>

答案 1 :(得分:-1)

您必须使用节标题列表视图来实现此功能

参考链接Section header listview