1 /* 2 * Copyright 2019 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.example.androidx.widget; 18 19 import android.app.Activity; 20 import android.os.Bundle; 21 import android.text.Editable; 22 import android.view.ViewGroup; 23 import android.view.animation.Interpolator; 24 import android.widget.EditText; 25 26 import androidx.recyclerview.widget.DividerItemDecoration; 27 import androidx.recyclerview.widget.LinearLayoutManager; 28 import androidx.recyclerview.widget.RecyclerView; 29 30 import com.example.androidx.Cheeses; 31 import com.example.androidx.R; 32 import com.example.androidx.widget.adapter.SimpleStringAdapter; 33 34 /** 35 * Simple activity to test {@link RecyclerView#smoothScrollBy(int, int, Interpolator, int)} 36 * functionality. 37 */ 38 public class RecyclerViewSmoothScrollByActivity extends Activity { 39 40 private RecyclerView mRecyclerView; 41 private EditText mEditText; 42 43 @Override onCreate(Bundle savedInstanceState)44 protected void onCreate(Bundle savedInstanceState) { 45 super.onCreate(savedInstanceState); 46 47 setContentView(R.layout.activity_rv_smoothscrollby); 48 49 mRecyclerView = findViewById(R.id.recyclerView); 50 mEditText = findViewById(R.id.editTextDuration); 51 52 mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); 53 mRecyclerView.setAdapter(new SimpleStringAdapter(this, Cheeses.sCheeseStrings) { 54 @Override 55 public ViewHolder onCreateViewHolder(ViewGroup parent, 56 int viewType) { 57 final ViewHolder vh = super 58 .onCreateViewHolder(parent, viewType); 59 return vh; 60 } 61 }); 62 mRecyclerView 63 .addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL)); 64 65 findViewById(R.id.buttonUp).setOnClickListener(v -> scroll(false)); 66 findViewById(R.id.buttonDown).setOnClickListener(v -> scroll(true)); 67 } 68 scroll(boolean down)69 private void scroll(boolean down) { 70 int duration = 100; 71 Editable editable = mEditText.getText(); 72 if (editable != null) { 73 duration = Integer.parseInt(editable.toString()); 74 } 75 mRecyclerView.smoothScrollBy(0, down ? 1000 : -1000, null, duration); 76 } 77 } 78