布局:
radio_selecter:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/colorPrimary" android:state_checked="true"/>
<item android:drawable="@color/colorAccent" android:state_checked="false"/>
<item android:drawable="@color/colorAccent"/>
</selector>
frame_layout:
<?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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
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:id="@+id/viewpager_news"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
</LinearLayout>
item_layout:
<?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="horizontal"
>
<ImageView
android:id="@+id/image"
android:src="@mipmap/ic_launcher"
android:layout_width="100dp"
android:layout_height="100dp" />
<TextView
android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
newschild_layout:
<?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.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="#383838"
ptr:ptrHeaderTextColor="#FFFFFF"
/>
</LinearLayout>
activity_main:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawer_layout"
xmlns:app="http://schemas.android.com/apk/res-auto">
<RelativeLayout
android:id="@+id/relative"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/frame_layout"
android:layout_above="@+id/radio_group"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<RadioGroup
android:id="@+id/radio_group"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="40dp">
<RadioButton
android:id="@+id/radio_01"
android:button="@null"
android:gravity="center"
android:padding="5dp"
android:background="@drawable/radio_selector"
android:checked="true"
android:text="首页"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<RadioButton
android:id="@+id/radio_02"
android:button="@null"
android:gravity="center"
android:padding="5dp"
android:background="@drawable/radio_selector"
android:text="资讯"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<RadioButton
android:id="@+id/radio_03"
android:button="@null"
android:gravity="center"
android:padding="5dp"
android:background="@drawable/radio_selector"
android:text="行情"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<RadioButton
android:id="@+id/radio_04"
android:button="@null"
android:gravity="center"
android:padding="5dp"
android:background="@drawable/radio_selector"
android:text="交易"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
</RadioGroup>
</RelativeLayout>
//抽屉的布局
<LinearLayout
android:id="@+id/lin_layout"
android:background="#ffffff"
android:orientation="vertical"
android:layout_gravity="left"
android:layout_width="250dp"
android:layout_height="match_parent">
<ImageView
android:src="@mipmap/ic_launcher"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="center"
/>
<TextView
android:text="客服热线"
android:padding="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="营业部网店"
android:padding="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="系统设置"
android:padding="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="换肤"
android:padding="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
dimens:
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
</resources>
'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
compile 'com.android.support:design:25.3.1'
compile 'com.google.code.gson:gson:2.8.2'
compile project(':library')
MainActivity:
package guolei20170922.guolei20170922;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity {
private FrameLayout frameLayout;
private RelativeLayout relativeLayout;
private DrawerLayout drawerLayout;
private RadioGroup group;
private LinearLayout linearLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
relativeLayout = (RelativeLayout) findViewById(R.id.relative);
frameLayout = (FrameLayout) findViewById(R.id.frame_layout);
group = (RadioGroup) findViewById(R.id.radio_group);
//抽屉布局
linearLayout = (LinearLayout) findViewById(R.id.lin_layout);
//获取新闻控件
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout,new FragmentNews()).commit();
group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.radio_01:
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout,new FragmentNews()).commit();
break;
case R.id.radio_02:
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout,new FragmentNews()).commit();
break;
case R.id.radio_03:
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout,new FragmentNews()).commit();
break;
case R.id.radio_04:
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout,new FragmentNews()).commit();
break;
}
}
});
}
}
NewsAdapter:
package guolei20170922.guolei20170922;
import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import java.util.List;
/**
* Created by Lenovo on 2017/9/22.
*/
public class NewsAdapter extends BaseAdapter {
Context context;
List<DataDataBean.ResultsBean> list;
public NewsAdapter(Context context, List<DataDataBean.ResultsBean> list) {
this.context=context;
this.list=list;
ImageLoader.getInstance().init(ImageLoaderConfiguration.createDefault(context));
}
public NewsAdapter(FragmentActivity activity, List<DataDataBean> list) {
}
@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) {
ViewHolder holder;
if (convertView==null){
convertView=View.inflate(context,R.layout.item_layout,null);
holder=new ViewHolder();
holder.imageView= (ImageView) convertView.findViewById(R.id.image);
holder.textView= (TextView) convertView.findViewById(R.id.text_view);
convertView.setTag(holder);
}else{
holder= (ViewHolder) convertView.getTag();
}
holder.textView.setText(list.get(position).getDesc());
ImageLoader.getInstance().displayImage(list.get(position).getUrl(),holder.imageView);
return convertView;
}
class ViewHolder{
ImageView imageView;
TextView textView;
}
}
DataDataBean:
package guolei20170922.guolei20170922;
import java.util.List;
/**
* Created by Lenovo on 2017/9/22.
*/
public class DataDataBean {
/**
* error : false
* results : [{"_id":"597e016c421aa90ca209c523","createdAt":"2017-07-30T23:55:24.154Z","desc":"Android终端","publishedAt":"2017-09-21T13:27:15.675Z","source":"chrome","type":"Android","url":"https://github.com/termux/termux-app","used":true,"who":"Jason"},{"_id":"597f2861421aa90ca209c527","createdAt":"2017-07-31T20:53:53.217Z","desc":"Google从 API 21 新增了接口 android.app.usage , 通过这个api我们可以统计到每个app的使用情况,启动次数,启动时间等,也可以判断是否前后台,比较方便。","images":["http://img.gank.io/c778f7da-b580-490b-961d-34706a57d326"],"publishedAt":"2017-09-21T13:27:15.675Z","source":"web","type":"Android","url":"http://www.jianshu.com/p/bdf47afe110d","used":true,"who":"Tamic (码小白)"},{"_id":"5980492b421aa90ca209c533","createdAt":"2017-08-01T17:26:03.968Z","desc":"内存泄漏简要分析","publishedAt":"2017-09-21T13:27:15.675Z","source":"web","type":"Android","url":"https://zhuanlan.zhihu.com/p/28169456?group_id=874607398092935168","used":true,"who":"Li Wenjing"},{"_id":"59c3201e421aa9727ddd19b6","createdAt":"2017-09-21T10:12:46.866Z","desc":"安卓版本的VegaScroll","images":["http://img.gank.io/22a7d123-7776-4954-8f73-176ad9e18ddf"],"publishedAt":"2017-09-21T13:27:15.675Z","source":"web","type":"Android","url":"https://github.com/xmuSistone/VegaLayoutManager","used":true,"who":"stone"},{"_id":"59bbe478421aa9118c8262ca","createdAt":"2017-09-15T22:32:24.21Z","desc":"Android上取代HashMap的ArrayMap","images":["http://img.gank.io/a87f186b-47cc-44a4-98c7-a2dd05576fe7"],"publishedAt":"2017-09-20T13:17:38.709Z","source":"web","type":"Android","url":"http://www.jianshu.com/p/088b9383f974","used":true,"who":"Niekon"},{"_id":"59bf3bb0421aa9118887ac35","createdAt":"2017-09-18T11:21:20.893Z","desc":"Glide v4最全中文文档来了!","publishedAt":"2017-09-20T13:17:38.709Z","source":"web","type":"Android","url":"https://github.com/Muyangmin/glide-docs-cn","used":true,"who":"Muyangmin"},{"_id":"59c1c557421aa9727fdb25c3","createdAt":"2017-09-20T09:33:11.73Z","desc":"你知道APP从启动到主页面显示经历了哪些过程吗?","publishedAt":"2017-09-20T13:17:38.709Z","source":"web","type":"Android","url":"https://mp.weixin.qq.com/s?__biz=MzIwMzYwMTk1NA==&mid=2247487014&idx=1&sn=ce2e5378d0dd8a8bc6b85358a8aaa47c","used":true,"who":"陈宇明"},{"_id":"59c1d24d421aa972845f2077","createdAt":"2017-09-20T10:28:29.264Z","desc":"全民 Google ARCore,解决官方不支持设备问题","publishedAt":"2017-09-20T13:17:38.709Z","source":"web","type":"Android","url":"https://github.com/tomthecarrot/arcore-for-all","used":true,"who":null},{"_id":"59b7909f421aa911847a0391","createdAt":"2017-09-12T15:45:35.132Z","desc":"View的事件分发机制\u2014\u2014由浅入深解析","publishedAt":"2017-09-19T12:07:31.405Z","source":"web","type":"Android","url":"http://www.jianshu.com/p/e6ceb7f767d8","used":true,"who":"匡罡"},{"_id":"59bfe526421aa911847a03b3","createdAt":"2017-09-18T23:24:22.545Z","desc":"一个简约番茄时钟的实现思路","images":["http://img.gank.io/cc9e461f-3e8d-456f-bba5-e15b80509dfb"],"publishedAt":"2017-09-19T12:07:31.405Z","source":"web","type":"Android","url":"http://rkhcy.github.io/2017/09/18/TomatoView%E5%AE%9E%E7%8E%B0/","used":true,"who":"HuYounger"}]
*/
private boolean error;
private List<ResultsBean> results;
public boolean isError() {
return error;
}
public void setError(boolean error) {
this.error = error;
}
public List<ResultsBean> getResults() {
return results;
}
public void setResults(List<ResultsBean> results) {
this.results = results;
}
public static class ResultsBean {
/**
* _id : 597e016c421aa90ca209c523
* createdAt : 2017-07-30T23:55:24.154Z
* desc : Android终端
* publishedAt : 2017-09-21T13:27:15.675Z
* source : chrome
* type : Android
* url : https://github.com/termux/termux-app
* used : true
* who : Jason
* images : ["http://img.gank.io/c778f7da-b580-490b-961d-34706a57d326"]
*/
private String _id;
private String createdAt;
private String desc;
private String publishedAt;
private String source;
private String type;
private String url;
private boolean used;
private String who;
private List<String> images;
public String get_id() {
return _id;
}
public void set_id(String _id) {
this._id = _id;
}
public String getCreatedAt() {
return createdAt;
}
public void setCreatedAt(String createdAt) {
this.createdAt = createdAt;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getPublishedAt() {
return publishedAt;
}
public void setPublishedAt(String publishedAt) {
this.publishedAt = publishedAt;
}
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public boolean isUsed() {
return used;
}
public void setUsed(boolean used) {
this.used = used;
}
public String getWho() {
return who;
}
public void setWho(String who) {
this.who = who;
}
public List<String> getImages() {
return images;
}
public void setImages(List<String> images) {
this.images = images;
}
}
}
FragmentNews:
package guolei20170922.guolei20170922;
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 Lenovo on 2017/9/22.
*/
public class FragmentNews extends Fragment {
private TabLayout tabLayout;
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.frame_layout,container,false);
tabLayout = (TabLayout) view.findViewById(R.id.tab_layout);
viewPager = (ViewPager) view.findViewById(R.id.viewpager_news);
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("理财");
viewPager.setOffscreenPageLimit(list.size());
viewPager.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
@Override
public CharSequence getPageTitle(int position) {
return list.get(position);
}
@Override
public Fragment getItem(int position) {
//根据切换的标题位置,,,显示不同的fragment
FragmentNewsChild fragmentNewsChild = new FragmentNewsChild();
return fragmentNewsChild;
}
@Override
public int getCount() {
return list.size();
}
});
tabLayout.setupWithViewPager(viewPager);
}
}
FragmentNewsChild:
package guolei20170922.guolei20170922;
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 com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Lenovo on 2017/9/22.
*/
public class FragmentNewsChild extends Fragment {
private PullToRefreshListView refreshListView;
private int page_num = 1;
private List<DataDataBean.ResultsBean> list=new ArrayList<>();
private NewsAdapter newadapter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.newschild_layout, container, false);
refreshListView = (PullToRefreshListView) view.findViewById(R.id.refresh_list_view);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getDataNet();
//设置刷新模式
refreshListView.setMode(PullToRefreshBase.Mode.BOTH);
//刷新的时候显示的文字状态
ILoadingLayout startLabels = refreshListView.getLoadingLayoutProxy(true, false);
startLabels.setPullLabel("下拉刷新");
startLabels.setRefreshingLabel("正在拉");
startLabels.setReleaseLabel("放开刷新");
ILoadingLayout endLabels = refreshListView.getLoadingLayoutProxy(false, true);
endLabels.setPullLabel("上拉刷新");
endLabels.setRefreshingLabel("正在载入...");
endLabels.setReleaseLabel("放开刷新...");
//设置监听
refreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
//下拉
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
getRefreash();
}
//上拉
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
page_num++;
getDataNet();
}
});
}
private void getDataNet() {
AsyncTask<Void, Void, String> async = new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... voids) {
String path = "http://gank.io/api/data/Android/10/1" + page_num;
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) {
//非空判断
if (json != null && !json.isEmpty()) {
Gson gson = new Gson();
DataDataBean dataDataBean = gson.fromJson(json, DataDataBean.class);
//获取集合...设置适配器
if (dataDataBean != null && dataDataBean.getResults()!= null) {
//添加到集合的前边
list.addAll(0, dataDataBean.getResults());
//设置适配器
setAdapter();
}
//刷新 停止
refreshListView.onRefreshComplete();
}
}
};
async.execute();
}
private void getRefreash() {
AsyncTask<Void, Void, String> async = new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... voids) {
String path = "http://gank.io/api/data/Android/10/1" + page_num;
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) {
//非空判断
if (json != null && !json.isEmpty()) {
Gson gson = new Gson();
DataDataBean dataDataBean = gson.fromJson(json, DataDataBean.class);
//获取集合...设置适配器
if (dataDataBean != null && dataDataBean.getResults()!= null) {
//添加到集合的前边
list.addAll(0, dataDataBean.getResults());
//设置适配器
setAdapter();
}
//刷新 停止
refreshListView.onRefreshComplete();
}
}
};
async.execute();
}
private void setAdapter() {
if (newadapter==null){
newadapter = new NewsAdapter(getActivity(),list);
refreshListView.setAdapter(newadapter);
}else{
newadapter.notifyDataSetChanged();
}
}
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;
}
}