SwipeRefreshLayout onRefresh方法不起作用

时间:2014-09-09 12:45:21

标签: android listview swiperefreshlayout

我将ListView添加到SwipeRefreshLayout。但是,onRefresh方法不起作用。 listview带有数据,我想listview的滚动会阻止onRefresh工作。我正在使用一种方法,当第一个项目可见时启用swipeContainer。你能检查我的代码并告诉我我做错了什么吗?

这是我的活动:

public class TimelineActivity extends  Activity {
    private SwipeRefreshLayout swipeContainer;
    JSONObject jsonobject;
    JSONObject jsonobj;
    CityList list;
    ArrayList<CityInfo> myList;
    SharedPreferences sp;
    JSONArray jArray = null;
    JSONArray jResult = null;
    Object info;
    String resultmsg = null;
    String json;
    ListView theListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        swipeContainer = (SwipeRefreshLayout) findViewById(R.id.swipeContainer);
        // Setup refresh listener which triggers new data loading
        swipeContainer.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                // Your code to refresh the list here.
                // Make sure you call swipeContainer.setRefreshing(false) when
                // once the network request has completed successfully.
                Log.i("refresh","refreshing");
                new DownloadJSON().execute();
            } 
        });

        ViewGroup vg = (ViewGroup) findViewById(R.id.mainlayoutarea);
        ViewGroup.inflate(TimelineActivity.this, R.layout.city_list, vg);
        setContentView(R.layout.city_list);
        theListView = (ListView) findViewById(R.id.cityList);

        theListView.setOnScrollListener(new OnScrollListener() {

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                    int visibleItemCount, int totalItemCount) {
                boolean enable = false;
                if(theListView != null && theListView.getChildCount() > 0){
                    // check if the first item of the list is visible
                    boolean firstItemVisible = theListView.getFirstVisiblePosition() == 0;
                    // check if the top of the first item is visible
                    boolean topOfFirstItemVisible = theListView.getChildAt(0).getTop() == 0;
                    // enabling or disabling the refresh layout
                    enable = firstItemVisible && topOfFirstItemVisible;
                }
                swipeContainer.setEnabled(enable);
            }
        });

        swipeContainer.setColorSchemeResources(android.R.color.holo_blue_light, 
                android.R.color.holo_green_light, 
                android.R.color.holo_orange_light, 
                android.R.color.holo_red_light);

        new DownloadJSON().execute();

    }

1 个答案:

答案 0 :(得分:0)

如果我没有误解,你必须继承SwipeRefreshLayout并实施canChildScrollUp。在ListView的情况下应该足以返回

firstVisibileItem != 0

编辑:(检查拼写错误)

public class MySwipeRefreshLayout extends SwipeRefreshLayout {

      private ListView mListView;

      public  boolean canChildScrollUp () {
           if (mListView == null) {
                mListView = (ListView) findViewById(R.id.idlistview);
           }
          return mListView.getFirstVisiblePosition() != 0;
      }


}