xml布局
主代码
package bwie.com.zhoukao3_lianxi.Frag;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import bwie.com.zhoukao3_lianxi.Adapter.MyBaseAdapter;
import bwie.com.zhoukao3_lianxi.R;
public class Frag1 extends Fragment {
private View view;
private TabLayout tab;
private ViewPager pager;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment1, container, false);
tab = view.findViewById(R.id.tab);
pager = view.findViewById(R.id.pager);
pager.setAdapter(new MyBaseAdapter(getChildFragmentManager()));
tab.setupWithViewPager(pager);
return view;
}
}
适配器
package bwie.com.zhoukao3_lianxi.Adapter;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import bwie.com.zhoukao3_lianxi.Frag.Fragment1;
import bwie.com.zhoukao3_lianxi.Frag.Fragment2;
import bwie.com.zhoukao3_lianxi.fragment.Fragment3;
import bwie.com.zhoukao3_lianxi.fragment.Fragment4;
import bwie.com.zhoukao3_lianxi.fragment.Fragment5;
public class MyBaseAdapter extends FragmentPagerAdapter {
String[] menu = new String[]{"要闻","深度","科普","学院","调研"};
public MyBaseAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
switch (i)
{
case 0:
return new Fragment1();
case 1:
return new Fragment2();
case 2:
return new Fragment3();
case 3:
return new Fragment4();
case 4:
return new Fragment5();
}
return null;
}
@Override
public int getCount() {
return menu.length;
}
public CharSequence getPageTitle(int position)
{
return menu[position];
}
}
Fragement的代码 xml布局 实现上拉刷新下拉加载
Fragement的代码 主代码 实现上拉刷新下拉加载
package bwie.com.zhoukao3_lianxi.Frag;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.gson.Gson;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import bwie.com.xlistviewlibrary.view.XListView;
import bwie.com.zhoukao3_lianxi.Adapter.MyAdapter;
import bwie.com.zhoukao3_lianxi.Bean.Bean;
import bwie.com.zhoukao3_lianxi.R;
public class Fragment1 extends Fragment
{
private View view;
private XListView xlv;
private int type = 1;
private int page = 1;
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.frag1, container, false);
xlv = view.findViewById(R.id.xlistview);
xlv.setPullRefreshEnable(true);
xlv.setPullLoadEnable(true);
xlv.setXListViewListener(new XListView.IXListViewListener() {
@Override
public void onRefresh() {
type = 1;
page = 1;
new MyAsy().execute();
}
@Override
public void onLoadMore() {
type = 2;
page++;
new MyAsy().execute("http://api.expoon.com/AppNews/getNewsList/type/1/p/"+page);
}
});
new MyAsy().execute();
return view;
}
class MyAsy extends AsyncTask {
private MyAdapter myAdapter;
@Override
protected String doInBackground(String... strings) {
String message = "";
String path = "http://api.expoon.com/AppNews/getNewsList/type/1/p/1";
try {
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
connection.setReadTimeout(4000);
connection.connect();
InputStream inputStream = connection.getInputStream();
byte[] b = new byte[1024];
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
int len = 0;
while ((len = inputStream.read(b)) != -1) {
outputStream.write(b, 0, len);
}
message = outputStream.toString();
inputStream.close();
connection.disconnect();
} catch (Exception e) {
}
return message;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Log.i("这是我解析的json", "s是不是为" + s);
if (s != null) {
Gson gson = new Gson();
//下面bean类 你看看
Bean bean = gson.fromJson(s, Bean.class);
List data = bean.getData();
if (type == 1) {
if (data != null) {
myAdapter = new MyAdapter(getActivity(), data);
xlv.setAdapter(myAdapter);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//获取当前时间
Date date = new Date(System.currentTimeMillis());
//时间格式
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String ss = format.format(date);
//设置刷新时间
xlv.setRefreshTime(ss);
//停止刷新
xlv.stopRefresh();
myAdapter.notifyDataSetChanged();
}
}, 1000);
}
}
if (type == 2) {
myAdapter = new MyAdapter(getActivity(), data);
xlv.setAdapter(myAdapter);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
xlv.stopLoadMore();
myAdapter.notifyDataSetChanged();
}
},1000);
}
}
}
}
}
下面是适配器 多条目加载
package bwie.com.zhoukao3_lianxi.Adapter;
import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.RecyclerView;
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 java.util.List;
import bwie.com.zhoukao3_lianxi.Bean.Bean;
import bwie.com.zhoukao3_lianxi.R;
public class MyAdapter extends BaseAdapter
{
Context context;
List data;
private ViewHolder holder;
public MyAdapter(Context context, List data) {
this.context=context;
this.data =data;
}
@Override
public int getViewTypeCount() {
return 3;
}
@Override
public int getItemViewType(int position) {
return position%3;
}
@Override
public int getCount() {
return data.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:
convertView = View.inflate(context,R.layout.frag1_1,null);
TextView name = convertView.findViewById(R.id.name);
name.setText(data.get(position).getNews_summary());
break;
case 1:
if(convertView == null)
{
holder = new ViewHolder();
convertView = View.inflate(context,R.layout.frag1_2,null);
holder.name = convertView.findViewById(R.id.name);
holder.img1 = convertView.findViewById(R.id.img);
holder.img2 = convertView.findViewById(R.id.img2);
convertView.setTag(holder);
}
else
{
holder =(ViewHolder) convertView.getTag();
}
holder.name.setText(data.get(position).getNews_summary());
ImageLoader.getInstance().displayImage(data.get(position).getPic_url(),holder.img1);
break;
case 2:
convertView = View.inflate(context,R.layout.frag1_3,null);
TextView title = convertView.findViewById(R.id.name);
ImageView img = convertView.findViewById(R.id.img);
ImageView img2 = convertView.findViewById(R.id.img2);
ImageView img3 = convertView.findViewById(R.id.img3);
ImageLoader.getInstance().displayImage(data.get(position).getPic_url(),img);
ImageLoader.getInstance().displayImage(data.get(position).getPic_url(),img2);
ImageLoader.getInstance().displayImage(data.get(position).getPic_url(),img3);
title.setText(data.get(position).getNews_summary());
break;
}
return convertView;
}
class ViewHolder
{
ImageView img1,img2;
TextView name;
}
}