0
点赞
收藏
分享

微信扫一扫

下拉刷新,上推加载(简单好用,终于会了)

​​https://github.com/scwang90/SmartRefreshLayout​​下拉刷新,上推加载(简单好用,终于会了)_下拉刷新

1

implementation 'androidx.appcompat:appcompat:1.0.0'                 //必须 1.0.0 以上

implementation 'io.github.scwang90:refresh-layout-kernel:2.0.5' //核心必须依赖
implementation 'io.github.scwang90:refresh-header-classics:2.0.5' //经典刷新头
implementation 'io.github.scwang90:refresh-header-radar:2.0.5' //雷达刷新头
implementation 'io.github.scwang90:refresh-header-falsify:2.0.5' //虚拟刷新头
implementation 'io.github.scwang90:refresh-header-material:2.0.5' //谷歌刷新头
implementation 'io.github.scwang90:refresh-header-two-level:2.0.5' //二级刷新头
implementation 'io.github.scwang90:refresh-footer-ball:2.0.5' //球脉冲加载
implementation 'io.github.scwang90:refresh-footer-classics:2.0.5' //经典加载

2

<com.scwang.smart.refresh.layout.SmartRefreshLayout
android:id="@+id/refresh_parent"
android:visibility="visible"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ListView
android:visibility="visible"
android:paddingLeft="13dp"
android:paddingRight="13dp"
android:paddingTop="17dp"
android:id="@+id/ship_news_lv"
android:divider="@drawable/window_divide_line"
android:background="@color/white"
android:layout_width="match_parent"
android:layout_height="match_parent" />

<TextView
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/dp_10"
android:textColor="@color/black_content"
android:layout_gravity="center"
android:layout_marginTop="@dimen/dp_10"
android:text="@string/no_content"
android:textSize="8sp" />
</LinearLayout>

</com.scwang.smart.refresh.layout.SmartRefreshLayout>

3

SmartRefreshLayout smartRefreshLayout;
int i=1;

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
smartRefreshLayout = getActivity().findViewById(R.id.refresh_parent);
//头部刷新样式
if (smartRefreshLayout != null) {
smartRefreshLayout.setRefreshHeader(new ClassicsHeader(getActivity()));
smartRefreshLayout.setRefreshFooter(new ClassicsFooter(getActivity()));
smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh(RefreshLayout refreshlayout) {
refreshlayout.finishRefresh(500/*,false*/);//传入false表示刷新失败
getData(1,12);
}
});

smartRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
@Override
public void onLoadMore(RefreshLayout refreshlayout) {
refreshlayout.finishLoadMore(500/*,false*/);//传入false表示加载失败
i++;
Log.e("wy", "onLoadMore i: "+i );
getData(i,12);
}
});
}


}

4 网络请求到数据的部分

ShipNewBean professionInfo = JSON.parseObject(result, ShipNewBean.class);
Log.e("wy", "onViewCreated onSuccess: " + professionInfo.getData().getRecords().size());
if (professionInfo.getCode().equals("00000000")) {
// List<ShipNewBean.DataDTO.RecordsDTO> records1 = professionInfo.getData().getRecords();
// recordsList1.addAll(records1);
List<ShipNewBean.DataDTO.RecordsDTO> records = professionInfo.getData().getRecords();
if(records.size()!=0){
shipNewsAdapter.addData(records);
shipNewsAdapter.notifyDataSetChanged();
}else {
smartRefreshLayout.finishLoadMore();
Toast.makeText(x.app(), R.string.no_more, Toast.LENGTH_SHORT).show();
}



//
shipNewsLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(getActivity(), ShipNewsActivity.class);
intent.putExtra("industryId", professionInfo.getData().getRecords().get(position).getIndustryId());
intent.putExtra("incrementId", professionInfo.getData().getRecords().get(position).getIncrementId());
intent.putExtra("incrementCode", 4);
Log.e("wy", "121 onItemClick professionInfo: " + professionInfo.getData().getRecords().get(position).getIncrementId());
intent.putExtra("homeType", "appIndustry");
// intent.putExtra("productId", professionInfo.getData().getRecords().get(position).getProductId());
startActivity(intent);

}
});

} else {
Toast.makeText(x.app(), professionInfo.getMsg(), Toast.LENGTH_SHORT).show();
}

5 ShipNewsAdapter 的正确姿势

public class ShipNewsAdapter extends BaseAdapter {
List<ShipNewBean.DataDTO.RecordsDTO> datas=new ArrayList<>();
Context context;

public ShipNewsAdapter( Context context) {
this.context = context;
}
public void addData(List<ShipNewBean.DataDTO.RecordsDTO> objects) {
// data.clear();
datas.addAll(objects);
}


@Override
public int getCount() {

return datas.size();
}

@Override
public Object getItem(int position) {
return null;
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.shipnews_lv_item, parent, false);

viewHolder.incrementNameTv = (TextView) convertView.findViewById(R.id.incrementName_tv);
viewHolder.releaseTimeTv = (TextView) convertView.findViewById(R.id.releaseTime_tv);

convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.incrementNameTv.setText(datas.get(position).getTitle());
viewHolder.releaseTimeTv.setText(datas.get(position).getReleaseTime());

return convertView;

}

private final class ViewHolder {
private TextView incrementNameTv;
private TextView releaseTimeTv;


}
}


举报

相关推荐

0 条评论