我正在尝试在回收视图中使用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);
}
});
}
答案 0 :(得分:0)
尝试 在您的Java代码中
horizontalScrollView.setLayoutDirection(View.FOCUS_RIGHT);
和XML
android:layoutDirection="rtl"
答案 1 :(得分:0)
用下面的代码替换滚动代码
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);
}
});