waw*_*los 15 android listview android-layout navigation-drawer
我用新的导航抽屉Object创建了一个项目.
我想自定义菜单的布局,添加另一个对象,如TextView,ImageView ......并且对于开始,我想修改默认布局,该布局仅由一个列表视图组成,通过在其中添加2或3个标题列表显示.
今天,我试图使用"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);
}
}
Run Code Online (Sandbox Code Playgroud)
和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" />
Run Code Online (Sandbox Code Playgroud)
您可以ListView通过教您ListAdapter返回标题行和详细行来以与在任何其他标题中添加标题相同的方式执行此操作.在较低级别,这涉及覆盖类似getViewTypeCount()和getItemViewType()在您的方法ListAdapter,以及getView()知道行类型之间的差异.或者,使用现有的高级实现,如https://github.com/emilsjolander/StickyListHeaders或http://code.google.com/p/android-amazing-listview/或搜索时找到的任何其他实现android listview headers.
其他答案都是正确的.
我找到了一个非常好的例子来自定义视图以包含两种类型的项目:menu section和menu item.当然,您可以将其更改为您想要的任何内容.
该示例还包含抽象活动类的实现,每个具有导航抽屉的活动都继承自该类.
http://www.michenux.net/android-navigation-drawer-748.html
| 归档时间: |
|
| 查看次数: |
35536 次 |
| 最近记录: |