2013-02-15 14 views
5

私はアンドロイドアプリで横と前のボタンを使って横スクロールビューを表示しています。これらのボタンを表示するには、scollview.ieが必要な場合のみ、スクロールコンテンツの幅が表示幅を超えている必要があります。これらのボタンをクリックすると、次/前のアイテムへどうやって行くのですか?アンドロイドのボタンクリックでHorizo​​ntalScrollViewをスクロールする方法は?

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

devu

Plzをのおかげでは、以下のリンク

1を見てください)http://android-er.blogspot.in/2012/07/implement-gallery-like.html

2)http://androiddreamers.blogspot.in/2012/09/horizontal-scroll-view-example.html

3)http://code.google.com/p/mobyfactory-uiwidgets-android/

、私を知ってみましょうどんな問題にも直面する場合

おかげ

+0

申し訳ありません。これらの投稿がどのように問題を解決できるのか分かりませんでした。 –

+0

@DevuSomanは、私が投稿したリンクを見て、水平スクロールビューはこの方法で動作します。また、これらのボタンを非表示にしたい場合は、最初の条件としてfalseを設定しています。あなたの選択に応じて何をしたいのですか? –

+0

@DevuSoman正確に何をしたいのですか?私はちょうどパディングとマイナーなレイアウトの調整を設定する必要があります。私は投稿したリンクを試しましたか? url choice.whatに応じてボタンのクリックリスナーにクリックするだけで適用できますか? –

4

は、これは少し遅れて来るかもしれませんが、この問題に直面するだろうそこに誰のために私は別の解決策(複数可)をお勧めします。

まず、Horizo​​ntalScrollViewとは異なるコンポーネントを使用します。ここで、オプションは次のとおりです。

OPTION 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); 
  • ボタンにonClickListenerを追加します。 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); 
    
        } 
    }); 
    

OPTION 2:への最新のソリューションまで詳細この問題は、Android Lに導入された新しいウィジェットRecyclerView(アンドロイドの追加:scrollbars = "vertical"はトリックを行うようですが、これ以外の従来のListViewの動作が必要です)。詳細については公式文書をチェックしてください。

-1

は、チタンAppceleratorのでは、あなたは、すべての画像やあなたが持っているすべてのコンテンツのループを実行し、このビューに追加することができ、

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

次に、この使用scrollableViewを行うことができます。 (ループ){
評価するため

( "VARビュー" + 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)。

関連する問題