我刚刚按照本教程http://www.tutecentral.com/android-custom-navigation-drawer/编写了一个使用导航抽屉的应用程序。如果您查看该链接,您会看到有两个部分:一个是他制作应用程序,另一个是自定义它(他添加了一个微调器和一些标题)。我想只添加标题但我无法弄清楚如何。 我还检查了谷歌的一些答案,但还没找到一个,甚至不在这里...... 有人可以开导我吗?
这是MainActivity.java:
public class MainActivity extends Activity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
CustomDrawerAdapter adapter;
List<DrawerItem> dataList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initializing
dataList = new ArrayList<DrawerItem>();
mTitle = mDrawerTitle = getTitle();
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
GravityCompat.START);
// Add Drawer Item to dataList
dataList.add(new DrawerItem("Message", R.drawable.ic_action_email));
dataList.add(new DrawerItem("Likes", R.drawable.ic_action_good));
dataList.add(new DrawerItem("Games", R.drawable.ic_action_gamepad));
dataList.add(new DrawerItem("Lables", R.drawable.ic_action_labels));
dataList.add(new DrawerItem("Search", R.drawable.ic_action_search));
dataList.add(new DrawerItem("Cloud", R.drawable.ic_action_cloud));
dataList.add(new DrawerItem("Camara", R.drawable.ic_action_camera));
dataList.add(new DrawerItem("Video", R.drawable.ic_action_video));
dataList.add(new DrawerItem("Groups", R.drawable.ic_action_group));
dataList.add(new DrawerItem("Import & Export",
R.drawable.ic_action_import_export));
dataList.add(new DrawerItem("About", R.drawable.ic_action_about));
dataList.add(new DrawerItem("Settings", R.drawable.ic_action_settings));
dataList.add(new DrawerItem("Help", R.drawable.ic_action_help));
adapter = new CustomDrawerAdapter(this, R.layout.custom_drawer_item,
dataList);
mDrawerList.setAdapter(adapter);
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, R.string.drawer_open,
R.string.drawer_close) {
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);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
public void SelectItem(int possition) {
Fragment fragment = null;
Bundle args = new Bundle();
switch (possition) {
case 0:
fragment = new FragmentOne();
args.putString(FragmentOne.ITEM_NAME, dataList.get(possition)
.getItemName());
args.putInt(FragmentOne.IMAGE_RESOURCE_ID, dataList.get(possition)
.getImgResID());
break;
case 1:
fragment = new FragmentTwo();
args.putString(FragmentTwo.ITEM_NAME, dataList.get(possition)
.getItemName());
args.putInt(FragmentTwo.IMAGE_RESOURCE_ID, dataList.get(possition)
.getImgResID());
break;
case 2:
fragment = new FragmentThree();
args.putString(FragmentThree.ITEM_NAME, dataList.get(possition)
.getItemName());
args.putInt(FragmentThree.IMAGE_RESOURCE_ID, dataList.get(possition)
.getImgResID());
break;
case 3:
fragment = new FragmentOne();
args.putString(FragmentOne.ITEM_NAME, dataList.get(possition)
.getItemName());
args.putInt(FragmentOne.IMAGE_RESOURCE_ID, dataList.get(possition)
.getImgResID());
break;
case 4:
fragment = new FragmentTwo();
args.putString(FragmentTwo.ITEM_NAME, dataList.get(possition)
.getItemName());
args.putInt(FragmentTwo.IMAGE_RESOURCE_ID, dataList.get(possition)
.getImgResID());
break;
case 5:
fragment = new FragmentThree();
args.putString(FragmentThree.ITEM_NAME, dataList.get(possition)
.getItemName());
args.putInt(FragmentThree.IMAGE_RESOURCE_ID, dataList.get(possition)
.getImgResID());
break;
case 6:
fragment = new FragmentOne();
args.putString(FragmentOne.ITEM_NAME, dataList.get(possition)
.getItemName());
args.putInt(FragmentOne.IMAGE_RESOURCE_ID, dataList.get(possition)
.getImgResID());
break;
case 7:
fragment = new FragmentTwo();
args.putString(FragmentTwo.ITEM_NAME, dataList.get(possition)
.getItemName());
args.putInt(FragmentTwo.IMAGE_RESOURCE_ID, dataList.get(possition)
.getImgResID());
break;
case 8:
fragment = new FragmentThree();
args.putString(FragmentThree.ITEM_NAME, dataList.get(possition)
.getItemName());
args.putInt(FragmentThree.IMAGE_RESOURCE_ID, dataList.get(possition)
.getImgResID());
break;
case 9:
fragment = new FragmentOne();
args.putString(FragmentOne.ITEM_NAME, dataList.get(possition)
.getItemName());
args.putInt(FragmentOne.IMAGE_RESOURCE_ID, dataList.get(possition)
.getImgResID());
break;
case 10:
fragment = new FragmentTwo();
args.putString(FragmentTwo.ITEM_NAME, dataList.get(possition)
.getItemName());
args.putInt(FragmentTwo.IMAGE_RESOURCE_ID, dataList.get(possition)
.getImgResID());
break;
case 11:
fragment = new FragmentThree();
args.putString(FragmentThree.ITEM_NAME, dataList.get(possition)
.getItemName());
args.putInt(FragmentThree.IMAGE_RESOURCE_ID, dataList.get(possition)
.getImgResID());
break;
case 12:
fragment = new FragmentOne();
args.putString(FragmentOne.ITEM_NAME, dataList.get(possition)
.getItemName());
args.putInt(FragmentOne.IMAGE_RESOURCE_ID, dataList.get(possition)
.getImgResID());
break;
default:
break;
}
fragment.setArguments(args);
FragmentManager frgManager = getFragmentManager();
frgManager.beginTransaction().replace(R.id.content_frame, fragment)
.commit();
mDrawerList.setItemChecked(possition, true);
setTitle(dataList.get(possition).getItemName());
mDrawerLayout.closeDrawer(mDrawerList);
}
@Override
public void setTitle(CharSequence title) {
mTitle = title;
getActionBar().setTitle(mTitle);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggles
mDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// The action bar home/up action should open or close the drawer.
// ActionBarDrawerToggle will take care of this.
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return false;
}
private class DrawerItemClickListener implements
ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
SelectItem(position);
}
}
}
CustomDrawerAdapter.java:
public class CustomDrawerAdapter extends ArrayAdapter<DrawerItem> {
Context context;
List<DrawerItem> drawerItemList;
int layoutResID;
public CustomDrawerAdapter(Context context, int layoutResourceID,
List<DrawerItem> listItems) {
super(context, layoutResourceID, listItems);
this.context = context;
this.drawerItemList = listItems;
this.layoutResID = layoutResourceID;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
DrawerItemHolder drawerHolder;
View view = convertView;
if (view == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
drawerHolder = new DrawerItemHolder();
view = inflater.inflate(layoutResID, parent, false);
drawerHolder.ItemName = (TextView) view
.findViewById(R.id.drawer_itemName);
drawerHolder.icon = (ImageView) view.findViewById(R.id.drawer_icon);
view.setTag(drawerHolder);
} else {
drawerHolder = (DrawerItemHolder) view.getTag();
}
DrawerItem dItem = (DrawerItem) this.drawerItemList.get(position);
drawerHolder.icon.setImageDrawable(view.getResources().getDrawable(
dItem.getImgResID()));
drawerHolder.ItemName.setText(dItem.getItemName());
return view;
}
private static class DrawerItemHolder {
TextView ItemName;
ImageView icon;
}
}
DrawerItem.java:
public class DrawerItem {
String ItemName;
int imgResID;
public DrawerItem(String itemName, int imgResID) {
super();
ItemName = itemName;
this.imgResID = imgResID;
}
public String getItemName() {
return ItemName;
}
public void setItemName(String itemName) {
ItemName = itemName;
}
public int getImgResID() {
return imgResID;
}
public void setImgResID(int imgResID) {
this.imgResID = imgResID;
}
}
答案 0 :(得分:1)
您要做的是为普通项目和标题项目创建2个不同的布局文件。例如:
<强> list_header.xml:强>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"/>
<强> list_item.xml 强>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/imageView"
android:textSize="20sp"/>
</RelativeLayout>
然后让您的适配器需要覆盖一些方法,以便识别2种不同类型并为它们使用不同的convertViews
:
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
if (getItem(position).getImgResID() == 0) {
// Header type = 1
return 1;
} else {
// Normal item types = 0
return 0;
}
}
最后所有的魔法都发生在getView
方法中。让它检查特定位置的项目是否是标题,并使特定布局膨胀:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
DrawerItemHolder drawerHolder;
DrawerItem dItem = getItem(position);
if (convertView == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
drawerHolder = new DrawerItemHolder();
if (dItem.getImgResID() == 0) {
// Inflate header layout
convertView = inflater.inflate(R.layout.list_header, parent, false);
drawerHolder.ItemName = (TextView) convertView.findViewById(R.id.textView);
// ...
} else {
// Inflate normal layout
convertView = inflater.inflate(R.layout.list_item, parent, false);
drawerHolder.ItemName = (TextView) convertView.findViewById(R.id.textView);
drawerHolder.icon = (ImageView) convertView.findViewById(R.id.imageView);
}
convertView.setTag(drawerHolder);
} else {
drawerHolder = (DrawerItemHolder) convertView.getTag();
}
drawerHolder.ItemName = dItem.getItemName();
// ...
return convertView;
}
然后您的商品将如下所示:
DrawerItem("Header1", 0, true);
DrawerItem("Message", R.drawable.ic_action_email);
DrawerItem("Message", R.drawable.ic_action_email);
DrawerItem("Header2", 0, true);
DrawerItem("Message", R.drawable.ic_action_email);
注意:header是一个没有drawable的简单项(img res id == 0)。