简体   繁体   中英

Recyclerview in Coordinatorlayout cutting off last item

I have a Bottom Navigation Bar, and a frame layout which is the fragment container. So when a an icon on a Bottom Navigation Bar is clicked, the fragment above changes.

In one of the fragment, I have a coordinator layout, collapsing toolbar, and recycler view. At the moment, the data is just a dummy data.

The problem that I am encountering is that whenever that fragment is inflated for the first time, the Recyclerview works fine and shows all of the items in the list. But when I change the fragment and come back to this fragment again, the last item of the Recyclerview is being cut off and is being hidden behind the Bottom Navigation Bar.

Here is my code for activity_home_screen.xml

<RelativeLayout 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"
tools:context="com.smartinc.livesportstv.activities.HomeScreen">

<android.support.design.widget.BottomNavigationView
    android:id="@+id/bottom_nav"
    android:layout_width="match_parent"
    android:layout_height="55dp"
    android:layout_alignParentBottom="true"
    android:layout_gravity="bottom"
    app:itemBackground="?attr/colorPrimary"
    app:itemIconTint="@drawable/selector_bottombar_item"
    app:itemTextColor="@drawable/selector_bottombar_item"
    app:menu="@menu/bottombar_menu" />

<FrameLayout
    android:id="@+id/frame_fragmentholder"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@id/bottom_nav"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    />

Here is the fragment_events.xml

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:fitsSystemWindows="true"
android:id="@+id/coordinator">

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar"
    android:layout_width="match_parent"
    android:layout_height="120dp"
    >

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:contentScrim="@color/colorPrimary"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        app:title="Events"
        app:expandedTitleTextAppearance="@style/TransparentText"
        app:collapsedTitleTextAppearance="@style/Toolbar_text_black"
        app:collapsedTitleGravity="center_horizontal"
        android:paddingEnd="20dp"
        android:fitsSystemWindows="true"
        >

        <!-- You can add views that you want to appear on the
             collapsing toolbar here -->

        <TextView
            android:id="@+id/events_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Events"
            android:textSize="30sp"
            android:textColor="@color/black"
            android:layout_marginTop="30dp"
            android:layout_marginStart="20dp"
            android:textStyle="bold"
            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="29 March, Thursday"
            android:textSize="20sp"
            android:textColor="@color/grey_800"
            android:layout_marginTop="70dp"
            android:layout_marginStart="20dp"
            />

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin"
            android:minHeight="?attr/actionBarSize"
            app:layout_scrollFlags="scroll|enterAlways"
            android:gravity="center_horizontal"
            app:popupTheme="@style/AppTheme"
            app:contentInsetLeft="0dp"
            app:contentInsetStart="0dp"
            />

    </android.support.design.widget.CollapsingToolbarLayout>

</android.support.design.widget.AppBarLayout>

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view_events"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    />


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Request"
    android:textSize="17sp"
    android:layout_gravity="end"
    android:textColor="@color/colorPrimaryDark"
    android:padding="10dp"
    android:layout_marginEnd="10dp"
    android:layout_marginTop="5dp"
    />

Here is HomeScreen.java

public class HomeScreen extends AppCompatActivity {

private BottomNavigationView bottomNavigationView;

private String fragName = "";

@Override
public void startActivity(Intent intent) {
    super.startActivity(intent);
    Constants.overridePendingTransitionEnter(this);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home_screen);

    init();
    setMainFragment();
    setBottomNavigation();
}

private void init(){
    bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_nav);


}

private void setBottomNavigation(){
    bottomNavigationView.setOnNavigationItemSelectedListener(
            new BottomNavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                    switch (item.getItemId()) {
                        case R.id.bottombaritem_events:
                            if(!fragName.equals("events")) {
                                FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                                ft.setCustomAnimations(R.anim.left_slide_in, R.anim.right_slide_out);
                                ft.replace(R.id.frame_fragmentholder, new EventsFragment());
                                ft.commit();
                                fragName = "events";
                            }
                            return true;
                        case R.id.bottombaritem_channels:
                            FragmentTransaction ft2 = getSupportFragmentManager().beginTransaction();
                            if(fragName.equals("events")) {
                                ft2.setCustomAnimations(R.anim.right_slide_in, R.anim.left_slide_out);
                            } else {
                                ft2.setCustomAnimations(R.anim.left_slide_in, R.anim.right_slide_out);
                            }

                            ft2.replace(R.id.frame_fragmentholder, new ChannelsFragment());
                            ft2.commit();
                            fragName = "channels";
                            return true;
                        case R.id.bottombaritem_more:
                            if(!fragName.equals("more")) {
                                FragmentTransaction ft3 = getSupportFragmentManager().beginTransaction();
                                ft3.setCustomAnimations(R.anim.right_slide_in, R.anim.left_slide_out);
                                ft3.replace(R.id.frame_fragmentholder, new MoreFragment());
                                ft3.commit();
                                fragName = "more";
                            }
                            return true;
                    }
                    return false;
                }
            });
}

private void setMainFragment(){
    fragName = "events";
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.replace(R.id.frame_fragmentholder, new EventsFragment());
    ft.commit();
}

@Override
public void onBackPressed() {
    super.onBackPressed();
    Constants.overridePendingTransitionExit(this);
}

}

And here is EventsFragment.java

public class EventsFragment extends Fragment {

private RecyclerView mRecyclerView;
private ArrayList<String> mData;

private CollapsingToolbarLayout collapsing_toolbar;

private Context mContext;

public EventsFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view =  inflater.inflate(R.layout.fragment_events, container, false);

    mContext = getActivity();

    final CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout) view.findViewById(R.id.collapsing_toolbar);
    AppBarLayout appBarLayout = (AppBarLayout) view.findViewById(R.id.app_bar);
    appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
        boolean isShow = true;
        int scrollRange = -1;

        @Override
        public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
            if (scrollRange == -1) {
                scrollRange = appBarLayout.getTotalScrollRange();
            }
            if (scrollRange + verticalOffset == 0) {
                collapsingToolbarLayout.setTitle("Events");
                isShow = true;
            } else if(isShow) {
                collapsingToolbarLayout.setTitle(" ");//carefull there should a space between double quote otherwise it wont work
                isShow = false;
            }
        }
    });

    // Initialize the RecyclerView
    mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_view_events);
    setUpRecyclerView();

    return view;
}

private void setUpRecyclerView() {
    LinearLayoutManager llm = new LinearLayoutManager(mContext);
    llm.setAutoMeasureEnabled(true);
    mRecyclerView.setLayoutManager(llm);

    EventsAdapter eventsAdapter = new EventsAdapter(mContext);
    mRecyclerView.setAdapter(eventsAdapter);
}

}

Any help would be really appreciated. Thanks!

Remove this line from Event XML.

android:fitsSystemWindows="true"

And everything would be good to go.

override the fragment's setUserVisibleHint(),call view?.requestlayout()

 override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        view?.requestLayout()
        super.setUserVisibleHint(isVisibleToUser)

    }
  collapsingToolbarLayout.setTitle("Events");

this layout is hiding your last Recycler view, when this is shown your last item is hidden, when you set

  collapsingToolbarLayout.setTitle(""); 

your last item is visible.

So, set padding to parent layout accordingly

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM