2013-02-27 3 views
2

У меня проблема с горизонтальным просмотром прокрутки.Как прокрутить изображение по горизонтали?

Это мой XML-код:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/relative_layout_id" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/bg" 
    tools:context=".MainActivity" > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/home_title_id" 
     android:text="@string/home_title" 
     android:layout_marginTop="@dimen/forty_text_size" 
     android:textColor="@android:color/white" 
     android:layout_centerHorizontal="true" 
     android:gravity="center" 
     android:textSize="@dimen/forty_text_size" /> 

    <LinearLayout 
     android:id="@+id/linear_layout_id" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/home_title_id"> 

     <HorizontalScrollView 
      android:id="@+id/horizontal_view_id" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" > 

      <ImageView 
       android:id="@+id/home_image_id" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:src="@drawable/abc" /> 
     </HorizontalScrollView> 

     </LinearLayout> 
     </RelativeLayout> 

В моем классе Java, расширяя деятельность я объявленную все виджеты, то я создал класс Scrollview, простираясь горизонтальный вид прокрутки.

Java-код выглядит следующим образом:

class scrollview extends HorizontalScrollView 
    { 
     private static final int SWIPE_MIN_DISTANCE = 5; 
     private static final int SWIPE_THRESHOLD_VELOCITY = 300; 
     private ArrayList mItems = null; 
     private GestureDetector mGestureDetector; 
     private int mActiveFeature = 0; 


     public scrollview(Context context) 
     { 
      super(context); 
     } 

     public void setFeatureItems(ArrayList items) 
     { 
      LinearLayout internalWrapper = new LinearLayout(getContext()); 
      internalWrapper.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
      internalWrapper.setOrientation(LinearLayout.HORIZONTAL); 
      addView(internalWrapper); 
      this.mItems = items; 
      for(int i = 0; i< items.size();i++) 
      { 
       LinearLayout featureLayout = (LinearLayout) View.inflate(this.getContext(),R.layout.activity_main,null); 
       //... 
       //Create the view for each screen in the scroll view 
       //... 
       internalWrapper.addView(featureLayout); 
      } 
      setOnTouchListener(new View.OnTouchListener() 
      { 
       @Override 
       public boolean onTouch(View v, MotionEvent event) 
       { 
        //If the user swipes 
        if (mGestureDetector.onTouchEvent(event)) 
        { 
         return true; 
        } 
        else if(event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL) 
        { 
         int scrollX = getScrollX(); 
         int featureWidth = v.getMeasuredWidth(); 
         mActiveFeature = ((scrollX + (featureWidth/2))/featureWidth); 
         int scrollTo = mActiveFeature*featureWidth; 
         smoothScrollTo(scrollTo, 0); 
         return true; 
        } 
        else 
        { 
         return false; 
        } 
       } 
      }); 
      mGestureDetector = new GestureDetector(new MyGestureDetector()); 
     } 
      class MyGestureDetector extends SimpleOnGestureListener 
      { 
      @Override 
      public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) 
      { 
       try 
       { 
        //right to left 
        if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) 
        { 
         int featureWidth = getMeasuredWidth(); 
         mActiveFeature = (mActiveFeature < (mItems.size() - 1))? mActiveFeature + 1:mItems.size() -1; 
         smoothScrollTo(mActiveFeature*featureWidth, 0); 
         return true; 
        } 
        //left to right 
        else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) 
        { 
         int featureWidth = getMeasuredWidth(); 
         mActiveFeature = (mActiveFeature > 0)? mActiveFeature - 1:0; 
         smoothScrollTo(mActiveFeature*featureWidth, 0); 
         return true; 
        } 
       } catch (Exception e) { 
         Log.e("Fling", "There was an error processing the Fling event:" + e.getMessage()); 
       } 
       return false; 
      } 
     } 
    } 

Но ничего не происходит, пожалуйста, помогите.

ORWILL ЭТО БУДЕТ ЛУЧШЕЕ, ЧТОБЫ ИМЕЕТСЯ КНОПКУ ПРОСМОТРА В ФУНКЦИИ ПЕРЕКЛЮЧЕНИЯ ONTOUCH ???

+0

У меня есть смятение ... ЭТО ДОСТАТОЧНО В ОТНОШЕНИИ ПЛОЩАДКИ НАСТОЯЩЕГО ВМЕСТО ИЗОБРАЖЕНИЯ ??? ЭТО, ЧТО Я СДЕЛАЛ – elegance

ответ

2

Неверное размещение тегов в вашем layout.xml файле, который я думаю. Поместите LinearLayout имеющий ImageView в HorizontalScrollView вместо HorizontalScrollView в LinearLayout, как показано ниже:

<HorizontalScrollView 
     android:id="@+id/horizontal_view_id" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/home_title_id" > 
    <LinearLayout 
    android:id="@+id/linear_layout_id" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 
     <ImageView 
      android:id="@+id/home_image_id" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/abc" /> 
    </LinearLayout> 
    </HorizontalScrollView> 
+0

Aʌɐpɥɐuı, НИКАКИЕ ЕГО НЕ РАБОТАЮТ ..... – elegance

+0

Aʌɐpɥɐuı, У меня СОМНУТЬ ... ЭТО ДОСТАТОЧНО В ОТНОШЕНИИ МЕНЬШЕЙ ПЛОЩАДЬ НАСТОЯЩЕГО ВМЕСТО ИЗОБРАЖЕНИЯ ??? ЭТО ЧТО Я СДЕЛАЛ. – elegance

+0

Aʌɐpɥɐuı, его работа !!!!! – elegance

1

нет необходимости в Java кодирования здесь. это ява кодирование требуется только для функциональности Swipe

XML-кодирование само по себе достаточно !!!, но в следующем формате

<HorizontalScrollView 
     android:id="@+id/horizontal_view_id" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/home_title_id" > 
    <LinearLayout 
    android:id="@+id/linear_layout_id" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 
     <ImageView 
      android:id="@+id/home_image_id" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/abc" /> 
    </LinearLayout> 
    </HorizontalScrollView> 

как горизонтальной прокрутки может содержать только один дочерний узел.

Смежные вопросы