2012-12-09 34 views
0

我書面方式申請,它看起來像這樣:鎖定水平滾動型 - Android電子

picture 1

怎麼可以看到有搜索欄在下方Horizo​​ntalScrolView頂部和幾個按鈕。我想用搜索欄,以滾動按鈕,現在這工作得很好,但我不能關閉默認的方式來滾動視圖(draginng屏幕)。我發現論壇similat話題:

Similar Topic

但問題是,這種方式鎖定整個滾動型,我不能被搜索欄和默認的方式使用它 - 也許我做錯事(我做到了就像在鏈接中一樣,即使我在mScrollable參數中有TRUE,也不是wort)。第二個問題是我的按鈕(它具有fill_parent高度參數)非常小。

問:還有就是要鎖定滾動視圖中默認的方式,並使用搜索欄來做到這一點沒有什麼好的辦法?

我的代碼(默認Horizo​​ntalScrollView):

public class MainActivity extends Activity implements SeekBar.OnSeekBarChangeListener { 

    static private String TAG = "MainActivity"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 

     SeekBar seek = (SeekBar) findViewById(R.id.seekBar); 
     final HorizontalScrollView hsv = (HorizontalScrollView)findViewById(R.id.hsv);  

     seek.setMax(948); 
     seek.setProgress(474); 
     seek.setOnSeekBarChangeListener(this); 
     hsv.post(new Runnable() { 
      @Override 
      public void run() { 
       hsv.scrollTo(474, 0); 
      } 
     }); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    } 

    @Override 
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) { 
     HorizontalScrollView hsv = (HorizontalScrollView)findViewById(R.id.hsv); 
     hsv.scrollTo(progress, 0); 
    } 

    @Override 
    public void onStartTrackingTouch(SeekBar seekBar) { 
     Log.d(TAG, "Tracking on"); 
    } 

    @Override 
    public void onStopTrackingTouch(SeekBar seekBar) { 
     Log.d(TAG, "Tracking off"); 

    } 

activity_main:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#0A0A0A" 
    android:orientation="vertical" > 

    <SeekBar 
     android:id="@+id/seekBar" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" /> 

    <HorizontalScrollView 
     android:id="@+id/hsv" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:scrollbars="none" > 

     <RelativeLayout 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:orientation="horizontal" > 

      <!-- BUTTONS CODE - NOT IMPORTANT --> 

     </RelativeLayout> 
    </HorizontalScrollView> 

</LinearLayout> 

回答

1

還有就是要鎖定滾動視圖中默認的方式,並使用 搜索欄來做到這一點沒有什麼好的辦法?

編輯:

的解決方案似乎要簡單得多,覆蓋HorizontalScrollView類,並通過視圖傳遞觸摸事件:

public class MyHScroll extends HorizontalScrollView { 

    // the constructors 

    @Override 
    public boolean onTouchEvent(MotionEvent ev) { 
     return false; 
    } 

} 

使用這個類的佈局,而不是默認HorizontallScrollView

<com.your.package.MyHScroll 
    android:id="@+id/hsv" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:scrollbars="none" > 

    <RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:orientation="horizontal" > 

     <!-- BUTTONS CODE - NOT IMPORTANT --> 

    </RelativeLayout> 
</com.your.package.MyHScroll> 


嘗試放置一個覆蓋圖上 HorizontalScrollView的頂部上以「吸收」觸摸事件,而不與 HorizontalScrollView本身搞亂:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#0A0A0A" 
    android:orientation="vertical" > 

    <SeekBar 
     android:id="@+id/seekBar" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" /> 
    <FrameLayout android:layout_width="wrap_content" 
     android:layout_height="fill_parent"> 

    <HorizontalScrollView 
     android:id="@+id/hsv" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:scrollbars="none" > 

     <RelativeLayout 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:orientation="horizontal" > 

      <!-- BUTTONS CODE - NOT IMPORTANT --> 

     </RelativeLayout> 
    </HorizontalScrollView> 

    <View android:id="@+id/overlay" android:layout_width="fill_parent" 
     android:layout_height="fill_parent" /> 

    </FrameLayout 

</LinearLayout> 

然後在onCreate方法進一步阻擋觸摸添加OnTouchListener用於疊加插件事件:

findViewById(R.id.overlay).setOnTouchListener(new OnTouchListener() { 

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

的第二個問題是,我的按鈕(具有高度FILL_PARENT參數 )是非常小的。

參見上面的佈局文件。

+0

這工作,解決自己的問題,但它使一個又一個 - 現在我不能使用的按鍵; /我嘗試使用onDragListener代替onTouchLister但它崩潰我的應用程序; /任何想法?我想過檢查X和Y,並檢查它應該是哪個按鈕,並做我的方法,但它不是太好,我認爲 – CookieMonssster

+1

@CookieMonssster是的,沒有想太多。我編輯了我的答案,看看是否有效。 – Luksprog

+0

THX給你很大。現在它工作正常;) – CookieMonssster