activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.design.widget.TabLayout android:id="@+id/tab" android:layout_width="match_parent" android:layout_height="50dp" app:tabGravity="center" app:tabIndicatorColor="#00d" app:tabMode="scrollable" app:tabSelectedTextColor="#ff0000" app:tabTextColor="#000"></android.support.design.widget.TabLayout> <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>
item.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"> <ListView android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="match_parent"></ListView> </LinearLayout>
item_item.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:id="@+id/te" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
MainActivity
import android.support.design.widget.TabLayout; 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.support.v7.app.AppCompatActivity; import android.os.Bundle; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private TabLayout tabLayout; private ViewPager vp; private List<String> list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tabLayout = (TabLayout) findViewById(R.id.tab); vp = (ViewPager) findViewById(R.id.vp); list = new ArrayList<>(); list.add("头条"); list.add("社会"); list.add("国内"); list.add("国际"); list.add("娱乐"); list.add("体育"); list.add("军事"); list.add("科技"); list.add("财经"); list.add("时尚"); vp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) { @Override public CharSequence getPageTitle(int position) { return list.get(position); } @Override public Fragment getItem(int position) { News news = new News(); Bundle bundle = new Bundle(); if (list.get(position).equals("头条")) { //fragment = new TouTiaoFragment(); bundle.putString("name", "top"); } else if (list.get(position).equals("社会")) { bundle.putString("name", "shehui"); } else if (list.get(position).equals("国内")) { bundle.putString("name", "guonei"); } else if (list.get(position).equals("国际")) { bundle.putString("name", "guoji"); } else if (list.get(position).equals("娱乐")) { bundle.putString("name", "yule"); } else if (list.get(position).equals("体育")) { bundle.putString("name", "tiyu"); } else if (list.get(position).equals("军事")) { bundle.putString("name", "junshi"); } else if (list.get(position).equals("科技")) { bundle.putString("name", "keji"); } else if (list.get(position).equals("财经")) { bundle.putString("name", "caijing"); } else if (list.get(position).equals("时尚")) { bundle.putString("name", "shishang"); } news.setArguments(bundle); return news; } @Override public int getCount() { return list.size(); } }); tabLayout.setupWithViewPager(vp); } }
News
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.BaseAdapter; import android.widget.ListView; import android.widget.TextView; import com.google.gson.Gson; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.List; /** * Created by lenovo on 2017/09/12. */ public class News extends Fragment { private ListView listView; private List<DataDataBean.ResultBean.DataBean> data; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.item, container, false); listView = view.findViewById(R.id.listview); return view; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); Bundle bundle = getArguments(); String string = bundle.getString("name", "top"); gethttp(string); } private void gethttp(String string) { final String path = "http://v.juhe.cn/toutiao/index?type=" + string + "&key=2f092bd9ce76c0257052d6d3c93c11b4"; AsyncTask<Void, Void, String> asyncTask = new AsyncTask<Void, Void, String>() { @Override protected String doInBackground(Void... voids) { String string = ""; try { URL url = new URL(path); HttpURLConnection con = (HttpURLConnection) url.openConnection(); // con.setConnectTimeout(5000); // con.setReadTimeout(5000); int code = con.getResponseCode(); if (code == 200) { InputStream is = con.getInputStream(); byte[] b = new byte[1024]; int l = 0; while ((l = is.read(b)) != -1) { String str = new String(b, 0, l); string += str; } } } catch (Exception e) { e.printStackTrace(); } return string; } @Override protected void onPostExecute(String s) { super.onPostExecute(s); Gson gson = new Gson(); DataDataBean bean = gson.fromJson(s, DataDataBean.class); data = bean.getResult().getData(); MyAdapter adapter = new MyAdapter(); listView.setAdapter(adapter); //listView.setOnItemClickListener(); } }; asyncTask.execute(); } class MyAdapter extends BaseAdapter { @Override public int getCount() { return data.size(); } @Override public Object getItem(int i) { return data.get(i); } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { Hand hand; if (view == null) { view = View.inflate(getActivity(), R.layout.item_item, null); hand = new Hand(); hand.te = view.findViewById(R.id.te); view.setTag(hand); } else { hand = (Hand) view.getTag(); } hand.te.setText(data.get(i).getTitle()); return view; } } class Hand { TextView te; } }