2013-11-20 66 views
2

我必須添加拉到刷新功能來刷新主屏幕上的信息。 Here`s我的屏幕UI的方案(紅色區域應處理好拉):ListView到ScrollView(拉到刷新)

enter image description here

我用ready solution爲拉來刷新。由於文檔,我的紅的佈局應該是這些類中的一種:

ListView 
ExpandableListView 
GridView 
WebView 
ScrollView 
HorizontalScrollView 
ViewPager 

但我有我的ListView屏幕上,所以我不能夠使用滾動型爲紅色的佈局。我堅持這個問題。可以在iOS中使用UITableView進入UIScrollView,但在Android中,我不知道在這種情況下應該做什麼。

任何幫助將不勝感激。謝謝!

回答

0

我已經解決了這個問題,卻忘了寫吧:)

layout.xml文件看起來像這樣:

<com.handmark.pulltorefresh.library.PullToRefreshScrollView 
     android:id="@+id/home_info_pulltorefresh" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fillViewport="true" > 

     <LinearLayout 
      android:id="@+id/home_info_layout" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="@color/white_background" 
      android:baselineAligned="false" 
      android:orientation="horizontal" > 

      <LinearLayout 
       android:id="@+id/charts_container" 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_weight="2" 
       android:orientation="vertical" 
       android:paddingRight="5dp" > 

       <FrameLayout 
        android:id="@+id/frame_container_chart_top" 
        android:layout_width="match_parent" 
        android:layout_height="0dp" 
        android:layout_weight="1" /> 

       <FrameLayout 
        android:id="@+id/frame_container_chart_bottom" 
        android:layout_width="match_parent" 
        android:layout_height="0dp" 
        android:layout_weight="1" /> 
      </LinearLayout> 

      <FrameLayout 
       android:id="@+id/frame_container_right" 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_weight="1" /> 
     </LinearLayout> 
    </com.handmark.pulltorefresh.library.PullToRefreshScrollView> 

android:fillViewport="true"被用來拉伸其內容以填充視口(請滾動match_parent的高度)

我添加包含的ListView片段編程(R.layout.frame_container_right片段資源ID)

一切工作正常,但當我試圖向下滾動ListView時,我的ScrollView開始滾動。滾動ListView工作正常。另外我注意到,如果我點擊ListView,將手指向左或向右移動,然後嘗試向下滾動,觸摸事件不會從ListView傳輸到ScrollView,並且我會得到預期的行爲。所以我決定以編程方式模擬這種情況:

mListViewReviews.setOnTouchListener(new OnTouchListener() { 

      @Override 
      public boolean onTouch(View v, MotionEvent event) { 

       if (event.getAction() == MotionEvent.ACTION_DOWN) { 
        long startTime = SystemClock.uptimeMillis(); 
        long duration = 1; 
        MotionEvent e = MotionEvent.obtain(startTime, startTime + duration, MotionEvent.ACTION_MOVE, event 
          .getX(), event.getY() + 10, 0); 
        MotionEvent ev = MotionEvent.obtain(startTime + duration, startTime + duration * 2, 
          MotionEvent.ACTION_MOVE, event.getX(), event.getY() + 20, 0); 

        v.dispatchTouchEvent(e); 
        v.dispatchTouchEvent(ev); 

       } 
       return false; 
      } 
     }); 

其結果是,我有工作與適當的單元複用和工作拉來刷新邏輯的ListView。謝謝!

+0

嘿UneXp,我在滾動時面臨同樣的問題,我已經嘗試過你的解決方案,但它不工作:( – Rajan

0

爲什麼不用一個LinearLayout替換listview,然後你可以使用ScrollView? 你只需要創建在LinearLayout中的項目佈局,然後使用像這樣將他們:

LinearLayout layout = (LinearLayout)findViewById(R.id.MyListLayout); 
for (int i=0; i<list.size(); i++) { 
    Item item = list.get(i); 
    View view = inflater.inflate(R.layout.MyRowLayout, null); 
    Textview myTextView = view.findViewById(R.id.MyTextView); 
    myTextView.setText(item.getString()); 
    list.addView(vi); 
} 
+0

感謝您的回答!不幸的是,我無法避免使用帶有單元重用的ListView,因爲可能會有大量的單元格。 – UneXp

+0

我知道這可能是一個痛苦的屁股,因爲ListView可以幫助你很多;但是,如果找不到更好的解決方案,可以嘗試手動給物品充氣(使用此選項作爲最後一個選項) – gian1200

+0

強烈建議不要「手動充氣」。如果物品的數量很大,則會顯着增加成本。 –

0

是拉來刷新水平或垂直?

如果它是水平的,使用Horizo​​ntalScrollView(一個ViewPager也可以),然後在其中放置一個Table?Layout。

如果它是水平的,那麼我不認爲我喜歡那種設計(拉到刷新區域應該只是ListView),但我相信有一些方法可以在不使用內部滾動的情況下使用ListView所以你可以依靠一個父ScrollView做滾動,但我需要檢查一個老問題的代碼來「記住」如何做到這一點。