2013-02-15 4 views
5

내 안드로이드 앱에 가로 및 세로 스크롤보기가 있습니다. 다음 버튼과 이전 버튼이 있습니다. scollview.isie가 필요한 경우에만이 버튼을 표시하고 scrollview 내용의 너비는 표시 너비를 초과합니다. 도달 할 때 이전 및 다음 버튼을 숨기려고합니다. 첫 번째 항목과 마지막 항목을 각각 클릭하십시오. 다음 버튼/이전 항목을 클릭하면 어떻게됩니까?안드로이드에서 버튼 클릭시 HorizontalScrollView를 스크롤하는 방법은 무엇입니까?

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' 값 내게 정확하지 않다. 최대 스크롤 값을 찾는 방법은 무엇입니까? 사전에 감사

답변

1
+0

죄송합니다.이 게시물이 내 문제를 어떻게 해결할 수 있는지 이해할 수 없습니다. –

+0

@DevuSoman은 내가 게시 한 링크를 보았습니다.이 방법으로 가로 스크롤보기가 작동합니다. 또한이 버튼을 숨기려면 첫 번째 조건에 대해 false로 enablity를 설정하고 반대의 경우에는 반대로 수행해야합니다. 각 버튼에 onclick 리스너와 선택에 따라 원하는 걸 할 .... –

+0

@DevuSoman 정확히 뭐야 .. 당신은 그냥 패딩 및 사소한 레이아웃 조정을 설정해야합니다. 내가 게시 한 링크로 해봤습니까? 당신의 버튼을 클릭 리스너에 적용하십시오. ur 선택에 따라. 당신은 클릭 할 때 무엇을하고 싶습니까? –

4

이것은 조금 늦을 수 있지만이 문제에 직면하게 될 사람에게는 대안 솔루션을 제안합니다.

먼저 HorizontalScrollView와 다른 구성 요소를 사용하십시오. 프로젝트에이 클래스를 추가 (com.yourproject.widgets 같은 것을 별도의 패키지를 만들 수) -Horizontal ListView가 :

옵션 1 : 다음은 옵션입니다. 또한 사용자 정의 어댑터를 작성해야합니다.이 내용은 example에서 어떻게 수행되는지보십시오. 별도의 어댑터 클래스 (exp. HorizontalListViewAdapter)를 생성하고 이미 생성 된 com.yourproject.widgets 패키지에 넣는 것이 좋습니다.

<com.yourproject.widgets.HorizontalListView android:id="@+id/hList" android:layout_width="match_parent" android:layout_height="wrap_content"/> 
  • 기준이 :

    • 당신은 같은 것을 추가하는 데 필요한 XML (스크롤 동작을 모방 할 필요가 버튼 사이에 넣어)에서 레이아웃에이 위젯을 추가 위젯을 이용하는 활동 /의 조각 (버튼과 함께)
    HorizontalListView mHList = (HorizontalListView) findViewById (R.id.hList); 
    Button bPrevoius = (Button) findViewById (R.id.btnPrevoius); 
    Button bNext = (Button) findViewById (R.id.btnNext); 
    
    • 버튼에 onClickListener를 추가하십시오. HorizontalListView 위젯에 미리 정의 된 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 : 더에 최신 솔루션까지 이 문제는 안드로이드 L에 추가 된 새로운 위젯 RecyclerView이 될 수 있습니다 (android : scrollbars = "vertical"은 트릭을 할 것 같은데, 기존의 ListView 동작이 있어야합니다). 자세한 내용은 공식 문서를 확인하십시오.

-1

티타늄 appcelerator, 당신은 당신은 모든 이미지 또는 당신이 가진 콘텐츠의 루프를 실행하고이보기에 추가 할 수 있습니다,

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

그런 다음이 사용 scrollableView을 할 수 있습니다.

for (루프) {
eval ("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);

관련 문제