【问题标题】:How to scroll to center of child of HorizontalScrollView?如何滚动到 Horizo​​ntalScrollView 的孩子的中心?
【发布时间】:2012-09-14 12:15:16
【问题描述】:

我需要滚动到 Horizo​​ntalScrollView 的 childView(TextView) 的中心。 我已经使用下面的代码获得了中心孩子,现在我想将箭头调整到该孩子视图的确切中心。

在这里,我做了一个 Horizo​​ntalScrollView,在里面有一个 LineaerLayout,在 LinearLayout 里面我有 TextViews。

这是我的 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

}
            });

【问题讨论】:

  • @Naser 你能帮我解决同样的问题吗?
  • @AnjaliTripathi 接受的答案对我有用。如果您需要其他任何内容,可以询问。
  • 是的,自定义 scrollView 概念

标签: android horizontalscrollview rect


【解决方案1】:

首先我可以看到你使用TextView来画一条黑线,你可以只使用一个View。

所以如果我们假设你创建了一个 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;
                }
            }
        }
    });

如您所见,步骤如下:

  1. 找到 ScrollView 的滚动中心。
  2. 扫描所有要居中的元素(childrenViews)(在这种情况下,只有 TextViews)。
  3. 如果滚动视图的中心在元素的边界内,则测试每个元素。
  4. 滚动(以任何你想要的方式 - 在这种情况下使用 scrollBy)到元素中心并打破循环。

【讨论】:

    【解决方案2】:

    这对我有用:

    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);
    

    【讨论】:

      【解决方案3】:

      这对我有用:

          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;
              }
          });
      

      【讨论】:

        猜你喜欢
        • 2012-10-08
        • 1970-01-01
        • 2012-08-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多