1. 程式人生 > >PullList+多條目(困難版(輪播圖+資料展示))

PullList+多條目(困難版(輪播圖+資料展示))

1.Fragment頁面

public class PullFragment extends Fragment {

private PullToRefreshListView pull;
private PullBase pullBase;
private int page;
private String path="http://172.17.8.100/movieApi/movie/v1/findHotMovieList?count=10&page=%d";
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.pullfragment,container,false);
    pull = v.findViewById(R.id.pulllist);
    page = 1;
    pullBase = new PullBase(getActivity());
    //return super.onCreateView(inflater, container, savedInstanceState);
    return v;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    pull.setAdapter(pullBase);
    //設定重新整理載入
    pull.setMode(PullToRefreshBase.Mode.BOTH);
    pull.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
        @Override
        public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
            //重新整理
            page = 1;
            loadData();
        }

        @Override
        public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
            loadData();
        }
    });
    page=1;
    loadData();

}

public void loadData(){
    NetUtil.getJson(String.format(path, page), NewsBean.class, new NetUtil.CallBack<NewsBean>() {
        @Override
        public void sucesss(NewsBean o) {
            if(!NetUtil.isHasNet(getActivity())||!o.isEquels()||o==null){
                Toast.makeText(getActivity(),"不可用",Toast.LENGTH_SHORT).show();
            }else{
            if(page == 1){
                pullBase.setPlist(o.getResult());
                pullBase.setList(o.getResult());

            }else{
                pullBase.addList(o.getResult());
            }
            page++;
            //停止重新整理載入
            pull.onRefreshComplete();
            }
        }
    });
}
}

2.介面卡

public class PullBase extends BaseAdapter {

private Context context;
private List<NewsBean.ResultBean> plist;
private List<NewsBean.ResultBean> list;

public PullBase(Context context) {
    this.context = context;
    list = new ArrayList<>();
    plist = new ArrayList<>();
}

public void setList(List<NewsBean.ResultBean> lists){
    list.clear();
    list.addAll(lists);
    notifyDataSetChanged();
}
public void addList(List<NewsBean.ResultBean> lists){
    list.addAll(lists);
    notifyDataSetChanged();
}

public void setPlist(List<NewsBean.ResultBean> lists){
    plist.clear();
    plist.addAll(lists);
    notifyDataSetChanged();
}

public boolean hasPhoto(){
    return plist.size()>0;
}
@Override
public int getCount() {
    return hasPhoto()? list.size()+1:list.size();
}

@Override
public int getViewTypeCount() {
    return 2;
}

@Override
public int getItemViewType(int position) {
   return position==0&&hasPhoto()?0:1;
}

@Override
public NewsBean.ResultBean getItem(int position) {

    return list.get(hasPhoto()?position-1:position);
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if(getItemViewType(position) == 0){
        ViewBanner viewBanner;
        if(convertView == null){
            convertView =LayoutInflater.from(context).inflate(R.layout.lunbo,parent,false);
            viewBanner = new ViewBanner();
            viewBanner.banner = convertView.findViewById(R.id.pagera);
            convertView.setTag(viewBanner);
        }else{
            viewBanner = (ViewBanner) convertView.getTag();
        }
        viewBanner.lunBo(plist);


    }else{
        ViewHolder holder;
        if(convertView == null){
            convertView =LayoutInflater.from(context).inflate(R.layout.item,parent,false);
            holder = new ViewHolder();
            holder.img = convertView.findViewById(R.id.imageView);
            holder.text = convertView.findViewById(R.id.textView);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        holder.text.setText(getItem(position).getName());
        ImageLoader.getInstance().displayImage(getItem(position).getImageUrl(),holder.img);

    }
    return convertView;
}

class ViewBanner{
    FlyBanner banner;

    public void lunBo(List<NewsBean.ResultBean> plist) {
        List<String> list = new ArrayList<>();
        for (int i = 0;i<plist.size();i++){
            list.add(plist.get(i).getImageUrl());
        }
        banner.setImagesUrl(list);
    }
}
class ViewHolder{
    TextView text;
    ImageView img;
}
}

3.XML佈局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

<com.handmark.pulltorefresh.library.PullToRefreshListView
    android:id="@+id/pulllist"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    >

</com.handmark.pulltorefresh.library.PullToRefreshListView>

</android.support.constraint.ConstraintLayout>

輪播圖依賴

implementation ‘com.recker.flybanner:flybanner:1.3’