RecyclerView Manager遍布我的片段

时间:2018-01-11 13:02:33

标签: android

社区片段图片

只想问一下这个问题。我的recyclerView位于我的Home片段中,但是当我要打开另一个像社区一样的片段时,recyclerView将出现在后台。

这是我的HomeFragment RecyleVIew代码

 DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
 ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
         this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
 drawer.addDrawerListener(toggle);
 toggle.syncState();

 //NavigationDrawer
 NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
 navigationView.setNavigationItemSelectedListener(this);
 //User Name on Navigation View

 txtFulNname = (TextView) navigationView.getHeaderView(0).findViewById(R.id.txtFullName);
 txtEmail = (TextView)navigationView.getHeaderView(0).findViewById(R.id.txtEmail);

 //Load menu
 recycler_menu = (RecyclerView)findViewById(R.id.recycler_menu);
 recycler_menu.setHasFixedSize(true);
 layoutManager = new LinearLayoutManager(this);
 recycler_menu.setLayoutManager(layoutManager);
 loadMenu();
 navigationView.setCheckedItem(R.id.nav_Menu);

加载菜单

private void loadMenu() {
    FirebaseRecyclerAdapter<Category, MenuViewHolder> adapter = new FirebaseRecyclerAdapter<Category, MenuViewHolder>(Category.class,R.layout.menu_item, MenuViewHolder.class,category) {
        @Override
        protected void populateViewHolder(MenuViewHolder viewHolder, Category model, int position) {
           viewHolder.textMenuName.setText(model.getName());
            Picasso.with(getBaseContext()).load(model.getImage()).into(viewHolder.imageView);
            final Category clickItem = model;
            viewHolder.setItemClickListener(    new ItemClickListener() {
                @Override
                public void onClick(View view, int position, boolean isLongClick) {
                   Intent intent = new Intent(MainActivity.this, Food_list.class);
                   startActivity(intent);
                   MainActivity.this.finish();
                }
            });
        }
    };
    recycler_menu.setAdapter(adapter);
}

碎片

public void setActionBarTitle(String title){
    getSupportActionBar().setTitle(title);
}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

private void NavigationDrawer(int id){
    android.support.v4.app.Fragment fragment = null;
    switch (id){
        case R.id.nav_Menu:
            fragment = new HomeFragment();
            break;
        case  R.id.nav_Communities:
            fragment = new CommunitiesFragment();
            break;
        case R.id.nav_logout:
            FirebaseAuth.getInstance().signOut();
            Intent intent = new Intent(MainActivity.this, LoginActivity.class);
            startActivity(intent);
            MainActivity.this.finish();
            break;
    }

        if (fragment !=null){
            android.support.v4.app.FragmentTransaction fragmentTransaction =getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.flmain, fragment);
            fragmentTransaction.commit();
    }
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);

}


@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item)  {
    // Handle navigation view item clicks here.
    android.support.v4.app.Fragment fragment;
    int id = item.getItemId();

    NavigationDrawer(id);

    return true;
}

Content_Main.XML

<android.support.constraint.ConstraintLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.fil_it.fragment_navigation.MainActivity"
tools:showIn="@layout/app_bar_main">


<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
 android:id="@+id/flmain">
</FrameLayout>


 <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"
        android:id="@+id/recycler_menu"
        ></android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>

3 个答案:

答案 0 :(得分:0)

您将为活动提供null作为null,或者在此活动或应用程序使用的样式中将background设置为null。

答案 1 :(得分:0)

尝试添加android:clickable =&#34; true&#34;在您的片段布局中

答案 2 :(得分:0)

只是我的猜测是什么。试试这个

<android.support.constraint.ConstraintLayout  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"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.fil_it.fragment_navigation.MainActivity"
tools:showIn="@layout/app_bar_main">

    <android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="vertical"
    android:id="@+id/recycler_menu"
    ></android.support.v7.widget.RecyclerView>


     <FrameLayout
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:id="@+id/flmain">
     </FrameLayout>

</android.support.constraint.ConstraintLayout>