xml布局代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.bawei.xlistviewlibrary.XListView
android:id="@+id/xListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
java代码;
public class Fragment21 extends Fragment implements XListView.IXListViewListener{
private XListView xListView;
private int count = 1;
private String path = "http://172.17.8.100/movieApi/movie/v1/findHotMovieList?page=1&count=";
private ArrayList<User.Result> list;
private MyAdapter myAdapter;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment21, container, false);
//找控件
xListView = (XListView) view.findViewById(R.id.xListView);
//允许上拉刷新下拉加载
xListView.setPullLoadEnable(true);
xListView.setPullRefreshEnable(true);
xListView.setXListViewListener(this);
//创建集合
list = new ArrayList<>();
getDate(count);
//适配器
myAdapter = new MyAdapter(getActivity(), list);
xListView.setAdapter(myAdapter);
return view;
}
//获取数据
private void getDate(int count) {
//调用封装类
HttpConUtils httpConUtils = new HttpConUtils();
httpConUtils.getpath(path + count);
httpConUtils.getstring(new HttpConUtils.GetDate() {
@Override
public void setstring(String s) {
//解析
Gson gson = new Gson();
User user = gson.fromJson(s, User.class);
//Toast.makeText(getActivity(),list.toString(),Toast.LENGTH_SHORT).show();
list.clear();
list.addAll(user.getResult());
myAdapter.getList(list);
}
});
}
//上拉刷新
@Override
public void onRefresh() {
list.clear();
getDate(1);
close();
}
//下拉加载
@Override
public void onLoadMore() {
count++;
getDate(count);
close();
}
//关闭
public void close() {
xListView.stopRefresh();
xListView.stopLoadMore();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date(System.currentTimeMillis());
xListView.setRefreshTime(""+simpleDateFormat.format(date));
}
}
adapter适配器:
public class MyAdapter extends BaseAdapter {
private Context context;
private ArrayList<User.Result> list;
public MyAdapter(Context context, ArrayList<User.Result> list) {
this.context = context;
this.list = list;
}
public void getList(ArrayList<User.Result> list) {
this.list = list;
notifyDataSetChanged();
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
convertView = View.inflate(context, R.layout.item_list, null);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.text);
holder.text2 = (TextView) convertView.findViewById(R.id.text2);
holder.image = (ImageView) convertView.findViewById(R.id.image);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(list.get(position).getName());
holder.text2.setText(list.get(position).getSummary());
Picasso.with(context).load(list.get(position).getImageUrl()).into(holder.image);
return convertView;
}
class ViewHolder {
TextView text, text2;
ImageView image;
}
}