2010-11-07 157 views
119

對於我的應用程序的一部分,用戶會看到一個名稱列表,並要求他們按照他們認爲合適的方式進行分組。如何滾動到長滾動視圖佈局的頂部?

(注意,ListView控件代碼是從Android逐字複製的資源教程。我還沒有它定製我的需要,我只是想弄清楚如何使這項工作。)

的基本佈局是一個LinearLayout,包含一個ScrollView(在下面的代碼中稱爲「groupsScrollView」),包含一個RelativeLayout。我有一些按鈕和文本,然後是我的ListView,它顯示名稱列表。所有這些都高於可見屏幕區域,因此用戶可以垂直滾動查看全部內容。

這一切都很精美,除非頁面加載時總是預滾動到頁面中間的ListView頂部。我創建的文本和按鈕告訴用戶該做什麼不可見。

我可以抓住屏幕並向上滾動,這工作得很好,但我希望屏幕加載已經滾動到頂部。用戶不需要向上滾動即可查看新加載頁面的頂部。但是,我嘗試以編程方式滾動到屏幕頂部的所有內容都失敗了。

這裏是我的onCreate方法:

protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 

    setContentView(R.layout.groups); 

    mainScrollView = (ScrollView)findViewById(R.id.groupsScrollView); 

    //get the Bundle out of the Intent... 
    Bundle extras = getIntent().getExtras(); 
    mNames = extras.getStringArray("mNames"); 

    setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, mNames)); 

    ListView lv = getListView(); 
    lv.setTextFilterEnabled(true); 

    //This is the line I'm having issues with 
    mainScrollView.pageScroll(View.FOCUS_UP); 

    lv.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View view, 
      int position, long id) { 
      // When clicked, show a toast with the TextView text 
      Toast.makeText(getApplicationContext(), ((TextView) view).getText(), 
       Toast.LENGTH_SHORT).show(); 
     } 
     }); 
} 

線「mainScrollView.pageScroll(View.FOCUS_UP);有問題它不會導致錯誤,但它似乎並沒有做任何事情。我試過scrollTo(0,0),scrollDirection(View.FOCUS_UP)和其他所有我能想到的東西

由於我沒有收到錯誤,我必須假定這些滾動命令實際上是工作的,但它們是滾動到ListView的頂部,而不是滾動視圖或包含它的RelativeLayout的頂部,這似乎得到了谷歌自己對滾動的描述的證實To(int x,int y)方法,他們說:「這個版本也會限制滾動到我們孩子的範圍。」

因此,爲了使長問題更長,我如何在ScrollView中包含的屏幕上加載一堆視圖,然後以編程方式將整個事物滾動到頁面的頂部,以便用戶可以與它進行交互?

謝謝!

回答

202

嘗試

mainScrollView.fullScroll(ScrollView.FOCUS_UP); 

它應該工作。

+0

這爲我工作。謝謝 – Shafi 2014-07-14 11:52:52

+0

適用於我也。 :D – 2017-10-26 09:26:41

143

有同樣的問題,可能是某種錯誤。

即使從其他答案fullScroll(ScrollView.FOCUS_UP)沒有工作。
只有在我顯示對話框後立即致電scroll_view.smoothScrollTo(0,0)

+0

這個工作最初,但後來當我試圖一次又一次,這種方法並沒有給出令人滿意的結果。 – JaydeepW 2013-08-12 06:57:00

+12

我解決scroll_view.smoothScrollTo(0,0),當我們有側面listview在當時scrollview fullScroll(ScrollView.FOCUS_UP)將無法正常工作。 – 2013-10-28 08:56:57

+1

scroll_view.smoothScrollTo(0,0)對我來說比mainScrollView.fullScroll(ScrollView.FOCUS_UP)更好。謝謝! – marienke 2014-10-24 07:37:37

24

我面臨同樣的問題,當我使用滾動視圖查看翻轉或對話,這樣的情況下scrollViewObject.smoothScrollTo(0, 0)是爲我工作scrollViewObject.fullScroll(ScrollView.FOCUS_UP)返回false這種情況下,內部

Scroll Focus Top

+0

完美!謝謝:) – Alex 2017-01-23 16:35:29

45

scrollViewObject.fullScroll(ScrollView.FOCUS_UP)這工作得很好,但只與問題這條線是,當數據在scrollViewObject中填充時,您立即調用。您必須等待幾毫秒,直到數據填充完畢。試試這個代碼:

scrollViewObject.postDelayed(new Runnable() { 
    @Override 
    public void run() { 
     scroll.fullScroll(ScrollView.FOCUS_UP); 
    } 
}, 600); 

OR

scrollViewObject.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 
    @Override 
    public void onGlobalLayout() { 
     scrollViewObject.getViewTreeObserver().removeOnGlobalLayoutListene‌​r(this); 
     scrollViewObject.fullScroll(View.FOCUS_UP); 
    } 
}); 
+0

我正在處理相同的問題,其中滾動視圖應該滾動到頁面的底部,將其固定! – 2014-11-26 14:33:28

+0

而不是猜測您需要等待的「magic」x毫秒數,View對象提供了一個可以使用的發佈(Runnable)方法,一旦小組件在UI上呈現,它就會發布可運行內容。 – Ryan 2016-03-29 18:37:03

+0

@Ryan:post(Runnable)在Nexus 5(Genymotion)上始終不起作用 – Dika 2016-05-04 09:37:58

30

的主要問題對所有這些解決方案,有效的是,你正在嘗試當它不能在屏幕上繪製尚未拉昇的滾動。

您需要等待滾動視圖在屏幕上,然後將其移動到任何這些解決方案。這比Postdelay更好,因爲你不介意延遲。

// Wait until my scrollView is ready 
    scrollView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 
     @Override 
     public void onGlobalLayout() { 
      // Ready, move up 
      scrollView.fullScroll(View.FOCUS_UP); 
     } 
    }); 
+5

不錯的解決方案 - 不要忘記在'onGlobalLayout'方法的末尾刪除Global Listener,調用 'scrollView.getViewTreeObserver()。removeOnGlobalLayoutListener(this);' – Nilhcem 2014-06-29 16:23:09

+1

好的工作,比其他解決方案完美。 – ajey 2015-02-11 06:41:19

+1

完美的工作完美 – 2015-05-06 07:19:41

9
runOnUiThread(new Runnable(){ 
    @Override 
    public void run(){ 
     mainScrollView.fullScroll(ScrollView.FOCUS_UP); 
    } 
} 
17
final ScrollView scrollview = (ScrollView)findViewById(R.id.selected_place_layout_scrollview); 

     scrollview.post(new Runnable() { 
       public void run() { 
        scrollview.scrollTo(0, 0); 
       } 
      }); 
76

我有同樣的問題,這個固定。希望它可以幫助你。

listView.setFocusable(false); 
+2

在這個頁面上的所有解決方案中,這是唯一一個爲我工作的解決方案。 – TheIcemanCometh 2015-10-04 22:31:29

+8

這個解決方案也適用於我,我在一個滾動視圖中放置了一個listview,當它的適配器獲得新的項目時,listview將獲得焦點,然後listview將滾動到底部自動調整,setFoucsable(false)是一個非常好的方法修復這種情況。 – Sunderr 2015-10-16 14:27:09

+0

正確和簡單的解決方案,您節省了我的時間,謝謝! – Daryn 2015-11-05 10:20:31

23

很容易

ScrollView scroll = (ScrollView) findViewById(R.id.addresses_scroll); 
    scroll.setFocusableInTouchMode(true); 
    scroll.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS); 
+4

工作就像一個魅力!恕我直言,這是最乾淨的解決方案。 – 2016-03-20 09:34:20

+1

對我來說它沒有工作(第三行),解決方案是:scroll.fullScroll(View.FOCUS_UP); (包括你的前兩行) – medskill 2017-01-27 22:22:25

+0

@medskill,你好,你在xml文件中寫了這些嗎?我發現這隻適用於'onCreate()'。 – 2017-03-22 09:13:37

0
@SuppressWarnings({ "deprecation", "unchecked" }) 
public void swipeTopToBottom(AppiumDriver<MobileElement> driver) 
        throws InterruptedException { 
     Dimension dimensions = driver.manage().window().getSize(); 
     Double screenHeightStart = dimensions.getHeight() * 0.30; 
     int scrollStart = screenHeightStart.intValue(); 
     System.out.println("s="+scrollStart); 
     Double screenHeightEnd = dimensions.getHeight()*0.90; 
     int scrollEnd = screenHeightEnd.intValue(); 
     driver.swipe(0,scrollStart,0,scrollEnd,2000); 
     CommonUtils.threadWait(driver, 3000); 
    } 
+0

肯定會運行, – 2017-07-18 12:11:35

+0

請提供關於此答案如何解決問題的簡要說明。 – 2017-07-18 16:13:23

2

這是力滾動的滾動型

  scrollView.post(new Runnable() { 
       @Override 
       public void run() { 
        scrollView.scrollTo(0, 0); 
        scrollView.pageScroll(View.FOCUS_UP); 
        scrollView.smoothScrollTo(0,0); 
       } 
      }); 
0
scrollView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 
    @Override 
    public void onGlobalLayout() { 
     // Ready, move up 
     scrollView.fullScroll(View.FOCUS_UP); 
    } 
});