简体   繁体   English

Coordinatorlayout 中的 Recyclerview 切断了最后一项

[英]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.我遇到的问题是,每当该片段第一次膨胀时,Recyclerview 都可以正常工作并显示列表中的所有项目。 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.但是当我更改片段并再次回到这个片段时,Recyclerview 的最后一项被切断并隐藏在底部导航栏后面。

Here is my code for activity_home_screen.xml这是我的 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这是 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这是 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这是 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.从事件 XML 中删除此行。

android:fitsSystemWindows="true"

And everything would be good to go.一切都会好起来的。

override the fragment's setUserVisibleHint(),call view?.requestlayout()覆盖片段的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此布局隐藏了您的最后一个 Recycler 视图,当显示此布局时,您的最后一个项目被隐藏,当您设置

  collapsingToolbarLayout.setTitle(""); 

your last item is visible.你的最后一个项目是可见的。

So, set padding to parent layout accordingly因此,相应地将填充设置为父布局

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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