package fragment.bwie.com.rikao11;
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 java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private TabLayout tabLayout;
private List list;
private ViewPager pager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabLayout = (TabLayout) findViewById(R.id.tablayout);
pager = (ViewPager) findViewById(R.id.pager);
list = new ArrayList<>();
list.add("头条");
list.add("社会");
list.add("国内");
list.add("国际");
list.add("娱乐");
list.add("体育");
list.add("军事");
list.add("科技");
list.add("财经");
list.add("时尚");
pager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public CharSequence getPageTitle(int position) {
return list.get(position);
}
@Override
public Fragment getItem(int position) {
NewsFragment newsFragment = new NewsFragment();
Bundle bundle = new Bundle();
if (list.get(position).equals("头条")){
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");
}
newsFragment.setArguments(bundle);
return newsFragment;
}
@Override
public int getCount() {
return list.size();
}
});
tabLayout.setupWithViewPager(pager);
}
}
-------------------------------------------fragment页面
package fragment.bwie.com.rikao11;
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 com.google.gson.Gson;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import fragment.bwie.com.rikao11.adapter.MyAdapter;
import fragment.bwie.com.rikao11.bean.Bean;
/**
* Created by CZ on 2017/9/12.
*/
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.news_layout, container, false);
listView = (ListView) view.findViewById(R.id.list_view);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Bundle bundle = getArguments();
String string = bundle.getString("name", "top");
getDataFromNet(string);
}
private void getDataFromNet(final String string) {
AsyncTask asyncTask = new AsyncTask() {
@Override
protected String doInBackground(Void... voids) {
String path = "http://v.juhe.cn/toutiao/index?type=" + string + "&key=597b4f9dcb50e051fd725a9ec54d6653";
try {
URL url = new URL(path);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setConnectTimeout(5000);
urlConnection.setReadTimeout(5000);
int responseCode = urlConnection.getResponseCode();
if (responseCode == 200) {
InputStream inputStream = urlConnection.getInputStream();
String json = streamTostring(inputStream, "utf-8");
return json;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Gson gson = new Gson();
Bean bean = gson.fromJson(s, Bean.class);
List list = bean.getResult().getData();
MyAdapter myAdapter = new MyAdapter(list, getActivity());
listView.setAdapter(myAdapter);
}
};
asyncTask.execute();
}
private String streamTostring(InputStream inputStream, String s) {
try {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, s);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuilder builder = new StringBuilder();
String string = null;
while ((string = bufferedReader.readLine()) != null) {
builder.append(string);
}
bufferedReader.close();
return builder.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
----------------------------------------------------适配器
package fragment.bwie.com.rikao11.adapter;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.List;
import fragment.bwie.com.rikao11.bean.Bean;
/**
* Created by CZ on 2017/9/12.
*/
public class MyAdapter extends BaseAdapter {
List list;
Context context;
public MyAdapter(List list, Context context) {
this.list = list;
this.context = context;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return list.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
if (view == null){
view = View.inflate(context,android.R.layout.simple_list_item_1,null);
}
TextView textView = (TextView) view.findViewById(android.R.id.text1);
textView.setText(list.get(i).getTitle());
return view;
}
}
-----------------------------------------------------布局
xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="fragment.bwie.com.rikao11.MainActivity">
<android.support.design.widget.TabLayout
android:id="@+id/tablayout"
android:layout_width="match_parent"
android:layout_height="40dp"
app:tabGravity="center"
app:tabIndicatorColor="#D43D3D"
app:tabMode="scrollable"
app:tabSelectedTextColor="#D43D3D"
app:tabTextColor="#000000">android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/tablayout">android.support.v4.view.ViewPager>
RelativeLayout>
-----------------------------fragment布局
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">
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent">ListView>
LinearLayout>
-------------------权限
design
版本全改成25