【问题标题】:ListView into ScrollView (pull-to-refresh)ListView 变成 ScrollView(下拉刷新)
【发布时间】:2013-12-04 14:40:33
【问题描述】:

我必须添加下拉刷新功能来刷新主屏幕上的信息。这是我的屏幕 UI 的示意图(红色区域应该处理拉动):

我使用ready solution 进行下拉刷新。由于文档,我的红色布局应该是这些类之一:

ListView
ExpandableListView
GridView
WebView
ScrollView
HorizontalScrollView
ViewPager

但是我的屏幕上有 ListView,所以我不能使用 ScrollView 作为红色布局。我被这个问题困住了。在 iOS 中可以将 UITableView 用于 UIScrollView,但在 Android 中,我不知道在这种情况下该怎么做。

任何帮助将不胜感激。谢谢!

【问题讨论】:

    标签: android listview scrollview pull-to-refresh


    【解决方案1】:

    为什么不用LinearLayout替换listview,然后就可以使用ScrollView了? 您只需要为线性布局中的项目创建一个布局,然后使用以下内容添加它们:

    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);
    }
    

    【讨论】:

    • 感谢您的回答!不幸的是,我无法避免将 ListView 与单元格重用一起使用,因为可能会有大量单元格。
    • 我知道这可能会让人头疼,因为 ListView 对您有很大帮助;但是,如果您找不到更好的解决方案,您可以尝试手动充气(将此作为最后一个选项)
    • “手动充气”强烈不推荐。如果商品数量多,成本会大大增加。
    • 这会增加成本,但你怎么能这样做呢?改变设计?
    【解决方案2】:

    下拉刷新是水平的还是垂直的?

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

    如果它是水平的,好吧,我认为我不喜欢这种设计(下拉刷新区域应该只是 ListView),但我相信有一些方法可以在没有内部滚动的情况下使用 ListView,所以您可以依靠父 ScrollView 进行滚动,但我需要检查一个旧问题的代码以“记住”如何做到这一点。

    【讨论】:

      【解决方案3】:

      我已经解决了这个问题,但是忘记写了:)

      我的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" 用于拉伸其内容以填充视口(使滚动高度匹配父项)

      我以编程方式添加包含 ListView 的 Fragment(R.layout.frame_container_rightFragment 资源 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。谢谢!

      【讨论】:

      • Hey UneXp,我在滚动时遇到了同样的问题,我已经尝试过你的解决方案,但它不起作用:(
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-14
      • 1970-01-01
      • 1970-01-01
      • 2016-07-16
      • 1970-01-01
      • 1970-01-01
      • 2018-12-16
      相关资源
      最近更新 更多