Fragment
public class FragmentShow extends Fragment implements IView{
private FlyBanner carousel_fly;
private PullToRefreshListView pull;
private MyPullBase adapter;
private Presenterimpl presenterimpl;
private int page;
private String PullUrl="http://www.xieast.com/api/news/news.php?page=%d";
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.show,container,false);
carousel_fly=view.findViewById(R.id.carousel_fly);
pull=view.findViewById(R.id.pull);
return view;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
carousel();
pull.setMode(PullToRefreshBase.Mode.BOTH);
adapter=new MyPullBase(getActivity());
pull.setAdapter(adapter);
presenterimpl=new Presenterimpl(this);
page=1;
pull.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
page=1;
load();
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
load();
}
});
load();
}
private void load() {
presenterimpl.setRequestData(String.format(PullUrl,page),JavaBean.class);
}
private void carousel() {
List<Integer> list=new ArrayList<>();
list.add(R.drawable.e);
list.add(R.drawable.a);
list.add(R.drawable.q);
list.add(R.drawable.w);
carousel_fly.setImages(list);
}
@Override
public void setData(Object data) {
JavaBean javaBean= (JavaBean) data;
if (page==1){
adapter.setList(((JavaBean) data).getData());
}else{
adapter.addList(((JavaBean) data).getData());
}
page++;
pull.onRefreshComplete();
}
}
适配器
class MyPullBase extends BaseAdapter {
private List<JavaBean.DataBean> mlist;
private Context context;
public MyPullBase(Context context) {
this.context = context;
mlist=new ArrayList<>();
}
public void setList(List<JavaBean.DataBean> list) {
mlist.clear();
mlist.addAll(list);
notifyDataSetChanged();
}
public void addList(List<JavaBean.DataBean> list) {
mlist.addAll(list);
notifyDataSetChanged();
}
@Override
public int getCount() {
return mlist.size();
}
@Override
public JavaBean.DataBean getItem(int position) {
return mlist.get(position);
}
@Override
public int getItemViewType(int position) {
if (position%2==0){
return 0;
}else {
return 1;
}
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView==null){
if (getItemViewType(position)==0){
convertView=View.inflate(context, R.layout.type1,null);
}else{
convertView=View.inflate(context,R.layout.type2,null);
}
holder=new ViewHolder();
holder.imageView1=convertView.findViewById(R.id.image1);
holder.imageView2=convertView.findViewById(R.id.image2);
holder.imageView3=convertView.findViewById(R.id.image3);
holder.text=convertView.findViewById(R.id.text);
convertView.setTag(holder);
}else{
holder= (ViewHolder) convertView.getTag();
}
holder.text.setText(getItem(position).getTitle());
ImageLoader.getInstance().displayImage(getItem(position).getThumbnail_pic_s(),holder.imageView1);
if (holder.imageView2!=null&&holder.imageView3!=null){
ImageLoader.getInstance().displayImage(getItem(position).getThumbnail_pic_s02(),holder.imageView2);
ImageLoader.getInstance().displayImage(getItem(position).getThumbnail_pic_s03(),holder.imageView3);
}
return convertView;
}
class ViewHolder{
ImageView imageView1,imageView2,imageView3;
TextView text;
}
}