1:在布局中添加SwipeRefreshLayout和Listview组件
-
<?xml version="1.0" encoding="utf-8"?>
-
<RelativeLayout
-
style="@style/BaseStyle.White"
-
xmlns:android="http://schemas.android.com/apk/res/android">
-
-
-
<android.support.v4.widget.SwipeRefreshLayout
-
android:id="@+id/srl_refresh"
-
style="@style/BaseStyle">
-
-
-
<ListView
-
android:id="@+id/lv_person_goods"
-
style="@style/BaseStyle"
-
android:gravity="center"
-
android:horizontalSpacing="@dimen/margin_standard"
-
android:numColumns="2"
-
android:scrollbarStyle="outsideOverlay"
-
android:verticalSpacing="@dimen/margin_standard"/>
-
</android.support.v4.widget.SwipeRefreshLayout>
-
-
-
<TextView
-
android:id="@+id/tv_no_data"
-
style="@style/BaseStyle.FullWrap"
-
android:layout_marginTop="@dimen/textview_width_small"
-
android:gravity="center_horizontal"
-
android:text="暂无数据"
-
android:textColor="@color/text_title_standard"
-
android:textSize="@dimen/font_size_small"
-
android:visibility="gone"/>
-
-
</RelativeLayout>
2:在主页面使用
-
-
-
-
-
public class MySaleGoodsListViewActivity extends VolleyActivity implements IInit, IResponseHandler, IPagination, SwipeRefreshLayout.OnRefreshListener {
-
-
private int auctionID;
-
private GoodsInfoViewModel mViewModel;
-
private TextView mEmptyTV;
-
private ListView mListView;
-
private PersonGoodsListAdapter mAdapter;
-
private SwipeRefreshLayout mSwipeRefreshLayout;
-
-
@Override
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.activity_me_auction_goods_list);
-
-
init();
-
}
-
-
@Override
-
public void init() {
-
ActionBarUtil.setup(this, "我店铺的宝贝");
-
-
auctionID = getIntent().getIntExtra(EnumIntentKey.AUCTION_ID.toString(), 0);
-
-
mListView = (ListView) findViewById(R.id.lv_person_goods);
-
mEmptyTV = (TextView) findViewById(R.id.tv_no_data);
-
-
mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.srl_refresh);
-
mSwipeRefreshLayout.setOnRefreshListener(this);
-
mSwipeRefreshLayout.setColorSchemeResources(R.color.background_blue_standard, R.color.white, R.color.background_blue_standard, R.color.white);
-
-
mViewModel = new GoodsInfoViewModel();
-
fetchData(FIRST);
-
}
-
-
@Override
-
public void fetchData(int tag) {
-
GetGoodsListForSellParam param = new GetGoodsListForSellParam(Data.getUserID(), 1, mViewModel.getPageIndex(), Data.PAGE_SIZE_MEDIUM);
-
SquareApi.getGoodsListForSell(this, param, tag);
-
}
-
-
-
-
-
@Override
-
protected void onRestart() {
-
super.onRestart();
-
mViewModel.reset();
-
fetchData(FIRST);
-
}
-
-
@Override
-
public void updateUI(Object response, final int tag) {
-
if (response == null) return;
-
-
if (tag == FIRST) {
-
-
mViewModel.inflate(response);
-
-
-
if (mAdapter == null) {
-
-
mSwipeRefreshLayout.setRefreshing(false);
-
-
mAdapter = new PersonGoodsListAdapter(this, mViewModel.getList());
-
mListView.setAdapter(mAdapter);
-
-
-
-
-
mListView.setOnScrollListener(new AbsListView.OnScrollListener() {
-
@Override
-
public void onScrollStateChanged(AbsListView view, int scrollState) {
-
switch (scrollState) {
-
case SCROLL_STATE_IDLE:
-
-
if (mListView.getLastVisiblePosition() == mViewModel.getList().size() - 1) {
-
if (!mViewModel.isComplete()) {
-
fetchNewData(FIRST);
-
}
-
}
-
break;
-
}
-
}
-
-
@Override
-
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
-
}
-
});
-
-
mListView.setEmptyView(mEmptyTV);
-
-
} else {
-
mAdapter.notifyDataSetChanged();
-
}
-
-
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
-
@Override
-
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
-
Intent i = new Intent();
-
if (getIntent().getBooleanExtra(EnumIntentKey.IS_SELECT_LINK.toString(), false)) {
-
i.putExtra(EnumIntentKey.GOODS_ID.toString(), mViewModel.getList().get(position).getGoodsID());
-
i.putExtra(EnumIntentKey.GOODS_NAME.toString(), mViewModel.getList().get(position).getGoodsName());
-
setResult(RESULT_OK, i);
-
} else {
-
i.setClass(MySaleGoodsListViewActivity.this, AddAuctionGoodsActivity.class);
-
i.putExtra(EnumIntentKey.AUCTION_GOODS_ID.toString(), mViewModel.getList().get(position).getGoodsID());
-
i.putExtra(EnumIntentKey.IS_DO.toString(), true);
-
i.putExtra(EnumIntentKey.AUCTION_ID.toString(), auctionID);
-
startActivity(i);
-
}
-
finish();
-
}
-
});
-
}
-
}
-
-
@Override
-
public void fetchNewData(int tag) {
-
mViewModel.increasePageIndex();
-
fetchData(FIRST);
-
}
-
-
@Override
-
public void onRefresh() {
-
mViewModel.reset();
-
mAdapter = null;
-
fetchData(FIRST);
-
}
-
}
解析:
(下拉刷新)主页面需要实现SwipeRefreshLayout.OnRefreshListener接口,然后在
-
@Override
-
public void onRefresh() {
-
}
方法中实现更新
(上拉更多)上拉更多需要监听setOnScrollListener()方法
-
mListView.setOnScrollListener(new AbsListView.OnScrollListener() {
-
@Override
-
public void onScrollStateChanged(AbsListView view, int scrollState) {
-
switch (scrollState) {
-
case SCROLL_STATE_IDLE:
-
-
if (mListView.getLastVisiblePosition() == mViewModel.getList().size() - 1) {
-
if (!mViewModel.isComplete()) {
-
fetchNewData(FIRST);
-
}
-
}
-
break;
-
}
-
}
-
-
@Override
-
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
-
}
-
});