RecyclerViw滚动与偏移

时间:2016-02-15 15:58:41

标签: android android-recyclerview

我在偏移滚动方面遇到了问题,我希望当我向上滚动时,我会得到下一个项目,并且对于向下项目也是如此。 我在中间视图中显示每个项目,所以我获得了像视图高度的偏移量,但是当我滚动它时,它给我最后一项或第一项。 我尝试了 scrollTo()和同样的问题

我这样做了但它没有用。

public void fillFormules(List<Category> objectsTextToShow)
{
    MyRecyclerAdapter adapter;
    final LinearLayout layoutItemDetail = (LinearLayout) view.findViewById(R.id.middle);
    LinearLayout relativeLayout = (LinearLayout) view.findViewById(R.id.titles);


    // Initialize recycler view
    for (int j = 0; j <objectsTextToShow.size() ; j++) {
        LinearLayout parentLayout = new LinearLayout(getActivity());
        parentLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, (float) 1.0));
        parentLayout.setOrientation(LinearLayout.HORIZONTAL);
        RecyclerView mRecyclerView = new RecyclerView(getActivity()); //(RecyclerView) view.findViewById(R.id.recycler_view);
        List<Item> items = new ArrayList<Item>();
        TextView textCat = new TextView(getActivity());
        textCat.setText(Check.Languages(objectsTextToShow.get(j).getName(), LANGUAGE_ID));
        textCat.setTextSize(28);
        textCat.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1f));
        textCat.setTextColor(colorUtils.TITLE);
        textCat.setGravity(Gravity.CENTER);

        relativeLayout.addView(textCat);
        textCat.setBackgroundColor(colorUtils.backgroundColor);

        for (int i = 0; i <objectsTextToShow.get(j).getItems().size() ; i++) {
            items.add(objectsTextToShow.get(j).getItems().get(i));
        }

        final LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(manager);
        adapter = new MyRecyclerAdapter(getActivity(), items);
        mRecyclerView.setAdapter(adapter);
        parentLayout.addView(mRecyclerView);
        layoutItemDetail.addView(parentLayout);

        mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                final int Height = layoutItemDetail.getHeight();
                if(dy > 0)
                {
                    Log.e("RecyclerView scrolled: ", "scroll up!"+Height);
                    recyclerView.smoothScrollToPosition(Height);
                }
                else
                {
                    recyclerView.smoothScrollToPosition(-Height);

                Log.e("RecyclerView scrolled: ", "scroll down!"+-Height);

                }

            }
        });

    }
}

pic:enter image description here

0 个答案:

没有答案