我在片段中使用了recyclerview。我想在向下滚动时实现移动到顶部功能。所以我在recyclerview中添加了一个滚动监听器,但是在片段onViewCreated方法调用时触发了一次。滚动此方法时不会触发,因此dy值始终为零。我不知道为什么这个问题在片段的情况下,请回答我,谢谢。
// HomeFragment.java
public class HomeFragment extends Fragment {
....
public HomeFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_home, container, false);
recyclerView = (RecyclerView)view.findViewById(R.id.indiafragment_rview);
fab = (FloatingActionButton) view.findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
smoothScroller.setTargetPosition(0);
mLayoutManager.startSmoothScroll(smoothScroller);
}
});
recyclerView.setNestedScrollingEnabled(false);
sharedPreferences = getActivity().getSharedPreferences("News", Context.MODE_PRIVATE);
mLayoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(mLayoutManager);
getdata();
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
Toast.makeText(getActivity(), "Scroll changed", Toast.LENGTH_SHORT).show();
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (dy > 0 && fab.getVisibility() == View.VISIBLE) {
Toast.makeText(getActivity(), "Scroll down", Toast.LENGTH_SHORT).show();
fab.hide();
} else if (dy < 0 && fab.getVisibility() != View.VISIBLE) {
Toast.makeText(getActivity(), "Scroll up", Toast.LENGTH_SHORT).show();
fab.show();
}
if (dy > 0 ) {
Toast.makeText(getActivity(), "Scroll down", Toast.LENGTH_SHORT).show();
fab.hide();
} else if (dy < 0 ) {
Toast.makeText(getActivity(), "Scroll up", Toast.LENGTH_SHORT).show();
fab.show();
}else
{
Toast.makeText(getActivity(), "dy = "+dy, Toast.LENGTH_SHORT).show();
}
}
});
smoothScroller = new LinearSmoothScroller(getContext()) {
@Override protected int getVerticalSnapPreference() {
return LinearSmoothScroller.SNAP_TO_START;
}
};
return view;
}}
// fragment_home.xml
<android.support.design.widget.CoordinatorLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".HomeFragment">
<!-- TODO: Update blank fragment layout -->
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="60dp"
android:scaleType="fitXY"
android:id="@+id/bannerimg"
android:src="@drawable/banner_sample"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/bannerimg">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="8dp"
>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/fragment_rview"
>
</android.support.v7.widget.RecyclerView>
</android.support.v7.widget.CardView>
</LinearLayout>
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
app:backgroundTint="#FFFFFF"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@drawable/movetop" />