滑动刷新仅在用户滑动时才调用,而不是在用户到达列表顶部时调用

时间:2016-12-23 06:07:44

标签: android android-support-library swiperefreshlayout

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

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

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

以下是我不想要的视频片段: https://drive.google.com/file/d/0By2g3SV1qz5TUFdZZ1FUNmxzU1E/view?usp=sharing

4 个答案:

答案 0 :(得分: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>

在上面的代码中,swipeRefreshLayout中有一个listview,把它放在你活动的行列里面

然后在你的活动中通过活动类

进行声明
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);
                    }
                }
            });

答案 1 :(得分:1)

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

答案 2 :(得分:0)

        swipe.setOnRefreshListener(
            new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {

                    Toast.makeText(getContext(), "List Refreshed", Toast.LENGTH_SHORT).show();

                    swipe.setRefreshing(false); //Refresh view Close
                }
            }
    );

答案 3 :(得分:0)

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

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

}