2017-09-16 70 views
-1

具有Recycler視圖的Android應用程序。我如何實現「回到頂部」按鈕Android。請幫助我..實現「返回頂部」按鈕Android

<android.support.v4.widget.SwipeRefreshLayout 
    android:id="@+id/swipeRefreshLayout2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 
    <android.support.v7.widget.RecyclerView 
     android:id="@+id/rcvbl" 
     android:background="@drawable/bbg1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" /> 

</android.support.v4.widget.SwipeRefreshLayout> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/flot" 
     android:layout_width="99dp" 
     android:layout_height="99dp" 
     android:layout_marginLeft="30dp" 
     android:layout_marginTop="-120dp" 
     android:scrollbars="horizontal" 
     android:src="@drawable/search11" 
     app:elevation="7dp" 
     app:fabSize="normal" /> 

回答

0

佑康試試這個你recyclerview

mRecyclerView.smoothScrollToPosition(0); 

,或者您也可以做到這一點

LinearLayoutManager layoutManager = (LinearLayoutManager) mRecyclerView 
       .getLayoutManager(); 
layoutManager.scrollToPositionWithOffset(0, 0); 
0

只要你所要做的使用在onClick事件以下FloatingActionButton

public void setFloatingActionButton(final View view) { 
    float actionButton = (android.support.design.widget.FloatingActionButton) getActivity() 
      .findViewById(R.id.float); 
    actionButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      LinearLayoutManager layoutManager = (LinearLayoutManager) mRecyclerView 
        .getLayoutManager(); 
      layoutManager.scrollToPositionWithOffset(0, 0); 
     } 
    }); 
} 
0
// set the Position to 0, if you want to scroll to first position. 

int Position = 0; 
SpeedyLinearLayoutManager linearLayoutManager = new SpeedyLinearLayoutManager(CustomerActivity.this); 
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL); 
linearLayoutManager.scrollToPosition(myPosition); 
myRecyclerView.setLayoutManager(linearLayoutManager); 
Adapter myAdapter = new Adapter(Activity.this, myArrayList); 
myRecyclerView.setAdapter(myCustomerAdapter); 
myAdapter.notifyDataSetChanged();    


public class SpeedyLinearLayoutManager extends LinearLayoutManager { 

     private static final float MILLISECONDS_PER_INCH = 2f; //default is 25f (bigger = slower) 

     public SpeedyLinearLayoutManager(Context context) { 
      super(context); 
     } 

     public SpeedyLinearLayoutManager(Context context, int orientation, boolean reverseLayout) { 
      super(context, orientation, reverseLayout); 
     } 

     public SpeedyLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 
      super(context, attrs, defStyleAttr, defStyleRes); 
     } 

     @Override 
     public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) { 

      final LinearSmoothScroller linearSmoothScroller = new LinearSmoothScroller(recyclerView.getContext()) { 

       @Override 
       public PointF computeScrollVectorForPosition(int targetPosition) { 
        return SpeedyLinearLayoutManager.this.computeScrollVectorForPosition(targetPosition); 
       } 

       @Override 
       protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) { 
        return MILLISECONDS_PER_INCH/displayMetrics.densityDpi; 
       } 
      }; 

      linearSmoothScroller.setTargetPosition(position); 
      startSmoothScroll(linearSmoothScroller); 
     } 
    }