private View view;
private TabLayout tab;
private ViewPager vp;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fr1, container, false);
tab = (TabLayout) view.findViewById(R.id.tl_tab);
vp = (ViewPager) view.findViewById(R.id.vp);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
final List<String>list=new ArrayList<>();
list.add("数据新闻");
list.add("快讯");
list.add("头条");
list.add("精编公告");
list.add("美股");
list.add("港股");
vp.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
@Override
public Fragment getItem(int position) {
P1 p = new P1();
Bundle b=new Bundle();
b.putString("name",list.get(position));
p.setArguments(b);
return p;
}
@Override
public CharSequence getPageTitle(int position) {
return list.get(position);
}
@Override
public int getCount() {
return list.size();
}
});
tab.setupWithViewPager(vp);
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------
public class P1 extends Fragment {
private View view;
private Myadp adp;
private PullToRefreshListView lv;
private String name;
private int num = 1;
List<Bean.DataBean> data = new ArrayList<>();
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.p1, container, false);
lv = (PullToRefreshListView) view.findViewById(R.id.relist);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getdata(name);
lv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
getdata(name);
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
redata(name);
}
});
name = getArguments().getString("name");
if (name.equals("数据新闻")) {
name = "xbsjxw";
} else if (name.equals("快讯")) {
name = "txs";
} else if (name.equals("头条")) {
name = "toutiao";
} else if (name.equals("精编公告")) {
name = "news/mobile/jbgg";
} else if (name.equals("美股")) {
name = "news/mobile/mgxw";
} else if (name.equals("港股")) {
name = "news/mobile/ggxw";
}
}
private void redata(String name) {
num++;
String path = "http://mnews.gw.com.cn/wap/data/news/" + name + "/page_" + num + ".json";
Utilgetdata.getdata(path, getActivity(), new Myinterface() {
@Override
public void inter(String json) {
try {
JSONArray j = new JSONArray(json);
String string = j.getString(0);
Gson g = new Gson();
Bean b = g.fromJson(string, Bean.class);
data.addAll(b.getData());
setadpter();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
private void getdata(String name) {
String path = "http://mnews.gw.com.cn/wap/data/news/" + name + "/page_1.json";
Utilgetdata.getdata(path, getActivity(), new Myinterface() {
@Override
public void inter(String json) {
try {
JSONArray j = new JSONArray(json);
String string = j.getString(0);
Toast.makeText(getActivity(),"======"+string,Toast.LENGTH_SHORT).show();
Gson g = new Gson();
Bean b = g.fromJson(string, Bean.class);
data.addAll(0, b.getData());
setadpter();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
private void setadpter() {
if (adp == null) {
adp = new Myadp(data, getActivity());
lv.setAdapter(adp);
} else {
adp.notifyDataSetChanged();
}
}
}