henryblue / TvRecyclerView

A custom RecyclerView for Android TV end
Apache License 2.0
7 stars 4 forks source link

[BUG] when use row with value "2" #13

Closed netodevel closed 6 years ago

netodevel commented 6 years ago

My code:

public class ModuleFocusVerticalActivity extends AppCompatActivity {

    private TvRecyclerView mTvRecyclerView;
    public int[] mStartIndex =      {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
    public int[] mItemRowSizes =    {2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
    public int[] mItemColumnSizes = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_module_vertical);
        mTvRecyclerView = (TvRecyclerView) findViewById(R.id.tv_recycler_view);
        init();
    }

    private void init() {
        ModuleLayoutManager manager = new MyModuleLayoutManager(2, LinearLayoutManager.VERTICAL,
                200, 200);
        mTvRecyclerView.setLayoutManager(manager);

        int itemSpace = getResources().
                getDimensionPixelSize(R.dimen.recyclerView_item_space);
        mTvRecyclerView.addItemDecoration(new SpaceItemDecoration(itemSpace));
        ModuleAdapter mAdapter = new ModuleAdapter(ModuleFocusVerticalActivity.this, mStartIndex.length);
        mTvRecyclerView.setAdapter(mAdapter);

        mTvRecyclerView.setOnItemStateListener(new TvRecyclerView.OnItemStateListener() {
            @Override
            public void onItemViewClick(View view, int position) {
                Toast.makeText(ModuleFocusVerticalActivity.this,
                        ContantUtil.TEST_DATAS[position], Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onItemViewFocusChanged(boolean gainFocus, View view, int position) {
            }
        });
        mTvRecyclerView.setSelectPadding(35, 34, 35, 38);
    }

    private class SpaceItemDecoration extends RecyclerView.ItemDecoration {

        private int space;

        SpaceItemDecoration(int space) {
            this.space = space;
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
                                   RecyclerView.State state) {
            outRect.bottom = space;
            outRect.left = space;
        }
    }

    private class MyModuleLayoutManager extends ModuleLayoutManager {

        MyModuleLayoutManager(int rowCount, int orientation, int baseItemWidth, int baseItemHeight) {
            super(rowCount, orientation, baseItemWidth, baseItemHeight);
        }

        @Override
        protected int getItemStartIndex(int position) {
            if (position < mStartIndex.length) {
                return mStartIndex[position];
            } else {
                return 0;
            }
        }

        @Override
        protected int getItemRowSize(int position) {
            if (position < mItemRowSizes.length) {
                return mItemRowSizes[position];
            } else {
                return 2;
            }
        }

        @Override
        protected int getItemColumnSize(int position) {
            if (position < mItemColumnSizes.length) {
                return mItemColumnSizes[position];
            } else {
                return 1;
            }
        }

        @Override
        protected int getColumnSpacing() {
            return getResources().
                    getDimensionPixelSize(R.dimen.recyclerView_item_space);
        }

        @Override
        protected int getRowSpacing() {
            return getResources().
                    getDimensionPixelSize(R.dimen.recyclerView_item_space);
        }
    }
}

BUG:

image

when I scroll to the end of the list and come back

image

henryblue commented 6 years ago

This is not a bug. startIndex is wrong, it should be: public int[] mStartIndex = {0, 1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; Because the first item occupies two base item size,the starting point of the third item is 3

netodevel commented 6 years ago

I got it, thanks.