Recylerview 在按下时附加旧结果

如何解决Recylerview 在按下时附加旧结果

我正在尝试创建一个带有底部导航视图的应用程序,但它看起来像是在堆积碎片。让我给你看清楚的图片

this is image 1 when app opens

from home I went to other page and when I clicked back button this happened it showed 2 results of same

[![再次点击后退按钮时黑屏][3]][3]

代码如下: 主页.xml

 <androidx.coordinatorlayout.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/bottomfrag"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="?attr/actionBarSize"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        />
    <com.google.android.material.bottomappbar.BottomAppBar
        android:id="@+id/bottom_app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        app:contentInsetStart="0dp"
        app:fabAlignmentMode="center">
        <com.rawat.soccermatch.CustomBottomNav
            android:id="@+id/bottomNavigationView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:itemRippleColor="@android:color/transparent"
            app:labelVisibilityMode="labeled"
            app:itemIconTint="@color/enable_disable"
            app:itemTextColor="@color/enable_disable"
            app:menu="@menu/bottom_nav_menu"
            app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior" />
    </com.google.android.material.bottomappbar.BottomAppBar>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:backgroundTint="#FF9800"
        android:contentDescription="@string/title_create"
        android:src="@drawable/ic_plusn"
        app:tint="#fff"
        app:layout_anchor="@id/bottom_app_bar" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

Home.java

        bt = findViewById(R.id.bottomNavigationView);
    loadFragment(new HomeFrag()); // at first home fragment will be loaded
    bt.setonNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            Fragment mfragment;
            int item_id=item.getItemId();
                    if(item_id==R.id.navigation_home){
                        mfragment = new HomeFrag();
                        loadFragment(mfragment);
                    }
                    else if(item_id==R.id.navigation_Feed){

                        mfragment = new Feed();
                        loadFragment(mfragment);
                    }
                    else if(item_id==R.id.navigation_create){

                        mfragment = new creatematch();
                        loadFragment(mfragment);
                    }
                    else if (item_id==R.id.navigation_following){

                        mfragment = new Following();
                        loadFragment(mfragment);
                    }
                    else {

                        mfragment = new Account();
                        loadFragment(mfragment);
                    }

            return true;
        }
    });

    ft = findViewById(R.id.fab);
    ft.setonClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ft.setBackgroundTintList(ColorStateList.valueOf(Color.GREEN));
            ft.setRippleColor(Color.GREEN);
            Fragment fragment = new creatematch();
            bt.getMenu().findItem(R.id.navigation_create).setChecked(true);
            loadFragment(fragment);
        }
    });

}

private void loadFragment(Fragment fragment) {
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.bottomfrag,fragment);
    transaction.addToBackStack(null);
    transaction.commit();
}

我也应该发布 HomFrag 代码吗?

picture 2

我现在已经编辑了这个问题,我理解的问题是当我点击返回我的 Home Fragment 从它离开的地方恢复时,但是 recylerview 值正在重置并将新结果附加到旧的。并且波纹颜色从跟随到回家没有变化。

解决方法

试着用这个函数代替你的

 public void loadFragment(Fragment fragment) {
    try {
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction ft = manager.beginTransaction();
        manager.popBackStack(null,FragmentManager.POP_BACK_STACK_INCLUSIVE);
        ft.addToBackStack(null);
        ft.replace(R.id.bottomfrag,fragment);
        ft.commit();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
,

您正在将该片段添加到后台堆栈,因此当您单击返回分离它时,您需要确保您的片段未添加到后台堆栈

,

覆盖 onBackPressed() 方法。在其中,您可以检查当前打开了哪个部分以及在这种情况下您想做什么(什么都不做/关闭应用程序/导航到另一个部分)。

,

在底部导航活动中,您必须设置默认片段是选择您想要的片段,例如在公共主页片段中始终显示为默认片段,因此将 homefragment 设置为默认值。

在创建时设置这些代码

bottomNavigationView.setSelectedItemId(R.id.nav_home);

检查背压

@Override
    public void onBackPressed() {

        if (getSupportFragmentManager().getBackStackEntryCount() == 1) {
            finish();
        } else {
            super.onBackPressed();
        }
    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?