导依赖
implementation project(':library')
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
MainAcitivty单纯的展示
public class MainActivity extends AppCompatActivity {
private PullToRefreshGridView gridView;
private GridAdapter adapter;
private int mpage=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView=findViewById(R.id.gridview);
adapter = new GridAdapter(MainActivity.this);
gridView.setAdapter(adapter);
gridView.setMode(PullToRefreshBase.Mode.BOTH);
GridView refreshableView = gridView.getRefreshableView();
refreshableView.setNumColumns(2);//设置展示个数
gridView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<GridView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<GridView> refreshView) {
mpage=1;
getData();
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) {
getData();
}
});
getData();
}
public void getData(){
NetUtil.getIntance().requestData("http://api.expoon.com/AppNews/getNewsList/type/1/p/" + mpage, ContentBean.class, new NetUtil.CallBack<ContentBean>() {
@Override
public void onsuccess(ContentBean contentBean) {
if(mpage==1){
adapter.setMlist(contentBean.getData());
}else{
adapter.addMlist(contentBean.getData());
}
mpage++;
gridView.onRefreshComplete();
}
});
}
}
GridAdapter
public class GridAdapter extends BaseAdapter {
private Context context;
private List<ContentBean.DataBean> mlist;
public GridAdapter(Context context) {
this.context = context;
mlist=new ArrayList<>();
}
public void setMlist(List<ContentBean.DataBean> mlist) {
this.mlist = mlist;
notifyDataSetChanged();
}
public void addMlist(List<ContentBean.DataBean> list){
if(list!=null){
mlist.addAll(list);
}
notifyDataSetChanged();
}
@Override
public int getCount() {
return mlist.size();
}
@Override
public ContentBean.DataBean getItem(int position) {
return mlist.get(position);
}
@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.item,parent,false);
viewholder.imageView=convertView.findViewById(R.id.image);
viewholder.textView=convertView.findViewById(R.id.text);
convertView.setTag(viewholder);
}else{
viewholder= (Viewholder) convertView.getTag();
}
ImageLoader.getInstance().displayImage(getItem(position).getPic_url(),viewholder.imageView);
viewholder.textView.setText(getItem(position).getNews_title());
return convertView;
}
class Viewholder{
ImageView imageView;
TextView textView;
}
}
工具类就不写了