- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
当我在导航 View 的菜单之间切换时,我遇到了 fragment 重叠的问题。最初我的应用程序具有带有多个选项的导航 View 。在该主页选项 fragment 中,具有包含两个选项卡的 View 寻呼机。当我在 View 之间切换时寻呼机标签效果很好..
问题是当我从“设置”之类的导航切换菜单时,相应的 fragment 加载良好,然后我重新加载主页 fragment , View 寻呼机加载选项卡,但旧 fragment (设置)显示在背景中
此外,当我多次加载 home 选项时, fragment 会重叠多次而不是替换 fragment
我的代码如下
Activity 类:DashboardActivity.java
/**
* DashboardActivity.java
*/
public class DashboardActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener, FragmentManager.OnBackStackChangedListener,
ViewPageListener {
/**
* Used as initializing the layout as data binding.
*/
private ActivityDashboardBinding activityDashboardBinding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activityDashboardBinding = DataBindingUtil.setContentView(this, R.layout.activity_dashboard);
activityDashboardBinding.setOnClickController(new DashboardController());
setSupportActionBar(activityDashboardBinding.toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, activityDashboardBinding.drawerLayout, activityDashboardBinding.toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
activityDashboardBinding.drawerLayout.addDrawerListener(toggle);
activityDashboardBinding.navigationView.setNavigationItemSelectedListener(this);
toggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(false);
toggle.setDrawerIndicatorEnabled(false);
toggle.setHomeAsUpIndicator(R.drawable.ic_menu_hamburger);
toggle.setToolbarNavigationClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (activityDashboardBinding.drawerLayout.isDrawerOpen(GravityCompat.START)) {
activityDashboardBinding.drawerLayout.closeDrawer(GravityCompat.START);
} else {
activityDashboardBinding.drawerLayout.openDrawer(GravityCompat.START);
}
}
});
displaySelectedScreen(R.id.nav_home);
}
@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_dashboard, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_notification) {
startActivity(new Intent(this, NotificationActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
// Handle navigation view item clicks here.
displaySelectedScreen(item.getItemId());
return true;
}
/**
* Method used as navigation selection option.
*
* @param itemId Selected id.
*/
private void displaySelectedScreen(int itemId) {
Fragment fragment = null;
String fragmentName = null;
if (itemId == R.id.nav_home) {
activityDashboardBinding.toolBarTitle.setText(R.string.toolbar_name_dashboard);
activityDashboardBinding.addNewRide.show();
fragment = new HomePageFragment();
fragmentName = Constants.NAME_NAVIGATION_DASHBOARD;
} else if (itemId == R.id.nav_rides) {
} else if (itemId == R.id.nav_profile) {
activityDashboardBinding.toolBarTitle.setText(R.string.toolbar_name_my_profile);
activityDashboardBinding.addNewRide.hide();
fragment = new UserProfileFragment();
fragmentName = Constants.NAME_NAVIGATION_MY_PROFILE;
} else if (itemId == R.id.nav_settings) {
activityDashboardBinding.toolBarTitle.setText(R.string.toolbar_name_settings);
fragment = new SettingsFragment();
activityDashboardBinding.addNewRide.hide();
fragmentName = Constants.NAME_NAVIGATION_SETTINGS;
} else if (itemId == R.id.nav_logout) {
Intent logoutIntent = new Intent(getApplicationContext(), LoginActivity.class);
logoutIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(logoutIntent);
}
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.addOnBackStackChangedListener(this);
fragmentManager.beginTransaction().add(R.id.container, fragment)
.addToBackStack(fragmentName).commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
}
@Override
public void onBackPressed() {
if (activityDashboardBinding.drawerLayout.isDrawerOpen(GravityCompat.START)) {
activityDashboardBinding.drawerLayout.closeDrawer(GravityCompat.START);
} else if (getFragmentName().equals(Constants.NAME_NAVIGATION_DASHBOARD)) {
finish();
} else {
// Let super handle the back press
super.onBackPressed();
}
}
@Override
public void onBackStackChanged() {
if (getFragmentName().equals(Constants.NAME_NAVIGATION_DASHBOARD)) {
activityDashboardBinding.toolBarTitle.setText(R.string.toolbar_name_dashboard);
activityDashboardBinding.addNewRide.show();
} else if (getFragmentName().equals(Constants.NAME_NAVIGATION_MY_PROFILE)) {
activityDashboardBinding.toolBarTitle.setText(R.string.toolbar_name_my_profile);
activityDashboardBinding.addNewRide.hide();
} else {
activityDashboardBinding.toolBarTitle.setText(R.string.toolbar_name_settings);
activityDashboardBinding.addNewRide.hide();
}
}
/**
* Method used to get the fragment transaction name to identify the fragment.
*
* @return the fragment name.
*/
private String getFragmentName() {
FragmentManager fm = getSupportFragmentManager();
return fm.getBackStackEntryAt(fm.getBackStackEntryCount() - 1).getName();
}
@Override
public void onViewPageListener() {
activityDashboardBinding.addNewRide.animate().translationY(0).setInterpolator(new LinearInterpolator()).start();
}
}
DashboardViewAdapter.java
/**
* The class that contains the list of fragments and list of title. We can use the fragments using
* view pager from here.
*
* @version 1.0
*/
public class DashboardViewAdapter extends FragmentStatePagerAdapter {
/**
* Title of the fragment list.
*/
private String[] mTitle;
/**
* Fragment list Which contains the fragments on the adapter.
*/
private List<Fragment> fragmentList;
/**
* Instantiates a new adapter dashboard view.
*
* @param fm the Instance of the FragmentManager.
*/
public DashboardViewAdapter(FragmentManager fm) {
super(fm);
}
/**
* Set the list of title from the activity.
*
* @param titles Title list.
*/
public void setTitle(String[] titles) {
this.mTitle = titles;
}
/**
* Set the fragment list for the particular view pager using FragmentStatePagerAdapter.
*
* @param fragmentList List of fragment.
*/
public void setFragmentList(List<Fragment> fragmentList) {
this.fragmentList = fragmentList;
}
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
@Override
public int getCount() {
return mTitle.length;
}
@Override
public CharSequence getPageTitle(int position) {
return mTitle[position];
}
}
HomePageFragment.java
/**
* Fragment used as home page to display the ride offered and my rides details.
*
* @version 1.0
*/
public class HomePageFragment extends Fragment implements ViewPager.OnPageChangeListener {
/**
* Binding the fragment.
*/
private FragmentHomePageBinding homePageBinding;
/**
* Interface to listen the view pager changes.
*/
private ViewPageListener viewPageListener;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
homePageBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_home_page, container, false);
setTabs();
setHasOptionsMenu(true);
viewPageListener = (ViewPageListener) getActivity();
return homePageBinding.getRoot();
}
/**
* Set up the Ride offers and My rides fragments in the tab
*/
private void setTabs() {
String[] mTitle = new String[]{"Rides offered", "My Rides"};
FragmentManager fm = getChildFragmentManager();
DashboardViewAdapter mAdapter = new DashboardViewAdapter(fm);
mAdapter.setTitle(mTitle);
mAdapter.setFragmentList(getFragmentList());
homePageBinding.viewPager.setAdapter(mAdapter);
homePageBinding.tabLayout.setupWithViewPager(homePageBinding.viewPager);
homePageBinding.viewPager.addOnPageChangeListener(this);
}
/**
* Get the fragment list to display the view pager tabs. Recent chat and contacts fragment will
* be return from this.
*
* @return List of the fragments for the viewpager
*/
private List<Fragment> getFragmentList() {
/**
* Add the fragment as a list.
*/
List<Fragment> fragmentList = new ArrayList<>();
/**
The fragment contacts which contains the ride offer list.
*/
RidesOfferedFragment ridesOfferedFragment = new RidesOfferedFragment();
/**
The fragment contacts which contains the ride offer list.
*/
MyRidesFragment myRidesFragment = new MyRidesFragment();
fragmentList.add(ridesOfferedFragment);
fragmentList.add(myRidesFragment);
return fragmentList;
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
viewPageListener.onViewPageListener();
}
@Override
public void onPageSelected(int position) {
//Overridden Method
}
@Override
public void onPageScrollStateChanged(int state) {
//Overridden Method
}
@Override
public void onPrepareOptionsMenu(Menu menu) {
menu.findItem(R.id.action_notification).setVisible(true);
super.onPrepareOptionsMenu(menu);
}
}
activity_dashboard.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="onClickController"
type="com.contus.carpooling.dashboard.homepage.viewmodel.DashboardController" />
</data>
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<android.support.design.widget.CoordinatorLayout
android:id="@+id/coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.contus.carpooling.dashboard.homepage.view.DashboardActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay">
<TextView
android:id="@+id/tool_bar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Dashboard"
android:textColor="@android:color/white"
android:textSize="18sp" />
</android.support.v7.widget.Toolbar>
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</LinearLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/add_new_ride"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|bottom"
android:layout_margin="10dp"
android:clickable="true"
android:onClick="@{onClickController.fabBtnOnClick()}"
android:src="@drawable/ic_action_add"
app:backgroundTint="@color/colorSecondaryColor"
app:fabSize="normal"
app:layout_behavior="com.contus.carpooling.view.ScrollAwareFABBehavior" />
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@android:color/white"
android:fitsSystemWindows="true"
app:headerLayout="@layout/navigation_header"
app:itemIconTint="@color/colorPrimary"
app:itemTextColor="#4b4b4b"
app:menu="@menu/activity_dashboard_drawer"
app:theme="@style/NavigationDrawerSelected" />
</android.support.v4.widget.DrawerLayout>
</layout>
fragment_home_page.xml
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.contus.carpooling.dashboard.homepage.view.HomePageFragment">
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:tabIndicatorColor="@color/colorHighLightBlue"
app:tabIndicatorHeight="4dp" />
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</LinearLayout>
</FrameLayout>
</layout>
设置显示在仪表板背景中..重叠图像
等待回复...提前致谢
最佳答案
这里的问题是添加的 fragment 将背景设置为透明,因此将背景设置为您要添加的布局 fragment 文件例如:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorBackground"
>
关于android - MVVM : Fragment overlapping issue in view pager with navigation view?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42713680/
所以我正在做一个Java作业,我必须创建一个矩形类,该类在一个绘制重叠矩形的程序中使用,并且在矩形重叠的地方,用新的颜色绘制一个新的矩形。我添加了硬件描述的链接,因为我认为让您查看它比我试图解释它更容
如何将两个重叠的 div 加上第三个 div 放在那些重叠的 div 的右侧(但第三个 div 没有一直 float )? I overlap id=two. I overlap id=one. I
我需要你的帮助,我有一个问题(见图),我假设有两个数组,每个数组都包含不同长度和实际值的间隔,我需要找出我如何有效地重叠这些间隔。 我对想法、论文理论或具体算法持开放态度,它们会让我找到出路! 我猜想
尝试编写一个从聚合数据返回气泡图的函数。 我将“agg”中的 data.frame 列传递给它。 aggs2 9) } ##order data.frame by mean agg1 <
我正在使用 d3.js 以这种方式生成一些直接位于彼此上方的矩形: var greenRed = d3.select(".green-red").append("svg") .attr("he
我正在尝试创建一个库,它提供一个简单的链表实现以及该链表的一些概括,例如堆栈和队列,所有这些都基于基本链表。 问题是,我希望拥有具有自己的“私有(private)”函数的不同类型,这样您就不会使用“s
我使用 AJAX 和 JQuery 从 MySQL 数据库中提取数据,基本上使用以下内容: function getCard(card, lineNr, linedisplay, type){ var
如何在 SQL OVERLAPS 中包含开始日期和结束日期? 喜欢=。怎么办? 因为在我看来 OVERLAPS 只检查两者之间的范围,但是例如: 日期 2001 年 1 月 1 日 - 2001 年
我在使用在 Google Web Fonts 上找到的字体时遇到了一些问题. 正如您在下面发布的图片中看到的,当我使用 Firefox 时,“Versus”中的大写 V 与“e”重叠。尽管当我使用 C
题目地址:https://leetcode.com/problems/image-overlap/description/ 题目描述 Twoimages A and B are given, re
题目地址:https://leetcode.com/problems/rectangle-overlap/description/ 题目描述: Arectangle is represented
在我们的测试环境中,我们的 solr 搜索引擎一直存在许多问题。我们在 4.6 版、单分片、4 个节点上有一个 solr 云设置。我们看到领导节点上的 CPU 水平线达到 100% 几个小时,然后服务
我正在做一个 excel 任务,我必须找出是否有重叠的日期。 在我的 excel 工作表中有 startDate(column D) 和 EndDate(comun E) 的列,由此我必须确定是否存在
在这张图片中,一个全景项目的内容渗透到前一个项目上: 如何在 Expression Blend/with XAML 中执行此操作? 这是我目前的 XAML:
我已经以编程方式创建了一个 iPhone UI,但我只是不知道如何调整 View ,以免它被 TabBar 重叠。这是所有权层次结构: -AppDelegate UITabBarController
我正在使用 wxMaxima 16.12.0 (Maxima 5.39.0),当我尝试打印输出时,出现重叠文本。 这是一个例子(请忽略命令不正确的事实) 第一张图片 有时也会发生在 print和 pr
我尝试为 javascript 添加一些语法高亮到 vim,但我一直遇到一个问题:当字符已经高亮时,它们似乎被所有其他正则表达式完全忽略。 例如,我尝试为函数的参数列表添加语法高亮显示。在创建正确的
我有五个 ImageView(ImageButton),我想将其显示在一行上,但是当我在小型设备上时,我的最后一个图像被裁剪了? 我该如何修复它? 有没有办法检测屏幕宽度? |一个 |乙| C | d
我尝试为 javascript 添加一些语法高亮到 vim,但我一直遇到一个问题:当字符已经高亮时,它们似乎被所有其他正则表达式完全忽略。 例如,我尝试为函数的参数列表添加语法高亮显示。在创建正确的
我将用数学解释,这是我正在努力为以下内容编写 Scheme 代码的转换: (f '(a b c) '(d e f)) = '(ad (+ bd ae) (+ cd be af) (+ ce bf) c
我是一名优秀的程序员,十分优秀!