RecyclerView混合滚动项目滚动位置

时间:2018-03-20 11:53:03

标签: android android-layout android-recyclerview recycler-adapter linearlayoutmanager

在垂直滚动期间RecyclerView混合主适配器项目的水平滚动位置。

enter image description here

MainActivity的代码:

LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
recyclerView = (RecyclerView) findViewById(R.id.recycler_viewiew);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(layoutManager);
adapterChanels = new AdapterChanels(this);
recyclerView.swapAdapter(adapterChanels, false);

RecyclerView项目持有人:

    programView = (RecyclerView)itemView.findViewById(R.id.progra_view);
    LinearLayoutManager layoutManager = new LinearLayoutManager(activty, LinearLayoutManager.HORIZONTAL, false);
    layoutManager.setAutoMeasureEnabled(true);
    programView.setLayoutManager(layoutManager);
    programView.setHasFixedSize(true);

onBindViewHolder:

    holder.programView.setAdapter(holder.schedulAdapter);
    holder.schedulAdapter.notifyDataSetChanged();

        holder.programView.post(new Runnable() {
            @Override
            public void run() {
                int pos = onScrollListener.getmCurX() - holder.programView.computeHorizontalScrollOffset();
                holder.programView.removeOnScrollListener(onScrollListener);
                holder.programView.scrollBy(onScrollListener.getmCurX(),0);
                holder.programView.addOnScrollListener(onScrollListener);

            }
        });

RecyclerView Item OnScrollListener:

    public class OnScrollListener extends RecyclerView.OnScrollListener {

private static final String Tag = OnScrollListener.class.getName();
int mCurX = 0;
RecyclerView mainRecyclerView;
LinearLayoutManager verticalLinearLayoutManager;

public OnScrollListener(RecyclerView mainRecyclerView, LinearLayoutManager verticalLinearLayoutManager, int startPos) {

    mCurX = startPos;
    this.mainRecyclerView = mainRecyclerView;
    this.verticalLinearLayoutManager = verticalLinearLayoutManager;
}


@Override
public void onScrollStateChanged(final RecyclerView recyclerView, final int newState) {
    super.onScrollStateChanged(recyclerView, newState);
    switch (newState) {
        case RecyclerView.SCROLL_STATE_IDLE:

            final int firstVisibleItemPosition1 = verticalLinearLayoutManager.findFirstVisibleItemPosition();
            final int lastVisibleItemPosition1 = verticalLinearLayoutManager.findLastVisibleItemPosition();
            for (int i = firstVisibleItemPosition1; i <= lastVisibleItemPosition1; ++i) {
                RecyclerView horizontalRecyclerView = (RecyclerView) mainRecyclerView.findViewHolderForAdapterPosition(i).itemView.findViewById(R.id.progra_view);
                if (horizontalRecyclerView != null && horizontalRecyclerView != recyclerView)
                    horizontalRecyclerView.addOnScrollListener(this);
            }
            recyclerView.setTag(false);


            break;
        case RecyclerView.SCROLL_STATE_SETTLING:
        case RecyclerView.SCROLL_STATE_DRAGGING:
            final int firstVisibleItemPosition = verticalLinearLayoutManager.findFirstVisibleItemPosition();
            final int lastVisibleItemPosition = verticalLinearLayoutManager.findLastVisibleItemPosition();
            for (int i = firstVisibleItemPosition; i <= lastVisibleItemPosition; ++i) {
                RecyclerView horizontalRecyclerView = (RecyclerView) mainRecyclerView.findViewHolderForAdapterPosition(i).itemView.findViewById(R.id.progra_view);
                if (horizontalRecyclerView != null && horizontalRecyclerView != recyclerView)
                    horizontalRecyclerView.removeOnScrollListener(this);
            }
    }
}

@Override
public void onScrolled(RecyclerView recyclerView, final int dx, int dy) {
    super.onScrolled(recyclerView, dx, dy);
    boolean isScrollTo = (boolean) recyclerView.getTag();
    if (isScrollTo) {
        mCurX += dx;
        final int firstVisibleItemPosition = verticalLinearLayoutManager.findFirstVisibleItemPosition();
        final int lastVisibleItemPosition = verticalLinearLayoutManager.findLastVisibleItemPosition();
        for (int i = firstVisibleItemPosition; i <= lastVisibleItemPosition; ++i) {
            if (mainRecyclerView.findViewHolderForAdapterPosition(i) != null) {

                final RecyclerView horizontalRecyclerView = (RecyclerView) mainRecyclerView.findViewHolderForAdapterPosition(i).itemView.findViewById(R.id.progra_view);
                if (horizontalRecyclerView != null && horizontalRecyclerView != recyclerView)
                    horizontalRecyclerView.scrollBy(dx, 0);
            }
        }
    }
}

public void moveTo(final int moveX) {

    mCurX = mCurX + moveX;
    final int firstVisibleItemPosition = verticalLinearLayoutManager.findFirstVisibleItemPosition();
    final int lastVisibleItemPosition = verticalLinearLayoutManager.findLastVisibleItemPosition();
    for (int i = firstVisibleItemPosition; i <= lastVisibleItemPosition; ++i) {
        if (mainRecyclerView.findViewHolderForAdapterPosition(i) != null) {

            final RecyclerView horizontalRecyclerView = (RecyclerView) mainRecyclerView.findViewHolderForAdapterPosition(i).itemView.findViewById(R.id.progra_view);
            if (horizontalRecyclerView != null) {
                horizontalRecyclerView.post(new Runnable() {
                    @Override
                    public void run() {
                        horizontalRecyclerView.removeOnScrollListener(getOnScrollListener());
                        horizontalRecyclerView.scrollBy(moveX, 0);
                        horizontalRecyclerView.addOnScrollListener(getOnScrollListener());
                        horizontalRecyclerView.invalidate();
                    }
                });

            }

        }
    }
}

public int getmCurX() {
    return mCurX;
}

public void moveToZero() {
    mCurX = 0;

    for (int i = 0; i <= mainRecyclerView.getChildCount(); i++) {
        if (mainRecyclerView.getChildAt(i) != null) {
            final RecyclerView horizontalRecyclerView = (RecyclerView) mainRecyclerView.getChildAt(i).findViewById(R.id.progra_view);
            if (horizontalRecyclerView != null)

                horizontalRecyclerView.removeOnScrollListener(getOnScrollListener());
                horizontalRecyclerView.scrollBy(-horizontalRecyclerView.computeHorizontalScrollOffset(), 0);
                horizontalRecyclerView.addOnScrollListener(getOnScrollListener());
                horizontalRecyclerView.invalidate();
        }
    }
}

}

RecyclerView Item RecyclerViewItemTouch:

public class RecyclerViewItemTouch implements RecyclerView.OnItemTouchListener {

public static final String Tag = RecyclerViewItemTouch.class.getName();

Context context;
RecyclerView recyclerView;
RecyclerView lastView;
public RecyclerViewItemTouch(Context context, RecyclerView recyclerView) {
    this.context = context;
    this.recyclerView = recyclerView;
}

@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {

    int action = e.getAction();
    switch (action){

        case MotionEvent.ACTION_UP:
            break;
        case MotionEvent.ACTION_DOWN:
            if (lastView != null){
                lastView.setTag(false);
            }
            stopScroll();
            rv.setTag(true);
            lastView = rv;
            break;
        case MotionEvent.ACTION_MOVE:
            break;
    }
    return false;
}

@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {

}

@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {


}


private void stopScroll(){

    if (recyclerView != null){

        for (int  i = 0; i < recyclerView.getChildCount(); i ++){
            ((RecyclerView)recyclerView.getChildAt(i).findViewById(R.id.progra_view)).stopScroll();
        }
    }
}

}

该节目是一个电视应用程序。在左侧有频道名称,在右侧有频道的时间表。当我向右滚动时,它也应滚动时间线。当我进行垂直滚动(之后调用onBind方法)时会出现问题。在onBinde方法中我尝试做一个水平滚动持有者的recyclerView,但它不滚动,为什么?

0 个答案:

没有答案