多级列表视图从一个项目打开两个活动。 Android 工作室 | java
Multilevel list view opening two activities from one item. Android Studio | java
我是初学者,我在导航视图中创建了一个 3 级多级列表视图,其中有项目和组项目,每个项目打开一个新的 activity。图片附在下面。
image
问题是有一个名为 Tomorrowland 的组项目,它还有两个项目,但在点击 Tomorrowland Winners 时,它会在堆栈中打开两个活动。就像 Tomorrowland Winners 应该打开 activity A 但它打开 Activity B 并且当在后面按下 Activity B 时 activity A 被打开。
Activity B 在点击获胜者时打开。
首页Activity代码:
public class HomeActivity1 extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
@BindView(R.id.multi_home)
MultiLevelListView multiLevelListView;
@BindView(R.id.drawer_layout)
DrawerLayout drawer;
@BindView(R.id.cvSlider)
CardView cvSlider;
@BindView(R.id.autoSlider)
SliderView autoSlider;
@BindView(R.id.rvClub)
RecyclerView rvClub;
@BindView(R.id.rvProjects)
RecyclerView rvProjects;
Toolbar toolbar;
NavigationView nav_view_home;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
ButterKnife.bind(this);
setSupportActionBar(toolbar);
toolbar = findViewById(R.id.appbarToolbar);
nav_view_home = findViewById(R.id.nav_view_home);
toolbar.setTitle(R.string.app_name);
toolbar.setTitleTextColor(getResources().getColor(R.color.white));
confMenu();
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.getDrawerArrowDrawable().setColor(getResources().getColor(R.color.white));
toggle.setDrawerSlideAnimationEnabled(true);
toggle.setDrawerIndicatorEnabled(true);
toggle.syncState();
toggle.getDrawerArrowDrawable().setColor(getResources().getColor(R.color.white));
nav_view_home.setNavigationItemSelectedListener(this);
displaySelectedScreen("HOME");
}
private OnItemClickListener mOnItemClickListener = new OnItemClickListener() {
private void showItemDescription(Object object, ItemInfo itemInfo) {
if (((BaseItem) object).getName().contains("Home")) {
displaySelectedScreen("HOME");
}
if (((BaseItem) object).getName().contains("We Believe")) {
displaySelectedScreen("WE BELIEVE");
}
if (((BaseItem) object).getName().contains("Workshop")) {
displaySelectedScreen("WORKSHOPS");
}
if (((BaseItem) object).getName().contains("Events")) {
displaySelectedScreen("EVENTS");
}
if (((BaseItem) object).getName().contains("Projects")) {
displaySelectedScreen("PROJECTS");
}
if (((BaseItem) object).getName().contains("Tomorrowland Winners")) {
displaySelectedScreen("TOMORROWLAND WINNERS");
}
if (((BaseItem) object).getName().contains("Tomorrowland Jr.")) {
displaySelectedScreen("TOMORROWLAND JR.");
}
if (((BaseItem) object).getName().contains("Winners")) {
displaySelectedScreen("SPELL BE WINNERS");
}
if (((BaseItem) object).getName().contains("Video Gallery")) {
displaySelectedScreen("VIDEO GALLERY");
}
if (((BaseItem) object).getName().contains("Blog")) {
displaySelectedScreen("BLOG");
}
if (((BaseItem) object).getName().contains("Club")) {
displaySelectedScreen("CLUBS");
}
if (((BaseItem) object).getName().contains("Feed")) {
displaySelectedScreen("FEED");
}
if (((BaseItem) object).getName().contains("Login")) {
displaySelectedScreen("LOGIN");
}
if (((BaseItem) object).getName().contains("Career")) {
displaySelectedScreen("CAREER");
}
if (((BaseItem) object).getName().contains("Inquiry")) {
displaySelectedScreen("INQUIRY");
}
}
@Override
public void onItemClicked(MultiLevelListView parent, View view, Object item, ItemInfo itemInfo) {
showItemDescription(item, itemInfo);
}
@Override
public void onGroupItemClicked(MultiLevelListView parent, View view, Object item, ItemInfo itemInfo) {
showItemDescription(item, itemInfo);
}
};
private void confMenu() {
// custom ListAdapter
ListAdapter listAdapter = new ListAdapter();
multiLevelListView.setAdapter(listAdapter);
multiLevelListView.setOnItemClickListener(mOnItemClickListener);
listAdapter.setDataItems(HomeCustomDataProvider.getInitialItems());
}
@Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
private void displaySelectedScreen(String itemName) {
switch (itemName) {
case "HOME":
drawer.close();
break;
case "WE BELIEVE":
Intent intent = new Intent(HomeActivity1.this,WeBelieveActivity.class);
startActivity(intent);
break;
case "WORKSHOPS":
Intent intent1 = new Intent(HomeActivity1.this,WorkshopsActivity.class);
startActivity(intent1);
break;
case "EVENTS":
Intent intent2 = new Intent(HomeActivity1.this,EventsActivity.class);
startActivity(intent2);
break;
case "PROJECTS":
Intent intent3 = new Intent(HomeActivity1.this,ProjectsActivity.class);
startActivity(intent3);
break;
case "TOMORROWLAND WINNERS":
Intent intent4 = new Intent(HomeActivity1.this, TomorrowLandWinnersActivity.class);
startActivity(intent4);
break;
case "TOMORROWLAND JR.":
Intent intent5 = new Intent(HomeActivity1.this, TomorrowLandJrWinnersActivity.class);
startActivity(intent5);
break;
case "SPELL BE WINNERS":
Intent intent6 = new Intent(HomeActivity1.this,SpellBeWinnersActivity.class);
startActivity(intent6);
break;
case "VIDEO GALLERY":
Intent intent7 = new Intent(HomeActivity1.this,VideoGalleryActivity.class);
startActivity(intent7);
break;
case "BLOG":
Intent intent8 = new Intent(HomeActivity1.this,BlogActivity.class);
startActivity(intent8);
break;
case "CLUBS":
Intent intent9 = new Intent(HomeActivity1.this,ClubActivity.class);
startActivity(intent9);
break;
case "FEED":
Intent intent10 = new Intent(HomeActivity1.this,FeedActivity.class);
startActivity(intent10);
break;
case "LOGIN":
Intent intent11 = new Intent(HomeActivity1.this,LoginActivity.class);
startActivity(intent11);
break;
case "CAREER":
Intent intent12 = new Intent(HomeActivity1.this,CareerActivity.class);
startActivity(intent12);
break;
case "INQUIRY":
Intent intent13 = new Intent(HomeActivity1.this,InquiryActivity.class);
startActivity(intent13);
break;
}
drawer.close();
}
@Override
public boolean onNavigationItemSelected(MenuItem item) {
//calling the method display selected screen and passing the id of selected menu
displaySelectedScreen(String.valueOf(item.getItemId()));
//make this method blank
return true;
}
private class ListAdapter extends MultiLevelListAdapter {
@Override
public List<?> getSubObjects(Object object) {
return HomeCustomDataProvider.getSubItems((BaseItem) object);
}
@Override
public boolean isExpandable(Object object) {
return HomeCustomDataProvider.isExpandable((BaseItem) object);
}
@SuppressLint("InflateParams")
@Override
public View getViewForObject(Object object, View convertView, ItemInfo itemInfo) {
ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = LayoutInflater.from(HomeActivity1.this).inflate(R.layout.makers_menu_item, null);
viewHolder.nameView = convertView.findViewById(R.id.dataItemName);
viewHolder.arrowView = convertView.findViewById(R.id.dataItemArrow);
viewHolder.levelBeamView = convertView.findViewById(R.id.dataItemLevelBeam);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.nameView.setText(((BaseItem) object).getName());
if (itemInfo.isExpandable()) {
viewHolder.arrowView.setVisibility(View.VISIBLE);
viewHolder.arrowView.setImageResource(itemInfo.isExpanded() ?
R.drawable.ic_arrow_up : R.drawable.ic_bottom_arrow);
} else {
viewHolder.arrowView.setVisibility(View.GONE);
}
viewHolder.levelBeamView.setLevel(itemInfo.getLevel());
return convertView;
}
private class ViewHolder {
TextView nameView;
ImageView arrowView;
LevelBeamView levelBeamView;
}
}
}
HomeCustomDataProvoder 代码如下:
public class HomeCustomDataProvider {
private static final int MAX_LEVELS = 3;
private static final int LEVEL_1 = 1;
private static final int LEVEL_2 = 2;
private static final int LEVEL_3 = 3;
private static List<BaseItem> mMenu = new ArrayList<>();
Context context;
public static List<BaseItem> getInitialItems() {
List<BaseItem> rootMenu = new ArrayList<>();
rootMenu.add(new Item("Home"));
rootMenu.add(new Item("We Believe"));
rootMenu.add(new GroupItem("Echo"));
rootMenu.add(new Item("Video Gallery"));
rootMenu.add(new Item("Blog"));
rootMenu.add(new Item("Clubs"));
rootMenu.add(new Item("Feeds"));
rootMenu.add(new Item("Login"));
rootMenu.add(new GroupItem("Connect"));
return rootMenu;
}
public static List<BaseItem> getSubItems(BaseItem baseItem) {
List<BaseItem> result = new ArrayList<>();
int level1 = ((GroupItem) baseItem).getLevel() + 1;
int level2 = ((GroupItem) baseItem).getLevel() + 2;
String menuItem = baseItem.getName();
GroupItem groupItem = (GroupItem) baseItem;
if (groupItem.getLevel() >= MAX_LEVELS) {
return null;
}
if (level1 == LEVEL_1) {
switch (menuItem.toUpperCase()) {
case "ECHO":
result = getListEcho();
break;
case "CONNECT":
result = getListConnect();
break;
}
}
if (level2 == LEVEL_2) {
switch (menuItem.toUpperCase()) {
case "TOMORROWLAND":
result = getListTomorrowland();
break;
case "SPELL BE":
result = getListSpellBe();
break;
}
}
return result;
}
public static boolean isExpandable(BaseItem baseItem) {
return baseItem instanceof GroupItem;
}
private static List<BaseItem> getListEcho() {
List<BaseItem> list = new ArrayList<>();
list.add(new Item("Workshops"));
list.add(new Item("Events"));
list.add(new Item("Projects"));
list.add(new GroupItem("Tomorrowland"));
list.add(new GroupItem("Spell Be"));
return list;
}
private static List<BaseItem> getListTomorrowland() {
List<BaseItem> list = new ArrayList<>();
list.add(new Item("Tomorrowland Winners"));
list.add(new Item("Tomorrowland Jr."));
return list;
}
private static List<BaseItem> getListSpellBe() {
List<BaseItem> list = new ArrayList<>();
list.add(new Item("Winners"));
return list;
}
private static List<BaseItem> getListConnect() {
List<BaseItem> list = new ArrayList<>();
list.add(new Item("Career"));
list.add(new Item("Inquiry"));
return list;
}
}
如有任何帮助,我们将不胜感激。提前致谢。
而不是使用 contains
,您应该使用 equals
来检查所有条件,在您的情况下,TomorrowLand Winners
包含 Winners
并且还包含 Winners
包含 Winners
,因此它同时打开。
//do this for all conditions
if (((BaseItem) object).getName().equals("Tomorrowland Winners")) {
displaySelectedScreen("TOMORROWLAND WINNERS");
}
我是初学者,我在导航视图中创建了一个 3 级多级列表视图,其中有项目和组项目,每个项目打开一个新的 activity。图片附在下面。 image
问题是有一个名为 Tomorrowland 的组项目,它还有两个项目,但在点击 Tomorrowland Winners 时,它会在堆栈中打开两个活动。就像 Tomorrowland Winners 应该打开 activity A 但它打开 Activity B 并且当在后面按下 Activity B 时 activity A 被打开。 Activity B 在点击获胜者时打开。
首页Activity代码:
public class HomeActivity1 extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
@BindView(R.id.multi_home)
MultiLevelListView multiLevelListView;
@BindView(R.id.drawer_layout)
DrawerLayout drawer;
@BindView(R.id.cvSlider)
CardView cvSlider;
@BindView(R.id.autoSlider)
SliderView autoSlider;
@BindView(R.id.rvClub)
RecyclerView rvClub;
@BindView(R.id.rvProjects)
RecyclerView rvProjects;
Toolbar toolbar;
NavigationView nav_view_home;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
ButterKnife.bind(this);
setSupportActionBar(toolbar);
toolbar = findViewById(R.id.appbarToolbar);
nav_view_home = findViewById(R.id.nav_view_home);
toolbar.setTitle(R.string.app_name);
toolbar.setTitleTextColor(getResources().getColor(R.color.white));
confMenu();
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.getDrawerArrowDrawable().setColor(getResources().getColor(R.color.white));
toggle.setDrawerSlideAnimationEnabled(true);
toggle.setDrawerIndicatorEnabled(true);
toggle.syncState();
toggle.getDrawerArrowDrawable().setColor(getResources().getColor(R.color.white));
nav_view_home.setNavigationItemSelectedListener(this);
displaySelectedScreen("HOME");
}
private OnItemClickListener mOnItemClickListener = new OnItemClickListener() {
private void showItemDescription(Object object, ItemInfo itemInfo) {
if (((BaseItem) object).getName().contains("Home")) {
displaySelectedScreen("HOME");
}
if (((BaseItem) object).getName().contains("We Believe")) {
displaySelectedScreen("WE BELIEVE");
}
if (((BaseItem) object).getName().contains("Workshop")) {
displaySelectedScreen("WORKSHOPS");
}
if (((BaseItem) object).getName().contains("Events")) {
displaySelectedScreen("EVENTS");
}
if (((BaseItem) object).getName().contains("Projects")) {
displaySelectedScreen("PROJECTS");
}
if (((BaseItem) object).getName().contains("Tomorrowland Winners")) {
displaySelectedScreen("TOMORROWLAND WINNERS");
}
if (((BaseItem) object).getName().contains("Tomorrowland Jr.")) {
displaySelectedScreen("TOMORROWLAND JR.");
}
if (((BaseItem) object).getName().contains("Winners")) {
displaySelectedScreen("SPELL BE WINNERS");
}
if (((BaseItem) object).getName().contains("Video Gallery")) {
displaySelectedScreen("VIDEO GALLERY");
}
if (((BaseItem) object).getName().contains("Blog")) {
displaySelectedScreen("BLOG");
}
if (((BaseItem) object).getName().contains("Club")) {
displaySelectedScreen("CLUBS");
}
if (((BaseItem) object).getName().contains("Feed")) {
displaySelectedScreen("FEED");
}
if (((BaseItem) object).getName().contains("Login")) {
displaySelectedScreen("LOGIN");
}
if (((BaseItem) object).getName().contains("Career")) {
displaySelectedScreen("CAREER");
}
if (((BaseItem) object).getName().contains("Inquiry")) {
displaySelectedScreen("INQUIRY");
}
}
@Override
public void onItemClicked(MultiLevelListView parent, View view, Object item, ItemInfo itemInfo) {
showItemDescription(item, itemInfo);
}
@Override
public void onGroupItemClicked(MultiLevelListView parent, View view, Object item, ItemInfo itemInfo) {
showItemDescription(item, itemInfo);
}
};
private void confMenu() {
// custom ListAdapter
ListAdapter listAdapter = new ListAdapter();
multiLevelListView.setAdapter(listAdapter);
multiLevelListView.setOnItemClickListener(mOnItemClickListener);
listAdapter.setDataItems(HomeCustomDataProvider.getInitialItems());
}
@Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
private void displaySelectedScreen(String itemName) {
switch (itemName) {
case "HOME":
drawer.close();
break;
case "WE BELIEVE":
Intent intent = new Intent(HomeActivity1.this,WeBelieveActivity.class);
startActivity(intent);
break;
case "WORKSHOPS":
Intent intent1 = new Intent(HomeActivity1.this,WorkshopsActivity.class);
startActivity(intent1);
break;
case "EVENTS":
Intent intent2 = new Intent(HomeActivity1.this,EventsActivity.class);
startActivity(intent2);
break;
case "PROJECTS":
Intent intent3 = new Intent(HomeActivity1.this,ProjectsActivity.class);
startActivity(intent3);
break;
case "TOMORROWLAND WINNERS":
Intent intent4 = new Intent(HomeActivity1.this, TomorrowLandWinnersActivity.class);
startActivity(intent4);
break;
case "TOMORROWLAND JR.":
Intent intent5 = new Intent(HomeActivity1.this, TomorrowLandJrWinnersActivity.class);
startActivity(intent5);
break;
case "SPELL BE WINNERS":
Intent intent6 = new Intent(HomeActivity1.this,SpellBeWinnersActivity.class);
startActivity(intent6);
break;
case "VIDEO GALLERY":
Intent intent7 = new Intent(HomeActivity1.this,VideoGalleryActivity.class);
startActivity(intent7);
break;
case "BLOG":
Intent intent8 = new Intent(HomeActivity1.this,BlogActivity.class);
startActivity(intent8);
break;
case "CLUBS":
Intent intent9 = new Intent(HomeActivity1.this,ClubActivity.class);
startActivity(intent9);
break;
case "FEED":
Intent intent10 = new Intent(HomeActivity1.this,FeedActivity.class);
startActivity(intent10);
break;
case "LOGIN":
Intent intent11 = new Intent(HomeActivity1.this,LoginActivity.class);
startActivity(intent11);
break;
case "CAREER":
Intent intent12 = new Intent(HomeActivity1.this,CareerActivity.class);
startActivity(intent12);
break;
case "INQUIRY":
Intent intent13 = new Intent(HomeActivity1.this,InquiryActivity.class);
startActivity(intent13);
break;
}
drawer.close();
}
@Override
public boolean onNavigationItemSelected(MenuItem item) {
//calling the method display selected screen and passing the id of selected menu
displaySelectedScreen(String.valueOf(item.getItemId()));
//make this method blank
return true;
}
private class ListAdapter extends MultiLevelListAdapter {
@Override
public List<?> getSubObjects(Object object) {
return HomeCustomDataProvider.getSubItems((BaseItem) object);
}
@Override
public boolean isExpandable(Object object) {
return HomeCustomDataProvider.isExpandable((BaseItem) object);
}
@SuppressLint("InflateParams")
@Override
public View getViewForObject(Object object, View convertView, ItemInfo itemInfo) {
ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = LayoutInflater.from(HomeActivity1.this).inflate(R.layout.makers_menu_item, null);
viewHolder.nameView = convertView.findViewById(R.id.dataItemName);
viewHolder.arrowView = convertView.findViewById(R.id.dataItemArrow);
viewHolder.levelBeamView = convertView.findViewById(R.id.dataItemLevelBeam);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.nameView.setText(((BaseItem) object).getName());
if (itemInfo.isExpandable()) {
viewHolder.arrowView.setVisibility(View.VISIBLE);
viewHolder.arrowView.setImageResource(itemInfo.isExpanded() ?
R.drawable.ic_arrow_up : R.drawable.ic_bottom_arrow);
} else {
viewHolder.arrowView.setVisibility(View.GONE);
}
viewHolder.levelBeamView.setLevel(itemInfo.getLevel());
return convertView;
}
private class ViewHolder {
TextView nameView;
ImageView arrowView;
LevelBeamView levelBeamView;
}
}
}
HomeCustomDataProvoder 代码如下:
public class HomeCustomDataProvider {
private static final int MAX_LEVELS = 3;
private static final int LEVEL_1 = 1;
private static final int LEVEL_2 = 2;
private static final int LEVEL_3 = 3;
private static List<BaseItem> mMenu = new ArrayList<>();
Context context;
public static List<BaseItem> getInitialItems() {
List<BaseItem> rootMenu = new ArrayList<>();
rootMenu.add(new Item("Home"));
rootMenu.add(new Item("We Believe"));
rootMenu.add(new GroupItem("Echo"));
rootMenu.add(new Item("Video Gallery"));
rootMenu.add(new Item("Blog"));
rootMenu.add(new Item("Clubs"));
rootMenu.add(new Item("Feeds"));
rootMenu.add(new Item("Login"));
rootMenu.add(new GroupItem("Connect"));
return rootMenu;
}
public static List<BaseItem> getSubItems(BaseItem baseItem) {
List<BaseItem> result = new ArrayList<>();
int level1 = ((GroupItem) baseItem).getLevel() + 1;
int level2 = ((GroupItem) baseItem).getLevel() + 2;
String menuItem = baseItem.getName();
GroupItem groupItem = (GroupItem) baseItem;
if (groupItem.getLevel() >= MAX_LEVELS) {
return null;
}
if (level1 == LEVEL_1) {
switch (menuItem.toUpperCase()) {
case "ECHO":
result = getListEcho();
break;
case "CONNECT":
result = getListConnect();
break;
}
}
if (level2 == LEVEL_2) {
switch (menuItem.toUpperCase()) {
case "TOMORROWLAND":
result = getListTomorrowland();
break;
case "SPELL BE":
result = getListSpellBe();
break;
}
}
return result;
}
public static boolean isExpandable(BaseItem baseItem) {
return baseItem instanceof GroupItem;
}
private static List<BaseItem> getListEcho() {
List<BaseItem> list = new ArrayList<>();
list.add(new Item("Workshops"));
list.add(new Item("Events"));
list.add(new Item("Projects"));
list.add(new GroupItem("Tomorrowland"));
list.add(new GroupItem("Spell Be"));
return list;
}
private static List<BaseItem> getListTomorrowland() {
List<BaseItem> list = new ArrayList<>();
list.add(new Item("Tomorrowland Winners"));
list.add(new Item("Tomorrowland Jr."));
return list;
}
private static List<BaseItem> getListSpellBe() {
List<BaseItem> list = new ArrayList<>();
list.add(new Item("Winners"));
return list;
}
private static List<BaseItem> getListConnect() {
List<BaseItem> list = new ArrayList<>();
list.add(new Item("Career"));
list.add(new Item("Inquiry"));
return list;
}
}
如有任何帮助,我们将不胜感激。提前致谢。
而不是使用 contains
,您应该使用 equals
来检查所有条件,在您的情况下,TomorrowLand Winners
包含 Winners
并且还包含 Winners
包含 Winners
,因此它同时打开。
//do this for all conditions
if (((BaseItem) object).getName().equals("Tomorrowland Winners")) {
displaySelectedScreen("TOMORROWLAND WINNERS");
}