Android ListView with Pull to Refresh&加载更多 - 不推荐使用!(?)

时间:2016-03-09 01:50:42

标签: android listview

我正在尝试找到一个支持" Pull to Refresh"和#34;加载更多"与gradle一起使用,使用一些较旧的API(我目前使用" LoadMoreListView"需要min API18)并且不被弃用...我所能找到的只是不再关心的depreacted库。有什么建议吗?

2 个答案:

答案 0 :(得分:2)

AbsListView.OnScrollListener listenerScroll = new AbsListView.OnScrollListener() {

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        Log.d("TAG", "onScrollStateChanged");
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem,
                         int visibleItemCount, int totalItemCount) {

        boolean loadMore = firstVisibleItem + visibleItemCount >= totalItemCount;
        Log.d("TAG", "onScroll " + loadMore + " " + totalItemCount + " ");

        if (loadMore && !mIsLoading && !isLoadFinish) {

            if (mTrackDataValues != null && mTrackDataValues.size() > 0) {
                callAPIHistory(mTrackDataValues.get(mTrackDataValues.size() - 1).getDateFor());
            }

        }

    }
};

答案 1 :(得分:1)

您可以修改此代码以满足您的要求。该类使用SwipeRefreshLayout

public class MainActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener{
private ListView mListView;
private SwipeRefreshLayout mSwipeRefreshLayout;
private List<String> mObjectList;
private StringAdapter mStringAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mObjectList = new ArrayList<String>();
    mObjectList.add("Sweden");
    mObjectList.add("Denmark");
    mObjectList.add("Finland");
    mObjectList.add("Norway");
    mObjectList.add("Iceland");
    mListView = (ListView)findViewById(R.id.listView);
    mSwipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.swipe_refresh_layout);
    mStringAdapter = new StringAdapter(MainActivity.this, mObjectList);
    mListView.setAdapter(mStringAdapter);
    mSwipeRefreshLayout.setOnRefreshListener(this);
    mSwipeRefreshLayout.setDistanceToTriggerSync(30);
    mSwipeRefreshLayout.setSize(SwipeRefreshLayout.DEFAULT);
}
Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        //super.handleMessage(msg);

        // add new data source to the adapter

        mStringAdapter = new StringAdapter(MainActivity.this, mObjectList);
        mListView.setAdapter(mStringAdapter);
        mSwipeRefreshLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                mSwipeRefreshLayout.setRefreshing(false);
            }
        }, 1000);
    }
};
@Override
public void onRefresh() {
    mSwipeRefreshLayout.postDelayed(new Runnable() {
        @Override
        public void run() {
            mSwipeRefreshLayout.setRefreshing(true);
            mHandler.sendEmptyMessage(0);
        }
    }, 1000);
  }
}