我的应用中的导航抽屉有问题。我无法获得“OnItemClick” - 我的抽屉列表中的行项目事件。
我的应用包含一个带有一些标签的Actionbar。所以我的活动扩展到了“ActionBarActivity”。
这是我的代码。我希望有人可以帮助我。
public class Hauptmenue_extended extends ActionBarActivity implements
ListView.OnItemClickListener,
android.content.DialogInterface.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hauptmenue_extended);
try {
ViewPager pager = (ViewPager) this.findViewById(R.id.pager);
pager.setAdapter(new SampleAdapter(this,
getSupportFragmentManager()));
actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Bereitet die ActionBar auf den Navigation Drawer vor
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
mDrawerLayout = (DrawerLayout) findViewById(R.id.fragView);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
GravityCompat.START);
drawerTitles = getResources().getStringArray(
R.array.drawerTitles_array);
drawerSubtitles = getResources().getStringArray(
R.array.drawerSubtitles_array);
drawerIcons = new int[] { R.drawable.icon_buergermeldung,
R.drawable.icon_sprechblase,
R.drawable.icon_action_settings_dark,
R.drawable.icon_user_chat, R.drawable.icon_haus,
R.drawable.icon_contact, R.drawable.icon_calendar,
R.drawable.icon_search };
MenuListAdapter mMenuAdapter = new MenuListAdapter(this,
drawerTitles, drawerSubtitles, drawerIcons);
mDrawerList.setAdapter(mMenuAdapter);
mDrawerList.setOnItemClickListener(this);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, R.string.drawer_open,
R.string.drawer_close) {
public void onDrawerClosed(View view) {
getSupportActionBar().setTitle(mTitle);
supportInvalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getSupportActionBar().setTitle(R.string.app_name);
supportInvalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
mPullToRefreshAttacher = PullToRefreshAttacher.get(this);
Tab tabB = actionBar.newTab();
tabB.setText("Home");
tabB.setIcon(R.drawable.icon_home);
tabB.setTabListener(new TabListener<Startmenue_activity>(this,
"Start", Startmenue_activity.class));
actionBar.addTab(tabB);
Tab tabA = actionBar.newTab();
tabA.setText("");
tabA.setIcon(R.drawable.icon_nachrichten_sel);
tabA.setTabListener(new TabListener<Nachrichten_activity>(this,
"Nachrichten", Nachrichten_activity.class));
actionBar.addTab(tabA);
Tab tabC = actionBar.newTab();
tabC.setText("");
tabC.setIcon(R.drawable.icon_favoriten);
tabC.setTabListener(new TabListener<Favoriten_activity>(this,
"Favoriten", Favoriten_activity.class));
actionBar.addTab(tabC);
this.conEinst = new conEinstellungen(getBaseContext());
} catch (Exception ex) {
ex.printStackTrace();
// Log.d("adkomm", );
// HelperClassAlertDialog.zeigeInfoDialog(this, "Error...",
// ex.getMessage(), "");
}
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Log.d("app", "OnItemClick Drawer");
}
永远不会调用方法“OnItemClick”。 :(其他一切都很完美。
这是.XML:
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent" > <android.support.v4.view.PagerTabStrip android:id="@+id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="top" /> </android.support.v4.view.ViewPager> <FrameLayout android:id="@+id/main" android:layout_width="match_parent" android:layout_height="match_parent" > </FrameLayout> <ListView android:id="@+id/left_drawer" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" android:background="#fff" android:choiceMode="singleChoice" android:clickable="true" android:descendantFocusability="blocksDescendants" android:divider="@android:color/transparent" android:dividerHeight="0dp" />
修改
我的ListItem.xml如下所示:
<ImageView android:id="@+id/icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:adjustViewBounds="true" android:contentDescription="Entry-Icon" android:src="@drawable/adkomm_logo" /> <TextView android:id="@+id/textView_balken_drawer" android:layout_width="fill_parent" android:layout_height="5dp" android:layout_alignParentBottom="false" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_below="@+id/subtitle" android:layout_marginTop="5dp" android:background="#33B5E5" /> <TextView android:id="@+id/title" style="?spinnerDropDownItemStyle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_marginLeft="15dp" android:layout_marginTop="10dp" android:layout_toRightOf="@+id/icon" android:ellipsize="end" android:singleLine="true" android:text="test" android:textColor="#33B5E5" android:textStyle="bold" /> <TextView android:id="@+id/subtitle" style="?spinnerDropDownItemStyle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/title" android:layout_below="@+id/title" android:text="tester" android:textSize="13dp" />
修改
这是我的“MenuList_Adapter”:
public class MenuListAdapter extends BaseAdapter {
private String[] mTitle;
private String[] mSubTitle;
private int[] mIcon;
private LayoutInflater inflater;
private Hauptmenue_extended mact;
public MenuListAdapter(String[] pTitle, String[] pSubtitle, int[] pIcon,
Hauptmenue_extended act) {
mTitle = pTitle;
mSubTitle = pSubtitle;
mIcon = pIcon;
this.mact = act;
inflater = (LayoutInflater) mact
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = inflater.inflate(R.layout.listitem, null);
itemView.setOnClickListener((OnClickListener) this.mact);
TextView txtTitle = (TextView) itemView.findViewById(R.id.title);
TextView txtSubTitle = (TextView) itemView.findViewById(R.id.subtitle);
ImageView imgIcon = (ImageView) itemView.findViewById(R.id.icon);
txtTitle.setText(mTitle[position]);
txtSubTitle.setText(mSubTitle[position]);
imgIcon.setImageResource(mIcon[position]);
return itemView;
}
@Override
public int getCount() {
return mTitle.length;
}
@Override
public Object getItem(int position) {
return mTitle[position];
}
@Override
public long getItemId(int position) {
return position;
}
}
编辑:
我想问题是,导航抽屉是否与ActionBar标签一起使用。 :(