1.app的build.gradle文件中加入依赖。
//smartRefreshLayout
implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.1.2' //1.0.5及以前版本的老用户升级需谨慎,API改动过大
implementation 'com.scwang.smartrefresh:SmartRefreshHeader:1.1.2' //没有使用特殊Header,可以不加这行
//关于recycleView的
def recyclerview_version = "1.0.0"
implementation "androidx.recyclerview:recyclerview:$recyclerview_version"
// For control over item selection of both touch and mouse driven selection
implementation "androidx.recyclerview:recyclerview-selection:$recyclerview_version"
2.使用 布局文件引入view。 SmartRefreshLayout里面的header和footer可加可不加。不加就是默认的。或者代码中指定。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/app_root_bg">
<com.scwang.smartrefresh.layout.SmartRefreshLayout
android:id="@+id/refreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<!--smartRefreshLayout里面的第一个view是hearder ,第二个是列表, 第三个view代表底部。如果里面只有一个view,那就是列表-->
<com.scwang.smartrefresh.header.MaterialHeader
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
/>
<com.scwang.smartrefresh.layout.footer.ClassicsFooter
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srlClassicsSpinnerStyle="Translate" />
</com.scwang.smartrefresh.layout.SmartRefreshLayout>
</RelativeLayout>
3.建立一个模型类。例如Job.java SucGetJob.java
import java.io.Serializable;
/**
* Filename: job.java <br>
*
*/
public class Job implements Serializable {
private String company_name;
private String jobs_name;
public Job() {
}
public String getJobs_name() {
return jobs_name;
}
public void setJobs_name(String jobs_name) {
this.jobs_name = jobs_name;
}
public String getCompany_name() {
return company_name;
}
public void setCompany_name(String company_name) {
this.company_name = company_name;
}
}
import java.io.Serializable;
import java.util.ArrayList;
/**
* Filename: SucGetJob.java <br>
* <p>
*/
public class SucGetJob implements Serializable {
private String result_code;
private String result_msg;
private String page_size;
private String page_num;
private String flag;
private ArrayList<Job> data;
public String getResult_code() {
return result_code;
}
public void setResult_code(String result_code) {
this.result_code = result_code;
}
public String getResult_msg() {
return result_msg;
}
public void setResult_msg(String result_msg) {
this.result_msg = result_msg;
}
public String getPage_size() {
return page_size;
}
public void setPage_size(String page_size) {
this.page_size = page_size;
}
public String getPage_num() {
return page_num;
}
public void setPage_num(String page_num) {
this.page_num = page_num;
}
public String getFlag() {
return flag;
}
public void setFlag(String flag) {
this.flag = flag;
}
public ArrayList<Job> getData() {
return data;
}
public void setData(ArrayList<Job> data) {
this.data = data;
}
}
4.编写adapter
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.xxx.wagesteward.R;
import com.xxx.wagesteward.bean.Job;
import java.util.List;
/**
* Filename: BankAdapter.java <br>
*
* Description: 我的钱包 钱包设置 适配器 <br>
*
* @author: HLJ <br>
* @version: 1.0 <br>
* @Createtime: 2015-5-19 <br>
*
* @Copyright: Copyright (c)2015 by HLJ <br>
*
*/
public class JobListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
private List<Job> jobslist;
private LayoutInflater inflater;
public JobListAdapter(Context context, List<Job> list) {
this.context = context;
this.inflater = LayoutInflater.from(context);
this.jobslist = list;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = inflater.inflate(R.layout.item_for_job_list, parent, false);
return new HolderView(itemView);
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
HolderView itemViewHolder = (HolderView) holder;
Job job=jobslist.get(position);
itemViewHolder.tv_jobname.setText(job.getJobs_name());
itemViewHolder.tv_fabu_company.setText(job.getCompany_name());
}
@Override
public long getItemId(int pos) {
return pos;
}
@Override
public int getItemCount() {
return jobslist.size();
}
private class HolderView extends RecyclerView.ViewHolder{
TextView tv_jobname;//
TextView tv_fabu_company;//
public HolderView(@NonNull View itemView) {
super(itemView);
tv_jobname = itemView.findViewById(R.id.tv_jobname);
tv_fabu_company = itemView.findViewById(R.id.tv_fabu_company);
}
}
}
5.activity里的用法,可以设置header和footer的样式,也可以不设置。然后拿到SmartRefreshLayout和RecyclerView控件,SmartRefreshLayout设置监听方法。RecyclerView设置adapter
下面代码里面的url根据自己实际情况而定。我这里删除了。注意点就是RecyclerView列表显示不出来要看看是否给列表设置了LayoutManager
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
/**
* 通讯录
*
* @author HLJ
*/
public class MainTab2Fragment extends BaseFragment implements View.OnClickListener {
private Context context;
private RefreshLayout mRefreshLayout;
private RecyclerView mRecyclerView;
private JobListAdapter mJobListAdapter;
private List<Job> mJobList;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
context = getActivity();
View contactsLayout = inflater.inflate(
R.layout.fragment_maintab2_layout, container, false);
initView(contactsLayout);
initData();
setListener();
return contactsLayout;
}
public MainTab2Fragment() {
super();
}
public static MainTab2Fragment getClientFragment() {
MainTab2Fragment clientFragment = new MainTab2Fragment();
return clientFragment;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
@Override
public void onResume() {
super.onResume();
}
@Override
public void initView(View view) {
mRefreshLayout=view.findViewById(R.id.refreshLayout);
mRecyclerView=view.findViewById(R.id.recyclerView);
LinearLayoutManager layoutManager = new LinearLayoutManager(context);
// layoutManager.setOrientation(LinearLayoutManager.VERTICAL); //设置方向
mRecyclerView.setLayoutManager(layoutManager);
//这里是设置每个item之间的高度和颜色
DividerItemDecoration decor = new DividerItemDecoration(context, layoutManager.getOrientation());
decor.setDrawable(ContextCompat.getDrawable(context,R.drawable.divide_hight)); //这里在就是
mRecyclerView.addItemDecoration(decor);
mJobList=new ArrayList<Job>();
mJobListAdapter=new JobListAdapter(context,mJobList);
mRecyclerView.setAdapter(mJobListAdapter);
//下面是设置下拉刷新和底部加载的一些样式和监听
mRefreshLayout.setDragRate(0.5f);//显示下拉高度/手指真实下拉高度=阻尼效果
mRefreshLayout.setReboundDuration(300);//回弹动画时长(毫秒)
mRefreshLayout.setHeaderHeight(100);//Header标准高度(显示下拉高度
// //设置 Header 为 Material风格
// mRefreshLayout.setRefreshHeader(new MaterialHeader(context).setShowBezierWave(true));
// //设置 Footer 为 球脉冲
// mRefreshLayout.setRefreshFooter(new BallPulseFooter(context).setSpinnerStyle(SpinnerStyle.Scale));
mRefreshLayout.setOnRefreshListener(new OnRefreshListener() { //下拉刷新
@Override
public void onRefresh(RefreshLayout refreshlayout) {
// refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
refreshlayout.finishRefresh(true);
}
});
mRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() { //上拉加载更多
@Override
public void onLoadMore(RefreshLayout refreshlayout) {
// refreshlayout.finishLoadMore(2000/*,false*/);//传入false表示加载失败
refreshlayout.finishLoadMore(true);
System.out.println("加载了");
}
});
}
@Override
public void initData() {
// 初始化
String url="";
getData(url);
}
@Override
public void setListener() {
// TODO Auto-generated method stub
// 搜索按钮
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
@Override
public void onDestroy() {
super.onDestroy();
}
private void getData(String url) {
OkhttpUtil.okHttpGet(url, new CallBackUtil.CallBackString() {
@Override
public void onFailure(Call call, Exception e) {
}
@Override
public void onResponse(String response) {
Toast.makeText(context, "Success", Toast.LENGTH_SHORT).show();
Log.d("kwwl", response);
if(response!=null)
{
SucGetJob sucGetJob = new Gson().fromJson(response, SucGetJob.class);
if(sucGetJob!=null)
{
// System.out.println(sucGetJob.getData().size());
if(sucGetJob.getData()!=null){
mJobList.addAll(sucGetJob.getData());
mJobListAdapter.notifyDataSetChanged();
}
}
}
}
});
}
private void getImageData(String url){
//获取图片要用okHttpGetBitmap
OkhttpUtil.okHttpGetBitmap(url, new CallBackUtil.CallBackBitmap() {
@Override
public void onFailure(Call call, Exception e) {
Log.d("call","e");
}
@Override
public void onResponse(Bitmap response) {
//TODO
Log.d("call","success");
// mMView.imageView.setImageBitmap(response);
}
});
}
private void postData(String url) {
HashMap<String, String> paramsMap = new HashMap<>();
paramsMap.put("audit", "1");
paramsMap.put("page_num", "1");
paramsMap.put("page_size", "20");
paramsMap.put("timestamp", "1584408111");
paramsMap.put("uid", "cd3b5bf2f58bc613de28bb2cfe");
paramsMap.put("sign", "5EB1BA87271888E13F49BE");
OkhttpUtil.okHttpPost(url, paramsMap, new CallBackUtil.CallBackString() {
@Override
public void onFailure(Call call, Exception e) {
}
@Override
public void onResponse(String response) {
// mMView.textview.setText(response);
}
});
}
}