/** 上拉刷新没有截图,有这功能
* 1、布局页面四个,主页面 和三个自定义页面(自己需要的)控件根据自己需要添加
* 2、Java代码类包括 主类 工具类 JSONBean封装类 适配器类
* 3、用到的权限
*
*
*
*
* 4、Android Studio 导包 Glide Gson 外部包xlistview_library
* 外部包导入顺序 File——new——import module——找到文件——finish。
* 接着 File——Project Stucture——选择项目——Depen~~——点击加号选第三个——选择项目
*/
public class Fragment01 extends Fragment {
@Override
private XListView listview;
private XAdpter adpter;
private List list;
private String url = "https://www.apiopen.top/meituApi?page=1";
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment01,container,false);
listview = view.findViewById(R.id.xlist);
listview.setPullLoadEnable(true);
//设置上拉和下拉
listview.setXListViewListener(new XListView.IXListViewListener(){
@Override
public void onRefresh() {
HttpUtils.getAsyncTask(url, new HttpUtils.ClaaBackA() {
@Override
public void getData(String s) {
//解析
Gson gson = new Gson();
JsonBean jsonBean = gson.fromJson(s, JsonBean.class);
list = jsonBean.getData();
//适配器
adpter = new XAdpter(list, getActivity());
listview.setAdapter(adpter);
listview.stopRefresh();
}
});
}
@Override
public void onLoadMore() {
HttpUtils.getAsyncTask(url, new HttpUtils.ClaaBackA() {
@Override
public void getData(String s) {
//这个方法在主线程中
//解析
Gson gson = new Gson();
JsonBean bean = gson.fromJson(s, JsonBean.class);
//本人的JsonBean是用Android studio工具快捷键Alt+S 快速生成的 本文省略
List list2 = bean.getData();
list.addAll(list2);
adpter.notifyDataSetChanged();
listview.stopLoadMore();
}
});
}
});
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//请求数据
HttpUtils.getAsyncTask(url, new HttpUtils.ClaaBackA() {
@Override
public void getData(String s) {
Gson gson = new Gson();
JsonBean bean = gson.fromJson(s, JsonBean.class);
list = bean.getData();
//适配器
adpter = new XAdpter(list,getActivity());
listview.setAdapter(adpter);
}
});
}
}
public class HttpUtils {
//创建接口
public interface ClaaBackA{
//写方法
void getData(String s);
}
//封装异步
public static void getAsyncTask(String url, final ClaaBackA backA){
new AsyncTask(){
@Override
protected String doInBackground(String... strings) {
return httpGet(strings[0]);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
backA.getData(s);
}
}.execute(url);
}
/**
* @param strUrl 网络接口
* @return :请求的 String 类型的数据
* @author: Mr.Zhao
* @data: 2019/1/2 16:24
* @Description: 请求网络数据的方法
*/
public static String httpGet(String strUrl) {
try {
URL url = new URL(strUrl);
//获取HttpURLConnection
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//设置为get请求(大写
connection.setRequestMethod("GET");
//设置主机超时时间
connection.setConnectTimeout(5000);
//得到数据
InputStream stream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
//拼接数据
StringBuilder builder = new StringBuilder();
String strr="";
while ((strr = reader.readLine())!=null){
builder.append(strr);
}
//关闭连接
connection.disconnect();
//返回数据
return builder.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
public class XAdpter extends BaseAdapter {
private List list;
private Context context;
public XAdpter(List list, Context context) {
this.list = list;
this.context = context;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
int type = getItemViewType(position);
switch (type){
case 0:
ViewHolder0 holder01 = null;
if (convertView==null){
convertView = View.inflate(context, R.layout.item01,null);
holder01 = new ViewHolder0();
holder01.textView0 = convertView.findViewById(R.id.textView01);
convertView.setTag(holder01);
}else{
holder01 = (ViewHolder0) convertView.getTag();
}
JsonBean.DataBean bean0 = list.get(position);
holder01.textView0.setText(bean0.getType());
break;
case 1:
ViewHolder1 holder1 = null;
if (convertView==null){
convertView = View.inflate(context,R.layout.item02,null);
holder1 = new ViewHolder1();
holder1.textView1 = convertView.findViewById(R.id.textView1);
holder1.imageView1 = convertView.findViewById(R.id.imageView1);
convertView.setTag(holder1);
}else{
holder1= (ViewHolder1) convertView.getTag();
}
JsonBean.DataBean bean1 = list.get(position);
holder1.textView1.setText(bean1.getType());
//设置图片
Glide.with(context).load("http://n.sinaimg.cn/sports/transform/237/w650h387/20190219/XUgA-htfpvza1481742.png").into(holder1.imageView1);
break;
case 2:
ViewHolder2 holder2 = null;
if (convertView == null){
convertView = View.inflate(context,R.layout.item03,null);
holder2 = new ViewHolder2();
holder2.textView2 = convertView.findViewById(R.id.textView2);
holder2.time2 = convertView.findViewById(R.id.textView22);
holder2.imageView2 = convertView.findViewById(R.id.imageView2);
convertView.setTag(holder2);
}else{
holder2= (ViewHolder2) convertView.getTag();
}
JsonBean.DataBean bean2 = list.get(position);
holder2.textView2.setText(bean2.getType());
holder2.time2.setText(bean2.getCreatedAt());
//设置图片
Glide.with(context).load("http://n.sinaimg.cn/sports/transform/237/w650h387/20190219/XUgA-htfpvza1481742.png").into(holder2.imageView2);
break;
}
return convertView;
}
class ViewHolder0{
TextView textView0;
}
class ViewHolder1{
TextView textView1;
ImageView imageView1;
}
class ViewHolder2{
TextView textView2;
ImageView imageView2;
TextView time2;
}
@Override
public int getViewTypeCount() {
return 3;
}
@Override
public int getItemViewType(int position) {
return position % 3;
}
}