主的Activity
public class MainActivity extends AppCompatActivity {
private DrawerLayout drawerlayout;
private RadioGroup radiogroup;
private LinearLayout linearLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawerlayout = (DrawerLayout) findViewById(R.id.drawer_layout);
radiogroup = (RadioGroup) findViewById(R.id.radio_group);
linearLayout = (LinearLayout) findViewById(R.id.liner_drawer);
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_layout,new FragmentNews()).commit();
radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.bt01:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_layout,new FragmentNews()).commit();
break;
case R.id.bt02:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_layout,new FragmentNews()).commit();
break;
case R.id.bt03:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_layout,new FragmentNews()).commit();
break;
case R.id.bt04:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_layout,new FragmentNews()).commit();
break;
}
}
});
}
}
TabLayout
package com.example.day02_0927;
import android.os.Bundle;
import android.support.annotation.Nullable;
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.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;
/**
* Created by hasee on 2017/9/27.
*/
public class FragmentNews extends Fragment {
private TabLayout tablauout;
private ViewPager viewpager;
private List<String> list;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.news_tab, null);
tablauout = (TabLayout) view.findViewById(R.id.tab_layout);
viewpager = (ViewPager) view.findViewById(R.id.vp2);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
list = new ArrayList<>();
list.add("头条");
list.add("社会");
list.add("国内");
list.add("国际");
list.add("娱乐");
list.add("体育");
list.add("军事");
list.add("科技");
list.add("财经");
list.add("时尚");
viewpager.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
@Override
public CharSequence getPageTitle(int position) {
return list.get(position);
}
@Override
public Fragment getItem(int position) {
Fragmentpull fragmentpull = new Fragmentpull();
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");
}
fragmentpull.setArguments(bundle);
return fragmentpull;
}
@Override
public int getCount() {
return list.size();
}
});
tablauout.setupWithViewPager(viewpager);
viewpager.setOffscreenPageLimit(list.size());
}
}
PullUpToRefresh
package com.example.day02_0927;
import android.content.Intent;
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.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
/**
* Created by hasee on 2017/9/27.
*/
public class Fragmentpull extends Fragment {
private PullToRefreshListView refrechListView;
private String string;
private ILoadingLayout startLabels;
private ILoadingLayout endLabels;
private List<NewsBean.ResultBean.DataBean> list;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.new_pull, null);
refrechListView = (PullToRefreshListView) view.findViewById(R.id.refresh_list_view);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Bundle bundle = getArguments();
string = bundle.getString("name", "top");
getData(string);
refrechListView.setMode(PullToRefreshBase.Mode.BOTH);
startLabels = refrechListView.getLoadingLayoutProxy(true, false);
startLabels.setPullLabel("下拉刷新");
startLabels.setRefreshingLabel("正在刷新...");
startLabels.setReleaseLabel("放开刷新...");
endLabels = refrechListView.getLoadingLayoutProxy(
false, true);
endLabels.setPullLabel("上拉刷新");
endLabels.setRefreshingLabel("正在载入...");
endLabels.setReleaseLabel("放开刷新...");
refrechListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
getData(string);
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
getData(string);
}
});
refrechListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(getActivity(),WebActivity.class);
intent.putExtra("url",list.get(position).getUrl());
startActivity(intent);
}
});
}
private void getData(final String string) {
new AsyncTask<String,Void,String>(){
String path = "http://v.juhe.cn/toutiao/index?type=" + string + "&key=dbf538946ceef40980879441117b4235";
@Override
protected String doInBackground(String... params) {
try {
URL url = new URL(path);
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");
return json;
}
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
@Override
protected void onPostExecute(String json) {
super.onPostExecute(json);
Gson gson = new Gson();
NewsBean newsBean = gson.fromJson(json, NewsBean.class);
list = newsBean.getResult().getData();
refrechListView.setAdapter(new MyAdapter());
refrechListView.onRefreshComplete();
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");
startLabels.setLastUpdatedLabel("上次刷新时间"+dateFormat.format(date));
}
}.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);
return builder.toString();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
class MyAdapter extends BaseAdapter{
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
convertView = View.inflate(getActivity(), R.layout.item, null);
}
ImageView iv01 = (ImageView) convertView.findViewById(R.id.iv_01);
ImageView iv02 = (ImageView) convertView.findViewById(R.id.iv_02);
ImageView iv03 = (ImageView) convertView.findViewById(R.id.iv_03);
TextView tv01 = (TextView) convertView.findViewById(R.id.tv01);
TextView tv02 = (TextView) convertView.findViewById(R.id.tv02);
tv01.setText(list.get(position).getTitle());
tv02.setText(list.get(position).getDate());
ImageLoader.getInstance().displayImage(list.get(position).getThumbnail_pic_s(),iv01);
ImageLoader.getInstance().displayImage(list.get(position).getThumbnail_pic_s02(),iv02);
ImageLoader.getInstance().displayImage(list.get(position).getThumbnail_pic_s03(),iv03);
return convertView;
}
}
}
布局 主
<RelativeLayout
android:id="@+id/relative"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/fragment_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"></FrameLayout>
<RadioGroup
android:layout_alignParentBottom="true"
android:id="@+id/radio_group"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="50dp">
<RadioButton
android:id="@+id/bt01"
android:button="@null"
android:gravity="center"
android:padding="5dp"
android:background="@drawable/radio_selector"
android:checked="true"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="首页"
android:layout_weight="1"/>
<RadioButton
android:id="@+id/bt02"
android:button="@null"
android:gravity="center"
android:padding="5dp"
android:background="@drawable/radio_selector"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="联系人"
android:layout_weight="1"/>
<RadioButton
android:id="@+id/bt03"
android:button="@null"
android:gravity="center"
android:padding="5dp"
android:background="@drawable/radio_selector"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="视频"
android:layout_weight="1"/>
<RadioButton
android:id="@+id/bt04"
android:button="@null"
android:gravity="center"
android:padding="5dp"
android:background="@drawable/radio_selector"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="我的"
android:layout_weight="1"/>
</RadioGroup>
</RelativeLayout>
<!-- 抽屉布局-->
<LinearLayout
android:background="#ffffff"
android:id="@+id/liner_drawer"
android:layout_gravity="left"
android:layout_width="300dp"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:src="@mipmap/ic_launcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="你好"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="未登录"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
tabLayout
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
app:tabSelectedTextColor="#f00"
app:tabTextColor="#0f0"
app:tabIndicatorColor="#f00"
app:tabGravity="center"
app:tabMode="scrollable"
android:layout_height="40dp"></android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="@+id/vp2"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v4.view.ViewPager>
PullUpToRefresh
<com.handmark.pulltorefresh.library.PullToRefreshListView
xmlns:ptr="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@+id/refresh_list_view"
ptr:ptrDrawable="@drawable/default_ptr_flip"
ptr:ptrAnimationStyle="flip"
ptr:ptrHeaderBackground="#00ff00"
ptr:ptrHeaderTextColor="#FFFFFF" >
</com.handmark.pulltorefresh.library.PullToRefreshListView>