activity_xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" > <android.support.design.widget.TabLayout android:id="@+id/tab" app:tabGravity="center" app:tabIndicatorColor="@color/colorAccent" app:tabMode="scrollable" app:tabSelectedTextColor="@color/colorPrimaryDark" app:tabTextColor="@color/colorPrimary" android:layout_width="match_parent" android:layout_height="40dp"> </android.support.design.widget.TabLayout> <android.support.v4.view.ViewPager android:layout_below="@+id/tab" android:id="@+id/view_pager" android:layout_width="match_parent" android:layout_height="match_parent"></android.support.v4.view.ViewPager> </RelativeLayout> fragment_layout<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/list_view" android:layout_width="match_parent" android:layout_height="match_parent"></ListView> </LinearLayout>Mainactivity.javapackage news1510a.bawei.com.a12_tablayout_viewpager; import android.os.Bundle; import android.support.design.widget.TabLayout; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import com.example.a12_tabayout_viewpager.NewsFragment; import com.example.a12_tabayout_viewpager.R; import java.util.ArrayList; import java.util.List; /** * 二.tablayout结合viewPager使用 * * 参考博客:http://www.jb51.net/article/120336.htm * * */ public class MainActivity extends AppCompatActivity { private TabLayout tabLayout; private ViewPager viewPager; private List<String> list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tabLayout = (TabLayout) findViewById(R.id.tab); viewPager = (ViewPager) findViewById(R.id.view_pager); list = new ArrayList<>(); list.add("头条"); list.add("军事"); list.add("娱乐"); list.add("新闻"); list.add("财经"); list.add("体育"); list.add("科技"); list.add("游戏"); list.add("汽车"); list.add("环保"); list.add("视频"); list.add("国内"); //1.给viewPager设置适配器 /** * 管理者对象有几种???? * getSupportFragmentManager()...activity管理他身上的fragment的时候使用 * getChildFragmentManager()...fragment嵌套的时候,,,管理孩子需要使用这个管理者 * getFragmentManager()....孩子里面还有fragment的话就使用这个 */ viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) { //2.重写这个方法getPageTitle,,,得到当前页面的标题 @Override public CharSequence getPageTitle(int position) { return list.get(position); } @Override public Fragment getItem(int position) { NewsFragment newsFragment = new NewsFragment(); //应该要做的是传值,,,,去fragment里面获取,,,获取到值之后,,,在进行url路径的拼接 Bundle bundle = new Bundle(); bundle.putString("name","头条"); newsFragment.setArguments(bundle); return newsFragment; } @Override public int getCount() { return list.size(); } }); //3.将tabLayout和viewPager关联起来 tabLayout.setupWithViewPager(viewPager); } }Newsfragment.javapackage news1510a.bawei.com.a12_tablayout_viewpager; import android.os.AsyncTask; import android.os.Bundle; 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 android.widget.ListView; import com.example.a12_tabayout_viewpager.DataDataBean; import com.example.a12_tabayout_viewpager.MyAdapter; import com.example.a12_tabayout_viewpager.R; import com.google.gson.Gson; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; /** * @author Dash * @date 2017/10/12 * @description: */ public class NewsFragment extends Fragment { private ListView listView; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_layout,container,false); listView = view.findViewById(R.id.list_view); return view; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); //获取数据 String name = getArguments().getString("name", ""); //判断 if (name.equals("头条")){ name = "top"; }else if (name.equals("军事")){ name = "junshi"; }else if (name.equals("社会")){ name = "shehui"; }else if (name.equals("财经")){ name = "caijing"; }else if (name.equals("国内")){ name = "guonei"; } getDataFromNet(name); } private void getDataFromNet(final String name) { AsyncTask<Void, Void, String> asyncTask = new AsyncTask<Void, Void, String>() { @Override protected String doInBackground(Void... voids) { try { URL url = new URL("http://v.juhe.cn/toutiao/index?type="+name+"&key=c4479ad58f41e7f78a8fa073d0b1f1b5"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setReadTimeout(5000); connection.setConnectTimeout(5000); //获取 int responseCode = connection.getResponseCode(); if (responseCode == 200){ InputStream inputStream = connection.getInputStream(); String json = streamToString(inputStream,"utf-8"); //打印字符串 Log.i("---",json); return json; } } catch (Exception e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(String s) { Gson gson = new Gson(); DataDataBean dataDataBean = gson.fromJson(s, DataDataBean.class); MyAdapter myAdapter = new MyAdapter(getActivity(), dataDataBean.getResult().getData()); listView.setAdapter(myAdapter); } }; asyncTask.execute(); } private String streamToString(InputStream inputStream,String charset) { try { InputStreamReader inputStreamReader = new InputStreamReader(inputStream,charset); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); String s = null; StringBuilder builder = new StringBuilder(); while ((s = bufferedReader.readLine()) != null){ builder.append(s); } bufferedReader.close(); return builder.toString(); } catch (Exception e) { e.printStackTrace(); } return null; } }