我有一个底部导航栏和一个框架布局,它是片段容器。因此,当单击底部导航栏上的图标时,上面的片段会发生变化。
在其中一个片段中,我有一个协调器布局,折叠工具栏和回收器视图。目前,数据只是一个虚拟数据。
我遇到的问题是,无论何时第一次对该片段进行充气,Recyclerview都能正常工作并显示列表中的所有项目。但是当我更改片段并再次返回此片段时,Recyclerview的最后一项被切断并隐藏在底部导航栏后面。
这是我的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"
/>
这是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"
/>
这是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);
}
}
这是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);
}
}
任何帮助都会非常感激。谢谢!
答案 0 :(得分:2)
从事件XML中删除此行。
val
一切都会好起来的。
答案 1 :(得分:0)
collapsingToolbarLayout.setTitle("Events");
此布局隐藏了您的上一个Recycler视图,当您显示隐藏的最后一项时,设置时
collapsingToolbarLayout.setTitle("");
你的最后一项是可见的。
因此,相应地将填充设置为父布局
答案 2 :(得分:0)
覆盖片段的setUserVisibleHint(),call view?.requestlayout()
override fun setUserVisibleHint(isVisibleToUser: Boolean) {
view?.requestLayout()
super.setUserVisibleHint(isVisibleToUser)
}