1

我在 Horizo​​ntalScrollView 中有 LinearLayout。

 <?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/hsv"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <LinearLayout
        android:id="@+id/mygallery"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="horizontal" >
    </LinearLayout>

</HorizontalScrollView>

在 LinearLayout 中,我通过以下方式加载图像:

myGallery = (LinearLayout) findViewById(R.id.mygallery);
for (int i = 1; i <= 5; i++) {
    ImageView iv = new ImageView(getApplicationContext());
    iv.setLayoutParams(new LayoutParams(screenWidth,LayoutParams.WRAP_CONTENT));
    iv.setImageBitmap(decodeSampledBitmapFromResource(getResources(),getResources().getIdentifier("pic0" + i, "drawable",getPackageName()), screenWidth, screenHeight));
    myGallery.addView(iv);

当用户滚动图像时,我需要锁定居中的图像(如Gallery

4

2 回答 2

0

You should use smoothScrollTo(x,y) so it will appear that the image is locked at the correct position. X and Y should be calculated manually.

于 2012-11-21T10:41:51.837 回答
0

这是我的解决方案: 1.create custom view

public class CenteringHorizontalScrollView extends HorizontalScrollView implements View.OnTouchListener {

private static final int SWIPE_PAGE_ON_FACTOR = 5;
View targetLeft, targetRight;
CircleImageView leftImage, rightImage;
private Context mContext;
private int mActiveItem;
private float mPrevScrollX;

private int mItemWidth = 250;// or whatever your item width is.
private int centreItemWidth = 350;
private final int LEFT = 1;
private final int RIGHT = 2;
private final int NA = 0;
private final int emptyView = 2;
private static String TAG = CenteringHorizontalScrollView.class.getSimpleName();

public CenteringHorizontalScrollView(Context context, AttributeSet attrs) {
    super(context, attrs);
    mContext = context;
    setOnTouchListener(this);

}

@Override
public boolean onTouch(View v, MotionEvent event) {
    int x = (int) event.getRawX();

    boolean handled = false;
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            mPrevScrollX = x;
            break;
        case MotionEvent.ACTION_MOVE:

            break;
        case MotionEvent.ACTION_UP:
            int minFactor = mItemWidth / SWIPE_PAGE_ON_FACTOR;

            if ((mPrevScrollX - (float) x) > minFactor) {
                if (mActiveItem < getMaxItemCount() - 1 - emptyView) {
                    setCurrentItem(mActiveItem + 1);
                    CommonConstant.currentChildIdx = (mActiveItem - emptyView);
                } 
            } else if (((float) x - mPrevScrollX) > minFactor) {
                if (mActiveItem > 0 + emptyView) {
                    setCurrentItem(mActiveItem - 1);
                    CommonConstant.currentChildIdx = (mActiveItem - emptyView);
                } 
            }
            scrollToActiveItem(NA);

            handled = true;
            break;
    }

    return handled;
}

private int getMaxItemCount() {
    return ((LinearLayout) getChildAt(0)).getChildCount();
}

private LinearLayout getLinearLayout() {
    return (LinearLayout) getChildAt(0);
}


private void setCurrentItem(int cur) {
    if (cur < 0 + emptyView) {
        mActiveItem = 2;
    } else if (cur > getMaxItemCount() - 1 - emptyView) {
        mActiveItem = getMaxItemCount() - 1 - emptyView;
    } else {
        mActiveItem = cur;
    }
}

/**
 * Scrolls the list view to the currently active child.
 */
private void scrollToActiveItem(int direction) {
    int maxItemCount = getMaxItemCount();
    if (maxItemCount == 0) {
        return;
    }

    int targetItem = Math.min(maxItemCount - 1, mActiveItem);
    targetItem = Math.max(0, targetItem);


    setCurrentItem(targetItem);
    targetItem = mActiveItem;

    // Scroll so that the target child is centered
    View targetView = getLinearLayout().getChildAt(targetItem);
    LinearLayout centerLayout = (LinearLayout) targetView;
    ImageView centerImage = (ImageView) centerLayout.getChildAt(0);
    centerImage.setBackground(mContext.getResources().getDrawable(R.drawable.rectangle_background));


    //get the image to left of the centered image
    if ((targetItem - 1) >= 0) {
        targetLeft = getLinearLayout().getChildAt(targetItem - 1);
        LinearLayout ll = (LinearLayout) targetLeft;
        leftImage = (CircleImageView) ll.getChildAt(0);
        centerImage.setBackground(null);
    }
    //get the image to right of the centered image
    if ((targetItem + 1) < maxItemCount) {
        targetRight = getLinearLayout().getChildAt(targetItem + 1);
        LinearLayout ll = (LinearLayout) targetRight;
        rightImage = (CircleImageView) ll.getChildAt(0);
        centerImage.setBackground(null);
    }
    int targetLeftx = centerLayout.getLeft();
    int childWidth = centerLayout.getRight() - targetLeftx;
    int width = getWidth() - getPaddingLeft() - getPaddingRight();
    int targetScroll = targetLeftx - ((width - childWidth) / 2);

    super.smoothScrollTo(targetScroll, 0);
}


/**
 * Sets the current item and centers it.
 *
 * @param currentItem The new current item.
 */
public void setCurrentItemAndCenter(int currentItem) {
    setCurrentItem(currentItem + emptyView);
    scrollToActiveItem(NA);
}
}

2.在活动/片段中

   private void initScrollView() {
    tools_top_scroll = (CenteringHorizontalScrollView) getActivity().findViewById(R.id.horizontalScrollView);
    rl = (LinearLayout) getActivity().findViewById(R.id.tools_top);

    addEmptyChildView();
    addEmptyChildView();
    for (int i = 0; i < 5; i++) {
        setInnerChildView(getChildren().get(i));
    }
    addEmptyChildView();
    addEmptyChildView();
    tools_top_scroll.post(new Runnable()
    {
        @Override
        public void run()
        {
            tools_top_scroll.setCurrentItemAndCenter(CommonConstant.currentChildIdx);
        }
    });
}

private void setInnerChildView(ChildR childR) {
    View view = LayoutInflater.from(getActivity()).inflate(R.layout.item_head_name, null);
    ImageView child_img = (ImageView) view.findViewById(R.id.child_img);
    TextView child_name = (TextView) view.findViewById(R.id.child_name);
    Log.i(TAG, "child name:" + childR.getChild().getC_name());
    child_name.setText(childR.getChild().getC_name());
    rl.addView(view);

}


private void addEmptyChildView() {
    View view = LayoutInflater.from(getActivity()).inflate(R.layout.item_head_name, null);
    view.setVisibility(View.INVISIBLE);
    rl.addView(view);

}

3.在xml中

    <com.shouhu.angel.view.CenteringHorizontalScrollView

    android:id="@+id/horizontalScrollView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@mipmap/tool_f_top_bg"
    android:scrollbars="none">

    <LinearLayout
        android:id="@+id/tools_top"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:gravity="center"
        android:orientation="horizontal">

    </LinearLayout>
</com.shouhu.angel.view.CenteringHorizontalScrollView>
于 2016-06-27T06:41:44.413 回答