2013-02-15 57 views
5

我在我的android應用程序中使用Next和Previous按鈕的水平滾動視圖。我只想在scollview需要滾動時顯示這些按鈕。滾動視圖內容的寬度超過了顯示寬度。同時希望在到達時隱藏上一個和下一個按鈕第一個和最後一個項目。點擊這些按鈕時如何操作下一個/上一個項目?如何滾動按鈕上的Horizo​​ntalScrollView點擊Android?

main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/mainLayout" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="#ffffff" > 

     <Button 
      android:id="@+id/btnPrevoius" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_centerVertical="true" 
      android:text="Previous" 
      android:visibility="gone" /> 

     <HorizontalScrollView 
      android:id="@+id/horizontalScrollView1" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_centerVertical="true" 
      android:layout_marginLeft="5dip" 
      android:layout_marginRight="5dip" 
      android:layout_toLeftOf="@+id/btnNext" 
      android:layout_toRightOf="@+id/btnPrevoius" 
      android:fillViewport="true" > 

      <LinearLayout 
       android:id="@+id/linearLayout1" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:orientation="horizontal" > 
      </LinearLayout> 
     </HorizontalScrollView> 

     <Button 
      android:id="@+id/btnNext" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:layout_centerVertical="true" 
      android:text="Next" 
      android:visibility="gone" /> 

    </RelativeLayout> 

活動

public class SampleActivity extends Activity { 
      private static LinearLayout linearLayout; 
      private static HorizontalScrollView horizontalScrollView; 
      private static Button btnPrevious; 
      private static Button btnNext; 
      private static int displayWidth = 0; 
      private static int arrowWidth = 0; 
      @Override 
      public void onCreate(Bundle savedInstanceState) { 
       super.onCreate(savedInstanceState); 
       setContentView(R.layout.main); 
       horizontalScrollView = (HorizontalScrollView) findViewById(R.id.horizontalScrollView1); 
       linearLayout = (LinearLayout) findViewById(R.id.linearLayout1); 
       btnPrevious = (Button) findViewById(R.id.btnPrevoius); 
       btnNext = (Button) findViewById(R.id.btnNext); 
       for (int i = 0; i < 15; i++) { 
        Button button = new Button(this); 
        button.setTag(i); 
        button.setText("---"); 
        linearLayout.addView(button); 
       } 
       ViewTreeObserver vto = linearLayout.getViewTreeObserver(); 
       vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 

        @Override 
        public void onGlobalLayout() { 
         ViewTreeObserver obs = linearLayout.getViewTreeObserver(); 
         obs.removeGlobalOnLayoutListener(this); 
         Display display = getWindowManager().getDefaultDisplay(); 
         displayWidth = display.getWidth(); 
         if (linearLayout.getMeasuredWidth() > (displayWidth - 40)) { 
          btnPrevious.setVisibility(View.VISIBLE); 
          btnNext.setVisibility(View.VISIBLE); 
         } 
        } 

       }); 
       btnPrevious.setOnClickListener(listnerLeftArrowButton); 
       horizontalScrollView.setOnTouchListener(listenerScrollViewTouch); 
      } 

      private OnTouchListener listenerScrollViewTouch = new OnTouchListener() { 

       @Override 
       public boolean onTouch(View v, MotionEvent event) { 
        showHideViews(); 
        return false; 
       } 
      }; 

      private OnClickListener listnerLeftArrowButton = new OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        horizontalScrollView.onKeyDown(KeyEvent.KEYCODE_DPAD_RIGHT, new KeyEvent(0, 0)); 
       } 
      }; 


     public static void showHideViews() { 
      int maxScrollX = horizontalScrollView.getChildAt(0).getMeasuredWidth()- displayWidth; 
      Log.e("TestProjectActivity", "scroll X = " +horizontalScrollView.getScrollX()); 
      Log.i("TestProjectActivity", "scroll Width = " +horizontalScrollView.getMeasuredWidth()); 
      Log.d("TestProjectActivity", "Max scroll X = " + maxScrollX); 

      if (horizontalScrollView.getScrollX() == 0) { 
       hideLeftArrow(); 
      } else { 
       showLeftArrow(); 
      } 
      if (horizontalScrollView.getScrollX() == maxScrollX) { 
       showRightArrow(); 
      } else { 
       //hideRightArrow(); 
      } 
     } 

     private static void hideLeftArrow() { 
      btnPrevious.setVisibility(View.GONE); 
     } 

     private static void showLeftArrow() { 
      btnPrevious.setVisibility(View.VISIBLE); 
     } 

     private static void hideRightArrow() { 
      btnNext.setVisibility(View.GONE); 
     } 

     private static void showRightArrow() { 
      btnNext.setVisibility(View.VISIBLE); 
     } 
    } 

'maxScrollX'值是不適合我是正確的。 如何找到這個最大的scrollvalue? 由於提前

回答

1
+0

我很抱歉,我無法理解這些帖子如何解決我的問題。 – 2013-02-15 05:36:33

+0

@DevuSoman你看過我發佈的鏈接,水平滾動視圖以這種方式工作。同樣,如果你想隱藏這些按鈕,你已經設置enablity爲第一個條件爲false,反之亦然。爲了執行下一個之前,你必須申請onclick聽衆在各自的按鈕,並做你想要根據您的選擇... – 2013-02-15 05:43:16

+0

@DevuSoman什麼是你想要的..我只需要設置填充和次要佈局adjust.did你嘗試與我張貼的鏈接?只需應用點擊監聽器上的按鈕..根據你的選擇,你想要做點什麼? – 2013-02-15 05:51:22

4

這可能來得有些晚了,但那裏的人將面臨這個問題,我建議替代的解決方案(S)。

首先,使用與Horizo​​ntalScrollView不同的組件。下面是選項:

選項1:Horizontal ListView - 這個類添加到項目中(創建一個單獨的包,像com.yourproject.widgets)。您還需要創建自定義適配器,請參閱example中的完成方式。我建議你創建單獨的適配器類(exp。Horizo​​ntalListViewAdapter)並將其放入已創建的com.yourproject.widgets包中。

  • 在XML(把它需要模仿滾動行爲按鈕之間),這個小部件添加到您的佈局,您需要添加類似:
<com.yourproject.widgets.HorizontalListView 
      android:id="@+id/hList" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"/> 
  • 參考此(連同按鈕)在活動/片段,其利用微件
HorizontalListView mHList = (HorizontalListView) findViewById (R.id.hList); 
Button bPrevoius = (Button) findViewById (R.id.btnPrevoius); 
Button bNext = (Button) findViewById (R.id.btnNext); 
  • 將onClickListeners添加到按鈕。使用Horizo​​ntalListView小部件中預定義的scrollTo()函數。正如你在代碼中看到的那樣,它需要int dp值來滾動。如果你想在右(下)滾動添加正值,而如果你想在左(前)滾動使用負值:

    bPrevoius.setOnClickListener(new OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         //value 500 is arbitrarily given. if you want to achieve 
         //element-by-element scroll you should get the width of the 
         //previous element dynamically or if the elements of the 
         //list have uniform width just put that value instead 
         mHList.scrollTo(-500); 
         //if it's the first/last element you can bPrevoius.setEnabled(false) 
    
        } 
    }); 
    
    
    bNext.setOnClickListener(new OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         mHList.scrollTo(500); 
    
        } 
    }); 
    

選項2:更多的最新解決方案這個問題可以是在Android L中引入的新小部件RecyclerView(除了android:scrollbars =「vertical」外,它似乎可以做到這一點;除此之外應該具有常規的ListView行爲)。欲瞭解更多信息,請查看官方文檔。

-1

在鈦Appcelerator的,你可以做到這一點使用scrollableView

var scrollableView = Ti.UI.createScrollableView({ 
      showPagingControl:true, 
      scrollingEnabled: true,  
      top: 360        
}); 

然後,您可以運行所有圖像,或者您有任何內容的循環,並將其添加到了這一觀點。 (「var view」+ i +「= Ti.UI.createView();」);}};}}}}

profile_image = Ti.UI.createImageView({ 
       image: result[0]['profile_image'], 
       left:15, 
       width:82, 
       height:104, 
       top: 0 
      }); 

eval("view"+i+".add(profile_image);"); 

eval("scrollableView.addView(view"+i+");"); 

}

mywin.add(scrollableView);

相關問題