HorizontalScrollView в RecycleView scrollRight в начале - PullRequest
0 голосов
/ 13 декабря 2018

Я пытаюсь использовать HorizontalScrollView в режиме рециркуляции, поэтому каждый элемент будет иметь все свои данные внутри, и я пытаюсь запустить его полностью правильно.но независимо от того, что я пытаюсь, я все еще вижу левую часть представления прокрутки в течение нескольких миллисекунд, прежде чем она прокручивается до конца

моего макета:

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">


    <com.cobox.core.ui.views.CustomHorizontalScollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/horizontal_scroll_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fillViewport="true"
    android:scrollbars="none">

    <LinearLayout
        android:id="@+id/view_home_card_and_button_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">


        <ImageView
            android:layout_width="100dp"
            android:layout_height="150dp"
            android:background="@color/amber_200" />

        <android.support.v7.widget.CardView
            android:id="@+id/cardView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/half_unit"
            app:cardCornerRadius="4dp">

        </android.support.v7.widget.CardView>


    </LinearLayout>

</com.cobox.core.ui.views.CustomHorizontalScollView>

ScrollView:

public class CustomHorizontalScollView extends HorizontalScrollView {

    public OnScrollChangedListener mOnScrollChangedListener;

    public CustomHorizontalScollView(Context context) {
        super(context);
    }

    public CustomHorizontalScollView(Context context, AttributeSet attrs) {
        super(context, attrs);

    }

    public CustomHorizontalScollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        if (mOnScrollChangedListener != null) {
            mOnScrollChangedListener.onScrollChanged(l, t, oldl, oldt);
        }
    }

    public void setOnScrollChangedListener(OnScrollChangedListener onScrollChangedListener) {
        this.mOnScrollChangedListener = onScrollChangedListener;
    }

    public interface OnScrollChangedListener {
        void onScrollChanged(int l, int t, int oldl, int oldt);
    }

}

и соответствующая часть кода:

public class HomeScreenCardView extends FrameLayout implements 
HomeCardActionsListener {
public HomeScreenCardView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    this.context = context;


    init(attrs, defStyle);
}

...
private void init(AttributeSet attrs, int defStyle) {

    WindowManager wm = ((Activity) context).getWindowManager();
    Display d = wm.getDefaultDisplay();
    ViewGroup.LayoutParams layoutParams2 = mCardView.getLayoutParams();
    float dip = 8f;
    float px = getPxFromDp(dip);
    layoutParams2.width = (int) (d.getWidth() - px * 2);
    mCardView.setLayoutParams(layoutParams2);

    horizontalScrollView.post(new Runnable() {
        @Override
        public void run() {
            horizontalScrollView.setSmoothScrollingEnabled(false);
            horizontalScrollView.fullScroll(View.FOCUS_RIGHT);
            horizontalScrollView.setSmoothScrollingEnabled(true);

        }
    });
}

Ответы [ 2 ]

0 голосов
/ 13 декабря 2018

Замените код прокрутки на код ниже

ViewTreeObserver vto = horizontalScrollView.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener(){
        @Override
        public void onGlobalLayout() {
            horizontalScrollView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            horizontalScrollView.setSmoothScrollingEnabled(false);
            horizontalScrollView.scrollTo(horizontalScrollView.getWidth(), 0);
            horizontalScrollView.setSmoothScrollingEnabled(false);
        }
    });
0 голосов
/ 13 декабря 2018

try В вашем Java-коде

horizontalScrollView.setLayoutDirection(View.FOCUS_RIGHT); 

и в XML

android:layoutDirection="rtl"

...