//Main
public class Tone3 extends Fragment {
private PullToRefreshListView pull;
private Mypulladapter adapter;
private int page=1;
private final int hang=4;
private String path="http://172.17.8.100/movieApi/movie/v1/findHotMovieList?count=%d&page=%d";
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.one3,container,false);
pull=view.findViewById(R.id.pull);
return view;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
adapter=new Mypulladapter(getActivity());
pull.setAdapter(adapter);
pull.setMode( PullToRefreshBase.Mode.BOTH);
pull.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
page=1;
load();
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
load();
}
});
page=1;
load();
}
private void load() {
BeanUtil.setstring(String.format(path,hang,page),Bean.class, new BeanUtil.Callback<Bean>() {
@Override
public void setsuccess(Bean o) {
if (page==1) {
adapter.setmlist(o.getResult());
adapter.setbanner(o.getResult());
}else {
adapter.addmlist(o.getResult());
}
page++;
pull.onRefreshComplete();
}
});
}
}
//适配器
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.recker.flybanner.FlyBanner;
import java.util.ArrayList;
import java.util.List;
class Mypulladapter extends BaseAdapter {
private List<Bean.ResultBean> mbanner;
private List<Bean.ResultBean> mlist;
private Context context;
public Mypulladapter(Context context){
this.context=context;
mbanner=new ArrayList<>();
mlist=new ArrayList<>();
}
public void setmlist(List<Bean.ResultBean> list){
mlist.clear();
if (list!=null){
mlist.addAll(list);
}
notifyDataSetChanged();
}
public void addmlist(List<Bean.ResultBean> list){
if (list!=null){
mlist.addAll(list);
}
notifyDataSetChanged();
}
public void setbanner(List<Bean.ResultBean> banner){
mbanner.clear();
if (banner!=null){
mbanner.addAll(banner);
}
notifyDataSetChanged();
}
public boolean hasbeanner(){
return mbanner.size()>0;
}
@Override
///
public int getCount() {
return hasbeanner() ? mlist.size()+1:mlist.size();
}
@Override
public Bean.ResultBean getItem(int position) {
///
return mlist.get(hasbeanner() ? position - 1 : position);
}
private final int Count=2;
private final int type1=0;
private final int type2=1;
@Override
public int getViewTypeCount() {
return Count;
}
@Override
public int getItemViewType(int position) {
return position==0&&hasbeanner()? type1:type2;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (getItemViewType(position)==type1){
BannerHolder holder;
if (convertView==null){
convertView=View.inflate(context,R.layout.bannertype,null);
holder=new BannerHolder();
holder.fly=convertView.findViewById(R.id.fly3);
convertView.setTag(holder);
}else{
holder= (BannerHolder) convertView.getTag();
}
holder.setBanners(mbanner);
}else{
ListViewHloder holder;
if (convertView==null){
convertView=View.inflate(context,R.layout.listtype,null);
holder=new ListViewHloder();
holder.image=convertView.findViewById(R.id.image1);
holder.text=convertView.findViewById(R.id.text);
convertView.setTag(holder);
}else {
holder= (ListViewHloder) convertView.getTag();
}
holder.text.setText(getItem(position).getSummary());
ImageLoader.getInstance().displayImage(getItem(position).getImageUrl(),holder.image);
}
return convertView;
}
class BannerHolder{
FlyBanner fly;
public void setBanners(List<Bean.ResultBean> mbanner) {
List<String> banner=new ArrayList<>();
for (int i = 0; i <mbanner.size() ; i++) {
banner.add(mbanner.get(i).getImageUrl());
}
fly.setImagesUrl(banner);
}
}
class ListViewHloder{
ImageView image;
TextView text;
}
}
//两个路径
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.Toast;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;
public class Tone1 extends Fragment {
private PullToRefreshGridView pull;
private MyBase adapter;
private int page=1;
private String bpath="http://172.17.8.100/movieApi/movie/v1/findHotMovieList?count=10&page=1";
private String lpath="http://172.17.8.100/movieApi/movie/v1/findReleaseMovieList?count=10&page=%d";
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.one1,container,false);
pull=view.findViewById(R.id.pull);
return view;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
page=1;
adapter=new MyBase(getActivity());
pull.setAdapter(adapter);
pull.setMode(PullToRefreshBase.Mode.BOTH);
pull.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<GridView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<GridView> refreshView) {
page=1;
haslb();
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) {
haslb();
}
});
haslb();
}
private void haslb(){
if (page==1){
load();
loads();
}else {
load();
}
}
private void loads() {
Util.setstring(bpath,BannerBean.class, new Util.Callback<BannerBean>() {
@Override
public void setsuccess(BannerBean o) {
if (!o.hasml()||o==null){
Toast.makeText(getActivity(),"无网络",Toast.LENGTH_SHORT).show();
}else {
adapter.setbanner(o.getResult());
}
}
});
}
private void load() {
Util.setstring(String.format(lpath,page),ListBean.class, new Util.Callback<ListBean>() {
@Override
public void setsuccess(ListBean o) {
if (!o.haswl() || o == null) {
Toast.makeText(getActivity(), "无网络", Toast.LENGTH_SHORT).show();
} else {
if (page == 1) {
adapter.setdata(o.getResult());
} else {
adapter.adddata(o.getResult());
}
page++;
pull.onRefreshComplete();
}
}
});
}
}