Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/207.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android如何在HorizontalScrollView中聚焦下一幅图像_Android_Gallery_Horizontal Scrolling_Horizontalscrollview - Fatal编程技术网

Android如何在HorizontalScrollView中聚焦下一幅图像

Android如何在HorizontalScrollView中聚焦下一幅图像,android,gallery,horizontal-scrolling,horizontalscrollview,Android,Gallery,Horizontal Scrolling,Horizontalscrollview,我正在尝试创建HorizontalScrollView来显示一些图像(gallery已弃用,我不想使用ViewPager)。画廊看起来像这样: 问题是,我不知道如何在水平滚动视图中像在图库中一样只聚焦中心的一幅图像。如果我向右滚动,下一个图像将出现在屏幕中央。 他们是通过水平滚动视图来实现这一点的吗 使用此自定义的水平滚动视图。在这种情况下,需要将一个位置传递给setCenter,使其居中 引自 公共类CenterLockHorizontalScrollview扩展了HorizontalScr

我正在尝试创建HorizontalScrollView来显示一些图像(gallery已弃用,我不想使用ViewPager)。画廊看起来像这样:

问题是,我不知道如何在水平滚动视图中像在图库中一样只聚焦中心的一幅图像。如果我向右滚动,下一个图像将出现在屏幕中央。
他们是通过水平滚动视图来实现这一点的吗

使用此自定义的水平滚动视图。在这种情况下,需要将一个位置传递给setCenter,使其居中

引自

公共类CenterLockHorizontalScrollview扩展了HorizontalScrollView{
语境;
int-prevIndex=0;
 
public CenterLockHorizontalScrollview(上下文上下文、属性集属性){
超级(上下文,attrs);
this.context=上下文;
此.setSmoothScrollingEnabled(true);
 
    }
 
public void setAdapter(上下文上下文,CustomListAdapter mAdapter){
 
试一试{
用适配器填充视图(mAdapter);
}catch(zerochilde异常){
 
e.printStackTrace();
        }
    }
 
专用void fillview with adapter(CustomListAdapter mAdapter)
抛出0个异常{
如果(getChildCount()==0){
抛出新的ZeroChildException(
“CenterLockHorizontalScrollView必须有一个子项”);
        }
if(getChildCount()==0 | | mAdapter==null)
返回;
 
ViewGroup parent=(ViewGroup)getChildAt(0);
 
parent.removeallview();
 
对于(int i=0;i
public class CenterLockHorizontalScrollview extends HorizontalScrollView {

        Context context;

        int prevIndex = 0;

     

        public CenterLockHorizontalScrollview(Context context, AttributeSet attrs) {

            super(context, attrs);

            this.context = context;

            this.setSmoothScrollingEnabled(true);

     

        }

     

        public void setAdapter(Context context, CustomListAdapter mAdapter) {

     

            try {

                fillViewWithAdapter(mAdapter);

            } catch (ZeroChildException e) {

     

                e.printStackTrace();

            }

        }

     

        private void fillViewWithAdapter(CustomListAdapter mAdapter)

                throws ZeroChildException {

            if (getChildCount() == 0) {

                throw new ZeroChildException(

                        "CenterLockHorizontalScrollView must have one child");

            }

            if (getChildCount() == 0 || mAdapter == null)

                return;

     

            ViewGroup parent = (ViewGroup) getChildAt(0);

     

            parent.removeAllViews();

     

            for (int i = 0; i < mAdapter.getCount(); i++) {

                parent.addView(mAdapter.getView(i, null, parent));

            }

        }

     

        public void setCenter(int index) {

     

            ViewGroup parent = (ViewGroup) getChildAt(0);

     

            View preView = parent.getChildAt(prevIndex);

            preView.setBackgroundColor(Color.parseColor("#64CBD8"));

            android.widget.LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(

                    LinearLayout.LayoutParams.WRAP_CONTENT,

                    LinearLayout.LayoutParams.WRAP_CONTENT);

            lp.setMargins(5, 5, 5, 5);

            preView.setLayoutParams(lp);

     

            View view = parent.getChildAt(index);

            view.setBackgroundColor(Color.RED);

     

            int screenWidth = ((Activity) context).getWindowManager()

                    .getDefaultDisplay().getWidth();

     

            int scrollX = (view.getLeft() - (screenWidth / 2))

                    + (view.getWidth() / 2);

            this.smoothScrollTo(scrollX, 0);

            prevIndex = index;

        }

     

    }