Android Navigation Drawer onItemClick 位置 0 永远不会被读取
Android Navigation Drawer onItemClick position 0 is never read
这是我的导航抽屉的样子:
从技术上讲,家的位置 = 0,管家 = 1...依此类推
但是,当我单击“主页”时,onItemClick 中返回的位置是 1,它会打开内务处理 activity。当我点击 Housekeeping 时会发生同样的事情,它会打开 Laundry。所以它returns位置=点击位置+1.
这是我的 BaseActivity 的代码(由其他想要使用 Navigation Drawer 的活动扩展)
public class BaseActivity extends AppCompatActivity {
protected FrameLayout frameLayout;
protected ListView mDrawerList;
protected String[] navItemsTitles = {"Home","Housekeeping","Laundry","Food","Reservations","Concierge","Hotel Info"};
protected ArrayList<Items> navItemsArray;
protected static int position;
private DrawerLayout mDrawerLayout;
private static boolean isLaunched = true;
private ActionBarDrawerToggle actionBarDrawerToggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.base_layout);
frameLayout = (FrameLayout) findViewById(R.id.content_frame);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
navItemsArray = new ArrayList<Items>();
Log.i("NAV", "---- navItemsTitles: " + Arrays.toString(navItemsTitles));
navItemsArray.add(new Items("Home", R.drawable.home_icon));
navItemsArray.add(new Items("Housekeeping", R.drawable.housekeeping_icon));
navItemsArray.add(new Items("Laundry", R.drawable.laundry_icon));
navItemsArray.add(new Items("Food", R.drawable.food_icon));
navItemsArray.add(new Items("Reservations", R.drawable.reservation_icon));
navItemsArray.add(new Items("Concierge", R.drawable.concierge_icon));
navItemsArray.add(new Items("Hotel Info", R.drawable.info_icon));
//Adding header on list view
View header = (View) getLayoutInflater().inflate(R.layout.list_view_header_layout, null);
mDrawerList.addHeaderView(header);
// Set the adapter for the list view
mDrawerList.setAdapter(new NavigationDrawerListAdapter(this, navItemsArray));
//Set the list's on-item click listener
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
Log.i("NAV", "---------- onItemClick position: " + position);
openActivity(position);
}
});
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.mipmap.ic_launcher_round);
getSupportActionBar().setHomeButtonEnabled(true);
actionBarDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
R.string.open_drawer,
R.string.close_drawer
) {
@Override
public void onDrawerClosed(View drawerView) {
Log.i("NAV", "---- onDrawerClosed position: " + position);
getSupportActionBar().setTitle(navItemsTitles[position]);
invalidateOptionsMenu();
super.onDrawerClosed(drawerView);
}
@Override
public void onDrawerOpened(View drawerView) {
getSupportActionBar().setTitle(getString(R.string.app_name));
invalidateOptionsMenu();
super.onDrawerOpened(drawerView);
}
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, slideOffset);
}
@Override
public void onDrawerStateChanged(int newState) {
super.onDrawerStateChanged(newState);
}
};
mDrawerLayout.addDrawerListener(actionBarDrawerToggle);
}
protected void openActivity(int position) {
mDrawerLayout.closeDrawer(mDrawerList);
BaseActivity.position = position;
Log.i("NAV", "------ Selected Item Position::"+position);
switch (position) {
case 0:
startActivity(new Intent(BaseActivity.this, HomeActivity.class));
break;
case 1:
startActivity(new Intent(BaseActivity.this, HousekeepingActivity.class));
break;
case 2:
startActivity(new Intent(BaseActivity.this, LaundryActivity.class));
break;
case 3:
startActivity(new Intent(BaseActivity.this, FoodActivity.class));
break;
case 4:
startActivity(new Intent(BaseActivity.this, ReservationsActivity.class));
break;
case 5:
startActivity(new Intent(BaseActivity.this, ConciergeActivity.class));
break;
case 6:
startActivity(new Intent(BaseActivity.this, HotelInfoActivity.class));
break;
default:
break;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//code
}
@Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
//code
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
//code
}
@Override
public void onBackPressed() {
//code
}
}
这是来自Logcat的信息:
I: ---- navItemsTitles: [Home, Housekeeping, Laundry, Food, Reservations, Concierge, Hotel Info]
I: ---------- onItemClick position: 1
I: ------ Selected Item Position::1
I: ---- navItemsTitles: [Home, Housekeeping, Laundry, Food, Reservations, Concierge, Hotel Info]
这是我的 NavigationDrawerListAdapter
public class NavigationDrawerListAdapter extends BaseAdapter {
private Context context;
private ArrayList<Items> itemsArray;
public NavigationDrawerListAdapter(Context context, ArrayList<Items> itemsArray) {
this.context = context;
this.itemsArray = itemsArray;
}
private class ViewHolder {
TextView itemName;
ImageView itemIcon;
}
@Override
public int getCount() {
return itemsArray.size();
}
@Override
public Object getItem(int position) {
return itemsArray.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if(convertView == null) {
convertView = inflater.inflate(R.layout.drawer_list_item, null);
holder = new ViewHolder();
holder.itemName = (TextView) convertView.findViewById(R.id.item_name_txtview);
holder.itemIcon = (ImageView) convertView.findViewById(R.id.item_icon_imgview);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Items item = (Items) getItem(position);
holder.itemName.setText(item.getItemName());
holder.itemIcon.setBackgroundResource((int)item.getIconId());
return convertView;
}
}
我做错了什么?所有项目都按给定的方式进入,所以每个人都在正确的位置,但是当第一个项目(在位置 0)被点击时,它 returns 位置 == 1.
当您必须在抽屉菜单上设置 header视图时,header 只是第 0 个位置。所以你的主菜单 return 只有第一个位置。
所以你必须这样改
switch (position) {
case 1:
startActivity(new Intent(BaseActivity.this, HomeActivity.class));
break;
case 2:
startActivity(new Intent(BaseActivity.this, HousekeepingActivity.class));
break;
case 3:
startActivity(new Intent(BaseActivity.this, LaundryActivity.class));
break;
case 4:
startActivity(new Intent(BaseActivity.this, FoodActivity.class));
break;
case 5:
startActivity(new Intent(BaseActivity.this, ReservationsActivity.class));
break;
case 6:
startActivity(new Intent(BaseActivity.this, ConciergeActivity.class));
break;
case 7:
startActivity(new Intent(BaseActivity.this, HotelInfoActivity.class));
break;
default:
break;
}
@codeinprogress 试试这行代码
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
Log.i("NAV", "---------- onItemClick position: " + position);
**openActivity(position-1);**
}
});
这是我的导航抽屉的样子:
从技术上讲,家的位置 = 0,管家 = 1...依此类推
但是,当我单击“主页”时,onItemClick 中返回的位置是 1,它会打开内务处理 activity。当我点击 Housekeeping 时会发生同样的事情,它会打开 Laundry。所以它returns位置=点击位置+1.
这是我的 BaseActivity 的代码(由其他想要使用 Navigation Drawer 的活动扩展)
public class BaseActivity extends AppCompatActivity {
protected FrameLayout frameLayout;
protected ListView mDrawerList;
protected String[] navItemsTitles = {"Home","Housekeeping","Laundry","Food","Reservations","Concierge","Hotel Info"};
protected ArrayList<Items> navItemsArray;
protected static int position;
private DrawerLayout mDrawerLayout;
private static boolean isLaunched = true;
private ActionBarDrawerToggle actionBarDrawerToggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.base_layout);
frameLayout = (FrameLayout) findViewById(R.id.content_frame);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
navItemsArray = new ArrayList<Items>();
Log.i("NAV", "---- navItemsTitles: " + Arrays.toString(navItemsTitles));
navItemsArray.add(new Items("Home", R.drawable.home_icon));
navItemsArray.add(new Items("Housekeeping", R.drawable.housekeeping_icon));
navItemsArray.add(new Items("Laundry", R.drawable.laundry_icon));
navItemsArray.add(new Items("Food", R.drawable.food_icon));
navItemsArray.add(new Items("Reservations", R.drawable.reservation_icon));
navItemsArray.add(new Items("Concierge", R.drawable.concierge_icon));
navItemsArray.add(new Items("Hotel Info", R.drawable.info_icon));
//Adding header on list view
View header = (View) getLayoutInflater().inflate(R.layout.list_view_header_layout, null);
mDrawerList.addHeaderView(header);
// Set the adapter for the list view
mDrawerList.setAdapter(new NavigationDrawerListAdapter(this, navItemsArray));
//Set the list's on-item click listener
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
Log.i("NAV", "---------- onItemClick position: " + position);
openActivity(position);
}
});
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.mipmap.ic_launcher_round);
getSupportActionBar().setHomeButtonEnabled(true);
actionBarDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
R.string.open_drawer,
R.string.close_drawer
) {
@Override
public void onDrawerClosed(View drawerView) {
Log.i("NAV", "---- onDrawerClosed position: " + position);
getSupportActionBar().setTitle(navItemsTitles[position]);
invalidateOptionsMenu();
super.onDrawerClosed(drawerView);
}
@Override
public void onDrawerOpened(View drawerView) {
getSupportActionBar().setTitle(getString(R.string.app_name));
invalidateOptionsMenu();
super.onDrawerOpened(drawerView);
}
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, slideOffset);
}
@Override
public void onDrawerStateChanged(int newState) {
super.onDrawerStateChanged(newState);
}
};
mDrawerLayout.addDrawerListener(actionBarDrawerToggle);
}
protected void openActivity(int position) {
mDrawerLayout.closeDrawer(mDrawerList);
BaseActivity.position = position;
Log.i("NAV", "------ Selected Item Position::"+position);
switch (position) {
case 0:
startActivity(new Intent(BaseActivity.this, HomeActivity.class));
break;
case 1:
startActivity(new Intent(BaseActivity.this, HousekeepingActivity.class));
break;
case 2:
startActivity(new Intent(BaseActivity.this, LaundryActivity.class));
break;
case 3:
startActivity(new Intent(BaseActivity.this, FoodActivity.class));
break;
case 4:
startActivity(new Intent(BaseActivity.this, ReservationsActivity.class));
break;
case 5:
startActivity(new Intent(BaseActivity.this, ConciergeActivity.class));
break;
case 6:
startActivity(new Intent(BaseActivity.this, HotelInfoActivity.class));
break;
default:
break;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//code
}
@Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
//code
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
//code
}
@Override
public void onBackPressed() {
//code
}
}
这是来自Logcat的信息:
I: ---- navItemsTitles: [Home, Housekeeping, Laundry, Food, Reservations, Concierge, Hotel Info]
I: ---------- onItemClick position: 1
I: ------ Selected Item Position::1
I: ---- navItemsTitles: [Home, Housekeeping, Laundry, Food, Reservations, Concierge, Hotel Info]
这是我的 NavigationDrawerListAdapter
public class NavigationDrawerListAdapter extends BaseAdapter {
private Context context;
private ArrayList<Items> itemsArray;
public NavigationDrawerListAdapter(Context context, ArrayList<Items> itemsArray) {
this.context = context;
this.itemsArray = itemsArray;
}
private class ViewHolder {
TextView itemName;
ImageView itemIcon;
}
@Override
public int getCount() {
return itemsArray.size();
}
@Override
public Object getItem(int position) {
return itemsArray.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if(convertView == null) {
convertView = inflater.inflate(R.layout.drawer_list_item, null);
holder = new ViewHolder();
holder.itemName = (TextView) convertView.findViewById(R.id.item_name_txtview);
holder.itemIcon = (ImageView) convertView.findViewById(R.id.item_icon_imgview);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Items item = (Items) getItem(position);
holder.itemName.setText(item.getItemName());
holder.itemIcon.setBackgroundResource((int)item.getIconId());
return convertView;
}
}
我做错了什么?所有项目都按给定的方式进入,所以每个人都在正确的位置,但是当第一个项目(在位置 0)被点击时,它 returns 位置 == 1.
当您必须在抽屉菜单上设置 header视图时,header 只是第 0 个位置。所以你的主菜单 return 只有第一个位置。
所以你必须这样改
switch (position) {
case 1:
startActivity(new Intent(BaseActivity.this, HomeActivity.class));
break;
case 2:
startActivity(new Intent(BaseActivity.this, HousekeepingActivity.class));
break;
case 3:
startActivity(new Intent(BaseActivity.this, LaundryActivity.class));
break;
case 4:
startActivity(new Intent(BaseActivity.this, FoodActivity.class));
break;
case 5:
startActivity(new Intent(BaseActivity.this, ReservationsActivity.class));
break;
case 6:
startActivity(new Intent(BaseActivity.this, ConciergeActivity.class));
break;
case 7:
startActivity(new Intent(BaseActivity.this, HotelInfoActivity.class));
break;
default:
break;
}
@codeinprogress 试试这行代码
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
Log.i("NAV", "---------- onItemClick position: " + position);
**openActivity(position-1);**
}
});