Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/231.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_Horizontalscrollview_Rect - Fatal编程技术网

Android 如何滚动到HorizontalScrollView子级的中心?

Android 如何滚动到HorizontalScrollView子级的中心?,android,horizontalscrollview,rect,Android,Horizontalscrollview,Rect,我需要滚动到水平滚动视图的childView(文本视图)的中心。 我使用下面的代码得到了中心子视图,现在我想将箭头调整到该子视图的精确中心 在这里,我所做的是,我拍摄了一个水平滚动视图,其中有一个LinearLayout,在LinearLayout中有文本视图 以下是我的XML: <com.sample.test.ScrollViewCustom android:id="@+id/mHorizontalScrollViewMain"

我需要滚动到水平滚动视图的childView(文本视图)的中心。 我使用下面的代码得到了中心子视图,现在我想将箭头调整到该子视图的精确中心

在这里,我所做的是,我拍摄了一个水平滚动视图,其中有一个LinearLayout,在LinearLayout中有文本视图

以下是我的XML:

 <com.sample.test.ScrollViewCustom
                android:id="@+id/mHorizontalScrollViewMain"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_alignBottom="@id/relativeOne"
                android:background="@drawable/mergethree2"
                android:scrollbars="none" >

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

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="12dip"
                        android:background="@drawable/blackline" />

                    <TextView
                        android:id="@+id/mtxtHome"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:layout_marginBottom="5dip"
                        android:layout_marginLeft="5dip"
                        android:layout_marginRight="5dip"
                        android:layout_marginTop="8dip"
                        android:text="home"
                        android:textColor="@android:color/white"
                        android:textSize="15sp"
                        android:typeface="serif" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="12dip"
                        android:background="@drawable/blackline" />

                    <TextView
                        android:id="@+id/mtxtSchools"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:layout_marginBottom="5dip"
                        android:layout_marginLeft="5dip"
                        android:layout_marginRight="5dip"
                        android:layout_marginTop="8dip"
                        android:text="schools"
                        android:textColor="@android:color/white"
                        android:textSize="15sp"
                        android:typeface="serif" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="12dip"
                        android:background="@drawable/blackline" />

                    <TextView
                        android:id="@+id/mtxtCalendar"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:layout_marginBottom="5dip"
                        android:layout_marginLeft="5dip"
                        android:layout_marginRight="5dip"
                        android:layout_marginTop="8dip"
                        android:text="calendar"
                        android:textColor="@android:color/white"
                        android:textSize="15sp"
                        android:typeface="serif" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="12dip"
                        android:background="@drawable/blackline" />

                    <TextView
                        android:id="@+id/mtxtActivitiesAndClubs"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:layout_marginBottom="5dip"
                        android:layout_marginLeft="5dip"
                        android:layout_marginRight="5dip"
                        android:layout_marginTop="8dip"
                        android:text="activitiesandclubs"
                        android:textColor="@android:color/white"
                        android:textSize="15sp"
                        android:typeface="serif" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="12dip"
                        android:background="@drawable/blackline" />

                    <TextView
                        android:id="@+id/mtxtBoosters"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:layout_marginBottom="5dip"
                        android:layout_marginLeft="5dip"
                        android:layout_marginRight="5dip"
                        android:layout_marginTop="8dip"
                        android:text="boosters"
                        android:textColor="@android:color/white"
                        android:textSize="15sp"
                        android:typeface="serif" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="12dip"
                        android:background="@drawable/blackline" />

                    <TextView
                        android:id="@+id/mtxtSchoolMenu"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:layout_marginBottom="5dip"
                        android:layout_marginLeft="5dip"
                        android:layout_marginRight="5dip"
                        android:layout_marginTop="8dip"
                        android:text="schoolmenu"
                        android:textColor="@android:color/white"
                        android:textSize="15sp"
                        android:typeface="serif" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="12dip"
                        android:background="@drawable/blackline" />

                    <TextView
                        android:id="@+id/mtxtStaff"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:layout_marginBottom="5dip"
                        android:layout_marginLeft="5dip"
                        android:layout_marginRight="5dip"
                        android:layout_marginTop="8dip"
                        android:text="staff"
                        android:textColor="@android:color/white"
                        android:textSize="15sp"
                        android:typeface="serif" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="12dip"
                        android:background="@drawable/blackline" />

                    <TextView
                        android:id="@+id/mtxtMyFavorites"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:layout_marginBottom="5dip"
                        android:layout_marginLeft="5dip"
                        android:layout_marginRight="5dip"
                        android:layout_marginTop="8dip"
                        android:text="myfavorites"
                        android:textColor="@android:color/white"
                        android:textSize="15sp"
                        android:typeface="serif" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="12dip"
                        android:background="@drawable/blackline" />

                    <TextView
                        android:id="@+id/mtxtAboutContact"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:layout_marginBottom="5dip"
                        android:layout_marginLeft="5dip"
                        android:layout_marginRight="5dip"
                        android:layout_marginTop="8dip"
                        android:text="aboutcontact"
                        android:textColor="@android:color/white"
                        android:textSize="15sp"
                        android:typeface="serif" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="12dip"
                        android:background="@drawable/blackline" />
                </LinearLayout>
        </com.sample.test.ScrollViewCustom>

我想要的是,我想要箭头精确地位于屏幕中央的TextView的中心。如下图所示

我现在得到的是

我用下面的代码得到了孩子的中心,但现在我的要求是滚动到孩子的屏幕中心

这是我的密码:

    scroll.setOnScrollStopListner(new onScrollStopListner() {
                    @Override
                    public void onScrollStoped() {
int scrollX = scroll.getScrollX();
                    int width = lLayOne.getChildAt(1).getWidth();
                    int home = width;
                    int school = width * 2;
                    int calender = width * 3;
                    int activityNclubs = width * 4;
                    int booster = width * 5;
                    int schoolMenu = width * 6;
                    int staff = width * 7;
                    int myFav = width * 8;
                    int about = width * 9;

                    if ( scrollX > 0 && scrollX < home ) {
                        Log.d(TAG, "Home "+" scrollx "+scrollX+" home "+home);
                    }else if ( scrollX > home && scrollX < school ) {
                        Log.d(TAG, "school"+" scrollx "+scrollX+" school "+school);
                    }else if ( scrollX > school && scrollX < calender ) {
                        Log.d(TAG, "calender"+" scrollx "+scrollX+" calender "+calender);
                    }else if ( scrollX > calender && scrollX < activityNclubs ) {
                        Log.d(TAG, "activity n clubs"+" scrollx "+scrollX+" activity "+activityNclubs);
                    }else if ( scrollX > activityNclubs && scrollX < booster ) {
                        Log.d(TAG, "booster"+" scrollx "+scrollX+" booster "+booster);
                    }else if ( scrollX > booster && scrollX < schoolMenu ) {
                        Log.d(TAG, "school menu"+" scrollx "+scrollX+" school menu "+schoolMenu);
                    }else if ( scrollX > schoolMenu && scrollX < staff ) {
                        Log.d(TAG, "staff"+" scrollx "+scrollX+" staff "+staff);
                    }else if ( scrollX > staff && scrollX < myFav ) {
                        Log.d(TAG, "myFav"+" scrollx "+scrollX+" fav "+myFav);
                    }else if ( scrollX > myFav && scrollX < about ) {
                        Log.d(TAG, "about"+" scrollx "+scrollX+" about "+about);
                    }                       // TODO Auto-generated method stub

}
            });
scroll.setOnScrollStopListner(新的onScrollStopListner(){
@凌驾
公共无效onScrollStoped(){
int scrollX=scroll.getScrollX();
int-width=lLayOne.getChildAt(1.getWidth();
int home=宽度;
int学校=宽度*2;
内压光机=宽度*3;
int activityNclubs=宽度*4;
int=宽度*5;
int学校菜单=宽度*6;
int staff=宽度*7;
int myFav=宽度*8;
int大约=宽度*9;
if(scrollX>0&&scrollXhome&&scrollX学校(&scrollX<日历){
Log.d(标签,“日历”+“滚动X”+滚动X+“日历”+日历);
}else if(scrollX>日历(&scrollXactivityNclubs&&scrollXbooster&&scrollXschool菜单&&scrollXstaff&&scrollXmyFav&&scrollX
首先,我可以看到您使用文本视图来画一条黑线,您可以只使用视图

因此,如果我们假设您创建了一个OnScrollStopListener接口,那么您可以使用以下代码来获取元素中心

      scroll.setOnScrollStopListener(new OnScrollStopListener() {
        @Override
        public void onScrollStoped() {
            //get the center
            int center = scroll.getScrollX() + scroll.getWidth() / 2;
            LinearLayout linearLayout = ((LinearLayout)scroll.findViewById(R.id.llayoutfirst));
            int chilrenNum = linearLayout.getChildCount();
            for (int i = 0; i < chilrenNum; i++) {
                View v = linearLayout.getChildAt(i);
                if(v.getClass()!=TextView.class){
                    // you do no care about that view
                    continue;
                }
                int viewLeft = v.getLeft();
                int viewWidth = v.getWidth();
                if (center >= viewLeft && center <= viewLeft + viewWidth) {
                    Log.d(TAG, "CENTER THIS : " + ((viewLeft + viewWidth / 2) - center));
                    scroll.scrollBy((viewLeft + viewWidth / 2) - center, 0);
                    break;
                }
            }
        }
    });
scroll.setOnScrollStopListener(新的OnScrollStopListener(){
@凌驾
公共无效onScrollStoped(){
//抓住中心
int center=scroll.getScrollX()+scroll.getWidth()/2;
LinearLayout LinearLayout=((LinearLayout)scroll.findViewById(R.id.llayoutfirst));
int chilrenNum=linearLayout.getChildCount();
对于(int i=0;i=viewleet&¢er这对我有效:

    SHVradioChannels.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            if (event.getAction() == MotionEvent.ACTION_UP) {       
                int center = SHVradioChannels.getScrollX() + SHVradioChannels.getWidth() / 2;
                LinearLayout linearLayout = ((LinearLayout)SHVradioChannels.findViewById(R.id.LLradioChannels));
                int chilrenNum = linearLayout.getChildCount();
                for (int i = 0; i < chilrenNum; i++) {
                    View v2 = linearLayout.getChildAt(i);
                    int viewLeft = v2.getLeft();
                    int viewWidth = v2.getWidth();
                    if (center >= viewLeft && center <= viewLeft + viewWidth) {
                        SHVradioChannels.scrollBy((viewLeft + viewWidth / 2) - center, 0);
                        break;
                    }
                }
            }
            return false;
        }
    });
SHVradioChannels.setOnTouchListener(新的OnTouchListener(){
@凌驾
公共布尔onTouch(视图v,运动事件){
如果(event.getAction()==MotionEvent.ACTION\u UP){
int center=SHVradioChannels.getScrollX()+SHVradioChannels.getWidth()/2;
LinearLayout LinearLayout=((LinearLayout)SHVradioChannels.findViewById(R.id.LLradioChannels));
int chilrenNum=linearLayout.getChildCount();
对于(int i=0;i=viewleet&¢er这对我有效:

HorizontalScrollView calendarScroller = (HorizontalScrollView)findViewById(R.id.calendarScroller);
    Handler handler=new Handler();
    Runnable mTabSelector = new Runnable() {
        public void run() {
            View tabView = dateScroller.getChildAt(selectedDateIndex);// selected child view
            final int scrollPos = tabView.getLeft() - (calendarScroller.getWidth() - tabView.getWidth()) / 2;
            calendarScroller.smoothScrollTo(scrollPos, 0);
           // mTabSelector = null;
        }
    };
    handler.postDelayed(mTabSelector, 10);

@纳赛尔,你能就同样的问题请我吗?@AnjaliTripathi接受的答案对我有用。如果你还需要什么,你可以问。是的,自定义滚动视图概念