//实现在fragment之上
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.astuetz.PagerSlidingTabStrip;
public class Fragment02 extends Fragment {
private PagerSlidingTabStrip tab;
private ViewPager vp;
String[] tabs = {"推荐", "热点", "体育", "娱乐", "社会", "汽车", "教育", "财经", "科技", "游戏"};
FragmentManager fm;
private MyVpAdapter adapter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment02, container, false);
tab = view.findViewById(R.id.tabs);
vp = view.findViewById(R.id.vp);
fm = getActivity().getSupportFragmentManager();
adapter = new MyVpAdapter(fm);
vp.setAdapter(adapter);
tab.setViewPager(vp);
return view;
}
class MyVpAdapter extends FragmentPagerAdapter {
public MyVpAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = new ContentFragment();
break;
case 1:
fragment = new ContentFragment();
break;
case 2:
fragment = new ContentFragment();
break;
case 3:
fragment = new ContentFragment();
break;
case 4:
fragment = new ContentFragment();
break;
case 5:
fragment = new ContentFragment();
break;
case 6:
fragment = new ContentFragment();
break;
case 7:
fragment = new ContentFragment();
break;
case 8:
fragment = new ContentFragment();
break;
case 9:
fragment = new ContentFragment();
break;
}
return fragment;
}
@Override
public int getCount() {
return tabs.length;
}
@Override
public CharSequence getPageTitle(int position) {
return tabs[position];
}
}
}
//解析数据来源
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;
import com.bwie.songjingxue20180108.Beans.Bean2;
import com.bwie.songjingxue20180108.R;
import com.bwie.songjingxue20180108.Utils.NetWorkUtils;
import com.google.gson.Gson;
import java.util.List;
public class ContentFragment extends Fragment {
private TextView vptv;
private ListView newslv;
private String[] urlS = {
"http://gank.io/api/data/%E7%A6%8F%E5%88%A9/10/1",
"http://gank.io/api/data/%E7%A6%8F%E5%88%A9/10/2",
"http://gank.io/api/data/%E7%A6%8F%E5%88%A9/10/3",
"http://gank.io/api/data/%E7%A6%8F%E5%88%A9/10/4",
"http://gank.io/api/data/%E7%A6%8F%E5%88%A9/10/5",
"http://gank.io/api/data/%E7%A6%8F%E5%88%A9/10/6",
"http://gank.io/api/data/%E7%A6%8F%E5%88%A9/10/7",
"http://gank.io/api/data/%E7%A6%8F%E5%88%A9/10/8",
"http://gank.io/api/data/%E7%A6%8F%E5%88%A9/10/9",
"http://gank.io/api/data/%E7%A6%8F%E5%88%A9/10/10"
};
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.content, container, false);
vptv = view.findViewById(R.id.vptv);
newslv = view.findViewById(R.id.newslv);
/*
Bundle arguments = getArguments();
String da = arguments.getString("data");*/
getJson();
return view;
}
public void getJson(){
new Task().execute(urlS[0]);
}
public class Task extends AsyncTask<String,Void,String>{
@Override
protected String doInBackground(String... strings) {
String str = NetWorkUtils.getJsonget(strings[0]);
return str;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Gson gson = new Gson();
Bean2 bean2 = gson.fromJson(s, Bean2.class);
List<Bean2.ResultsBean> resultsBeans = bean2.getResults();
vptv.setText(resultsBeans.get(0).getType());
}
}
}
//xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:id="@+id/vptv"
android:textColor="#0f0"
android:textSize="42sp"
android:text="123"/>
<ListView
android:id="@+id/newslv"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.astuetz.PagerSlidingTabStrip
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="48dip" />
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
</LinearLayout>