【问题标题】:Swipe refresh to be called only when user swipes for it, not when user reaches top of list滑动刷新仅在用户滑动时调用,而不是在用户到达列表顶部时调用
【发布时间】:2016-12-23 06:07:44
【问题描述】:

https://github.com/googlesamples/android-SwipeRefreshLayoutBasic

参考上面的示例。当用户向上滚动到列表顶部时,将调用滑动刷新。我想要的是仅在用户专门为它滑动时才调用滑动刷新。当用户向上滚动并到达顶端时不调用滑动刷新。

当我使用下面的库时,我得到了我想要的。但是我遇到了这个库的一些问题,所以我不想使用它。 github.com/baoyongzhang/android-PullRefreshLayout

这是我不想要的视频剪辑: https://drive.google.com/file/d/0By2g3SV1qz5TUFdZZ1FUNmxzU1E/view?usp=sharing

【问题讨论】:

  • 除了阅读之外,您是否测试过该工具?当我到达列表顶部时,我不记得滑动刷新会触发。
  • 先生,当我到达此列表的顶部时,即使在此示例中,也会触发滑动刷新。
  • 您究竟想达到什么目的只是为了在您的项目中编辑示例或实现 swiperefresh
  • 请输入sn-p ..
  • 好的,等几分钟

标签: android android-support-library swiperefreshlayout


【解决方案1】:

现在从 xml 开始

<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/contact_swipe_refresh"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/contact_listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ListView>
</android.support.v4.widget.SwipeRefreshLayout>

在上面的代码中有一个listview在swipeRefreshLayout里面,把它放在你activity的layout里面

然后在你的活动中通过这个在活动类中声明它

SwipeRefreshLayout swipeRefreshLayout;
ListView listView;

然后在 onCreate 方法内部将 xml 代码分配给它

     swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.contact_swipe_refresh);
 listView = (ListView) findViewById(R.id.contact_listView);

然后将你的适配器放到 listview 中,并通过这个将 onrefreshlistener 设置为 swiperefreshlayout

swipeRefreshLayout.setOnRefreshListener(new 

    SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                   //call a method here which make a webservice call or what ever you are using to put data inside the listview then set adapter and call  

swipeRefreshLayout.setRefreshing(false);
                    }
                }
            });

【讨论】:

  • 我已经正确地完成了实现......请看一次视频剪辑。我不希望在到达列表顶部时触发此滑动刷新
【解决方案2】:
        listView.setOnScrollListener(new AbsListView.OnScrollListener() {

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            switch (scrollState) {
                case SCROLL_STATE_IDLE:
                    //scroll was stopped, let's show search bar again
                    break;
                case SCROLL_STATE_TOUCH_SCROLL:
                    //user is scrolling, let's hide search bar
                    break;
            }
        }
        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            if (firstVisibleItem > 0) {
                //user scrolled down, first element is hidden
                //you can add the swipe trigger statement anywhere
            }
            else {//else
            }
        }
    });

【讨论】:

    【解决方案3】:
            swipe.setOnRefreshListener(
                new SwipeRefreshLayout.OnRefreshListener() {
                    @Override
                    public void onRefresh() {
    
                        Toast.makeText(getContext(), "List Refreshed", Toast.LENGTH_SHORT).show();
    
                        swipe.setRefreshing(false); //Refresh view Close
                    }
                }
        );
    

    【讨论】:

    • 只需在带有 Id 的列表视图周围添加滑动刷新布局,您就可以使用上面的代码执行操作。正如你希望它在用户想要滑动时工作。不是当 listView 到达顶部时
    • 它没有按应有的方式工作。请检查我添加的视频剪辑...谢谢
    • 滑动的属性是当它到达列表顶部并且你已经到达列表顶部时它开始(来源:视频)。所以它被拉下来刷新。在 Gmail 应用中可以看到相同的属性。
    • 同意......但是当你有一个比屏幕大一两个元素的列表时就会出现问题。当用户在列表的底部时,当他试图立即向上滚动时会触发刷新,感觉很尴尬
    • 我猜你正在使用模拟器来运行你的应用程序。当你从上到下的列表。它只是击中顶部并且不会触发滑动。到达顶部后,您必须再次拉动以触发滑动。当您在手机上而不是在模拟器中使用它时,您可以获得差异。
    【解决方案4】:

    在这里,我试图解决你的问题

    activity_main.xml

    <android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/swipe_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >
    
                <TextView
                    android:id="@+id/infoText"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:gravity="center"
                    android:text="Pull Down to Refresh" />
    
                <ListView
                    android:id="@+id/listView"
                    android:layout_width="match_parent"
                    android:layout_height="424dp"
                    android:layout_marginTop="10dp" >
                </ListView>
            </LinearLayout>
    
        </ScrollView>
    
    </android.support.v4.widget.SwipeRefreshLayout>
    

    MainActivity.java

    public class MainActivity extends Activity implements OnRefreshListener {
    
        private SwipeRefreshLayout swipeView;
    
        private ListView listView;
        private ArrayAdapter<String> adapter;
    
        private String[] LIST_ITEM_TEXT_CITIES = { "Los Angeles", "Chicago",
                "Indianapolis", "San Francisco", "Oklahoma City", "Washington" };
    
        private String[] LIST_ITEM_TEXT_MORE_CITIES = { "Phoenix", "San Antonio",
                "San Jose", "Nashville", "Las Vegas", "Virginia Beach" };
    
        private List<String> cityList;
    
        // variable to toggle city values on refresh
        boolean refreshToggle = true;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            swipeView = (SwipeRefreshLayout) findViewById(R.id.swipe_view);
            swipeView.setOnRefreshListener(this);
            swipeView.setColorSchemeColors(Color.RED, Color.BLUE, Color.YELLOW,
                    Color.CYAN, Color.BLACK);
            swipeView.setDistanceToTriggerSync(20);// in dips
            swipeView.setSize(SwipeRefreshLayout.DEFAULT);// LARGE also can be used
    
            cityList = Arrays.asList(LIST_ITEM_TEXT_CITIES);
            listView = (ListView) findViewById(R.id.listView);
            adapter = new ArrayAdapter<String>(getApplicationContext(),
                    R.layout.list_item, cityList);
            listView.setAdapter(adapter);
            listView.requestLayout();
        }
    
        Handler handler = new Handler() {
            public void handleMessage(android.os.Message msg) {
    
                if (refreshToggle) {
                    refreshToggle = false;
                    cityList = Arrays.asList(LIST_ITEM_TEXT_MORE_CITIES);
                    adapter = new ArrayAdapter<String>(getApplicationContext(),
                            R.layout.list_item, cityList);
                } else {
                    refreshToggle = true;
                    cityList = Arrays.asList(LIST_ITEM_TEXT_CITIES);
                    adapter = new ArrayAdapter<String>(getApplicationContext(),
                            R.layout.list_item, cityList);
                }
                listView.setAdapter(adapter);
    
                swipeView.postDelayed(new Runnable() {
    
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(),
                                "city list refreshed", Toast.LENGTH_SHORT).show();
                        swipeView.setRefreshing(false);
                    }
                }, 1000);
            };
        };
    
        @Override
        public void onRefresh() {
    
            swipeView.postDelayed(new Runnable() {
    
                @Override
                public void run() {
                    swipeView.setRefreshing(true);
                    handler.sendEmptyMessage(0);
                }
            }, 1000);
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2010-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多