15

我用新的导航抽屉对象创建了一个项目。

我想自定义菜单的布局,添加另一个对象,如 TextView、ImageView ...首先,我想修改仅由一个列表视图组成的默认布局,方法是在列表显示。

今天,我尝试使用“addHeaderView”,但我认为它只能用于添加一个标题。

如何添加标题并真正自定义我的布局菜单?因为,从开发人员 API 看来,“android.support.v4.widget.DrawerLayout”下似乎只有两个孩子是允许的。

这是我今天的布局截图:

列表视图中的导航抽屉标题

这是我要创建的捕获:

列表视图中的导航抽屉标题

这是我的 MainActivity 的一段代码:

public class MainActivity extends Activity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;

private CharSequence mDrawerTitle;
private CharSequence mTitle;
private String[] mPlanetTitles;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mTitle = mDrawerTitle = getTitle();
    mPlanetTitles = getResources().getStringArray(R.array.planets_array);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

    // Declaration of the 2 listview's 
    mDrawerList = (ListView) findViewById(R.id.dernieres_news);

    LayoutInflater inflater = getLayoutInflater();

    // Add header news title
    ViewGroup header_news = (ViewGroup)inflater.inflate(R.layout.header_dernieres_news, mDrawerList, false);
    mDrawerList.addHeaderView(header_news, null, false);

    // set a custom shadow that overlays the main content when the drawer opens
    mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

    String[] names=new String[]{"Title 1", "Title 2", "Title 3", "Title 4", "Title 5"};

    /*Array of Images*/
    int[] image = new int[] {R.drawable.ic_action_feed, R.drawable.ic_action_feed, R.drawable.ic_action_feed, R.drawable.ic_action_feed, R.drawable.ic_action_feed};

    List<HashMap<String, String>> listinfo = new ArrayList<HashMap<String, String>>();
    listinfo.clear();
    for(int i=0;i<5;i++){
        HashMap<String, String> hm = new HashMap<String, String>();
        hm.put("name", names[i]);
        hm.put("image", Integer.toString(image[i]));
        listinfo.add(hm);
    }

    // Keys used in Hashmap
    String[] from = { "image", "name" };
    int[] to = { R.id.img, R.id.txt };
    SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), listinfo, R.layout.drawer_list_item, from, to);
    mDrawerList.setAdapter(adapter);

    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

    // enable ActionBar app icon to behave as action to toggle nav drawer
    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);

    // ActionBarDrawerToggle ties together the the proper interactions
    // between the sliding drawer and the action bar app icon
    mDrawerToggle = new ActionBarDrawerToggle(
            this,                  /* host Activity */
            mDrawerLayout,         /* DrawerLayout object */
            R.drawable.ic_drawer,  /* nav drawer image to replace 'Up' caret */
            R.string.drawer_open,  /* "open drawer" description for accessibility */
            R.string.drawer_close  /* "close drawer" description for accessibility */
            ) {
        public void onDrawerClosed(View view) {
            getActionBar().setTitle(mTitle);
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }

        public void onDrawerOpened(View drawerView) {
            getActionBar().setTitle(mDrawerTitle);
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }
    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);

    if (savedInstanceState == null) {
        selectItem(0);
    }
}

以及 activity_main.xml 的代码:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />


    <ListView
        android:id="@+id/dernieres_news"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#F3F3F4"
        android:choiceMode="singleChoice"
        android:divider="#E3E9E3"
        android:dividerHeight="1dp" />

4

3 回答 3

8

其他答案都是正确的。

我发现了一个非常好的自定义视图以包含两种类型的项目的示例:menu sectionmenu item。当然,您可以将其更改为您想要的任何内容。

该示例还包含一个抽象活动类的实现,每个带有导航抽屉的活动都继承自该类。

http://www.michenux.net/android-navigation-drawer-748.html

于 2013-09-10T11:18:09.323 回答
8

您可以像在任何其他 中添加标题一样执行此操作,方法是ListView教您ListAdapter返回标题行和详细信息行。在低级别,这涉及覆盖您的getViewTypeCount()和之类的方法,以及了解行类型之间的区别。或者,使用现有的高级实现,如https://github.com/emilsjolander/StickyListHeadershttp://code.google.com/p/android-amazing-listview/或在搜索.getItemViewType()ListAdaptergetView()android listview headers

于 2013-06-07T12:43:37.880 回答
1

您必须继承 BaseAdapter 并在 ListView 中使用它而不是 SimpleAdapter。

您向适配器提供填充数据,其中额外的数据是标题。标题和列表项本身将是同一公共类的成员。然后在适配器中,您根据数据项决定实际视图是标题还是项目,并相应地对其进行膨胀。

更新:

这是一个很好的例子: http ://w2davids.wordpress.com/android-sectioned-headers-in-listviews/

这实际上将标题与数据项分开并正确使用 convertView,这与我迄今为止在以前的应用程序中使用的解决方案不同。

于 2013-06-07T12:45:12.443 回答