我想在我的应用中实现视差效果。我有两个片段(实际上是一个包含两个视图的片段)。在这两个片段中,我有recyclerView到显示项目。一切都还可以,但问题是当我滚动recyclerView和工具栏上升,如果我切换到第二个片段,在recyclerview和tab之间将有一个额外的空白,因为工具栏已经消失,我不知道如何处理这个 这是我拍摄的视频链接,向您展示问题: video link
这是我的片段类:
public class FirstFragment extends Fragment implements RecycleAdapter.OnViewClickedListener{
private ArrayList<String> items = new ArrayList<>();
public static Fragment getInstance(int catId){
FirstFragment firstFragment = new FirstFragment();
Bundle bundle = new Bundle();
bundle.putInt("CAT_ID", catId);
firstFragment.setArguments(bundle);
return firstFragment;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view;
int id = getArguments().getInt("CAT_ID");
if (id == 1){
view = inflater.inflate(R.layout.first_fragment,container,false);
}else{
view = inflater.inflate(R.layout.second_fragment,container,false);
}
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
final LinearLayoutManager manager = new LinearLayoutManager(getActivity().getApplicationContext());
manager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(manager);
RecycleAdapter adapter = new RecycleAdapter(this,items);
final MainActivity mainActivity = (MainActivity) getActivity();
final int toolbarHeight = Utils.getToolbarHeight(getActivity());
recyclerView.setOnScrollListener(new MaterialRecycler(getActivity()) {
@Override
public void onMoved(int distance) {
mainActivity.toolbarContainer.setTranslationY(-distance);
}
@Override
public void onShow() {
mainActivity.toolbarContainer.animate().translationY(0).setInterpolator(new AccelerateInterpolator());
}
@Override
public void onHide() {
mainActivity.toolbarContainer.animate().translationY(-toolbarHeight).setInterpolator(new DecelerateInterpolator());
}
});
recyclerView.setAdapter(adapter);
feedArray();
return view;
}
这是我的类来处理recyclerview滚动
private int toolbarOffset=0;
private int toolbarHeight;
private boolean controlsVisible=true;
private int totalScrolledDistance;
private static final int HIDE_OFFSET = 10;
private static final int SHOW_OFFSET = 80;
public MaterialRecycler(Context context){
toolbarHeight = Utils.getToolbarHeight(context);
}
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (newState == RecyclerView.SCROLL_STATE_IDLE){
if (totalScrolledDistance < toolbarHeight){
setVisible();
}else{
if (controlsVisible){
if (toolbarOffset > HIDE_OFFSET){
setInvisible();
}else{
setVisible();
}
}else{
if (toolbarHeight - toolbarOffset > SHOW_OFFSET){
setVisible();
}else{
setInvisible();
}
}
}
}
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, final int dy) {
super.onScrolled(recyclerView, dx, dy);
clipOffset();
onMoved(toolbarOffset);
if ((toolbarOffset < toolbarHeight && dy >0) || (toolbarOffset > 0 && dy<0)){
toolbarOffset +=dy;
}
totalScrolledDistance +=dy;
}
private void clipOffset(){
if (toolbarOffset > toolbarHeight){
toolbarOffset = toolbarHeight;
}else if (toolbarOffset < 0){
toolbarOffset = 0;
}
}
private void setVisible(){
if (toolbarOffset > 0){
onShow();
toolbarOffset =0;
}
controlsVisible = true;
}
private void setInvisible(){
if (toolbarOffset < toolbarHeight){
onHide();
toolbarOffset = toolbarHeight;
}
controlsVisible = false;
}
public abstract void onMoved(int distance);
public abstract void onShow();
public abstract void onHide();
这是我的activity_main.xml文件:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" android:background="#fff">
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/pager"/>
<LinearLayout android:layout_width="match_parent"
android:id="@+id/toolbarContainer"
android:orientation="vertical"
android:layout_height="wrap_content">
<include layout="@layout/toolbar" />
<include layout="@layout/tab" />
</LinearLayout>
</FrameLayout>
还是我对recyclerView的recycleler.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:id="@+id/recyclerView"
android:paddingTop="120dp"
/>
</FrameLayout>
工具栏:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" android:gravity="top">
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="top"
android:background="#3e2723"
>
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="72dp"
android:id="@+id/toolbar"
android:clipToPadding="false">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Material App"
android:textSize="18dp"
android:gravity="left"
android:padding="14dp"
android:textColor="#fff"
/>
</android.support.v7.widget.Toolbar>
</LinearLayout>
</LinearLayout>
tab.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" android:gravity="top">
<com.astuetz.PagerSlidingTabStrip android:layout_width="match_parent"
android:layout_height="48dp"
android:id="@+id/tab"
android:layout_gravity="top"
android:background="#3e2723"
app:pstsDividerColor="#00000000"
app:pstsIndicatorColor="#fff"
app:pstsIndicatorHeight="3dp"
app:pstsShouldExpand="true"
>
</com.astuetz.PagerSlidingTabStrip>
</LinearLayout>
最后是first_fragment.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:background="#fff"
android:layout_height="match_parent">
<include layout="@layout/recycler_view" />
</FrameLayout>