RecyclerView |最快的smoothScrollToPosition动画

时间:2016-07-14 14:22:35

标签: android android-recyclerview android-animation recycler-adapter

问题是基于真实场景,我简化了用例:

我们假设我们有一个带有0到999之间值的微调器和循环视图。 另外,对于常规的回收器滑动选项,用户也可以从微调器中选择一个数字,回收器将滚动到索引。

问题:当当前页面与新微调器值之间的差值很大时,需要花费大量时间来滚动视图(例如,我们位置很高)现在用户从微调器400中选择。

以下是代码(项目布局仅包含textview):

public class MainActivity extends AppCompatActivity {
    RecyclerView recyclerView;
    Spinner spinner;
    List<Integer> items = new ArrayList<>();
    ItemAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // init "database"
        for (int i = 0; i < 1000; i++)
            items.add(i);

        // Spinner
        spinner = (Spinner) findViewById(R.id.spinner);
        final ArrayAdapter<Integer> spinnerAdapter = new ArrayAdapter<Integer>(this, R.layout.support_simple_spinner_dropdown_item, items);
        spinner.setAdapter(spinnerAdapter);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                recyclerView.smoothScrollToPosition(i);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        // Recycle View
        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
        adapter = new ItemAdapter();
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(adapter);
    }

    public class ItemAdapter extends RecyclerView.Adapter<ViewHolder>
    {

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View v = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item, parent, false);
            ViewHolder vh = new ViewHolder(v);
            return vh;
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            holder.tvIndex.setText(String.valueOf(position));
        }

        @Override
        public int getItemCount() {
            return items.size();
        }
    }

    private class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvIndex;

        public ViewHolder(View itemView) {
            super(itemView);
            tvIndex = (TextView) itemView.findViewById(R.id.tv_index);
        }
    }
}

问题:我如何执行类似&#34;快速滚动&#34;什么时候需要刷很多页?

编辑:我想要动画,但想要短片。

1 个答案:

答案 0 :(得分:5)

recyclerView.scrollToPosition(I-5);然后recyclelerView.smoothScrollToPosition(i);但请记住根据您的使用验证此“i”