package com.bwei.gepeng3;
import android.graphics.Color;
import android.os.Bundle;
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.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements TextView.OnClickListener{
private HorizontalScrollView hzs;
private LinearLayout ll;
private ViewPager vp;
String[] title=new String[]{"福利","安卓","苹果","视频","拓展","前端","全部"};
List listtv=new ArrayList<>();
private FragmentAdapte adapte;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化控件
initView();
//添加textView
for (int i = 0; i
package com.bwei.gepeng3;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
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.ImageView;
import android.widget.TextView;
import com.example.xx.xlist_library.view.XListView;
import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
public class Frag extends Fragment implements XListView.IXListViewListener{
private View view;
List results=new ArrayList<>();
int page=1;
String[] title=new String[]{"福利","Android","iOS","休息视频","拓展资源","前端","all"};
//String url="http://gank.io/api/data/数据类型/10/"+page;
private XListView xlv;
private Base base;
private int position;
public static Fragment getFrag(int position){
Frag frag = new Frag();
Bundle bundle = new Bundle();
bundle.putInt("position",position);
frag.setArguments(bundle);
return frag;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//加载布局
view = inflater.inflate(R.layout.frag, container, false);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
xlv = (XListView)view.findViewById(R.id.xlv);
xlv.setXListViewListener(this);
xlv.setPullLoadEnable(true);
xlv.setPullLoadEnable(true);
position = (int) getArguments().get("position");
base = new Base();
new StringAsyncTask().execute("http://gank.io/api/data/"+ URLEncoder.encode(title[position])+"/10/"+page);
//new StringAsyncTask().execute("http://gank.io/api/data/"+title[position]+"/10/"+page);
}
//String
class StringAsyncTask extends AsyncTask {
@Override
protected String doInBackground(String... strings) {
return new Utils().getJson(strings[0]);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Gson gson = new Gson();
JsonBean jsonBean = gson.fromJson(s, JsonBean.class);
results = jsonBean.getResults();
xlv.setAdapter(base);
/*for (int i = 0; i {
@Override
protected String doInBackground(String... strings) {
return new Utils().getJson(strings[0]);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Gson gson = new Gson();
JsonBean jsonBean = gson.fromJson(s, JsonBean.class);
List r = jsonBean.getResults();
for (JsonBean.ResultsBean w: r) {
results.add(w);
}
base.notifyDataSetChanged();
}
}
//适配器
class Base extends BaseAdapter {
@Override
public int getCount() {
return results.size();
}
@Override
public Object getItem(int i) {
return results.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder vh;
if(view==null){
vh = new ViewHolder();
view= View.inflate(getActivity(), R.layout.list, null);
vh.img=(ImageView)view.findViewById(R.id.img);
vh.tv=(TextView)view.findViewById(R.id.tv);
view.setTag(vh);
}else{
vh = (ViewHolder) view.getTag();
}
JsonBean.ResultsBean resultsBean = results.get(i);
vh.tv.setText(resultsBean.getDesc());
//显示图片的配置
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.mipmap.ic_launcher)
.showImageOnFail(R.mipmap.ic_launcher)
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
ImageLoader.getInstance().displayImage(resultsBean.getUrl(), vh.img, options);
return view;
}
class ViewHolder{
ImageView img;
TextView tv;
}
}
Handler handler=new Handler();
/**
* 下拉刷新,
*/
@Override
public void onRefresh() {
handler.postDelayed(new Runnable() {
@Override
public void run() {
results.clear();
page=1;
new StringAsyncTask1().execute("http://gank.io/api/data/"+ URLEncoder.encode(title[position])+"/10/"+page);
onLoad();
}
},1000);
}
/**
* 上拉加载,
*/
@Override
public void onLoadMore() {
handler.postDelayed(new Runnable() {
@Override
public void run() {
//results.clear();
page++;
new StringAsyncTask1().execute("http://gank.io/api/data/"+ URLEncoder.encode(title[position])+"/10/"+page);
onLoad();
}
},1000);
}
/**
* 停止刷新,
*/
private void onLoad() {
xlv.stopRefresh();
xlv.stopLoadMore();
xlv.setRefreshTime("刚刚");
}
}
package com.bwei.gepeng3;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class Utils {
public String getJson(String u){
try {
//定义一个url对象
URL url = new URL(u);
//通过url打开连接;
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
//获取响应码
int responseCode = urlConnection.getResponseCode();
//判断是否成功进入链接 即 响应码为 200 成功加载
if(responseCode==200){
//获取服务器返回的数据 都是以流的形式;
InputStream inputStream = urlConnection.getInputStream();
//包装为BufferedReader
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder sb = new StringBuilder();
String str = "";
while((str=br.readLine())!=null){
sb.append(str);
}
return sb.toString();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
package com.bwei.gepeng3;
import java.util.List;
public class JsonBean {
/**
* error : false
* results : [{"_id":"59b7a98e421aa911847a0392","createdAt":"2017-09-12T17:31:58.794Z","desc":"ViewStub学习","images":["http://img.gank.io/9f51b7bc-73d3-4ce4-bd59-c61cb0c64325"],"publishedAt":"2017-09-14T16:36:51.63Z","source":"web","type":"Android","url":"http://rkhcy.github.io/2017/09/12/ViewStub%E5%AD%A6%E4%B9%A0/","used":true,"who":"HuYounger"},{"_id":"59b7d963421aa911847a0394","createdAt":"2017-09-12T20:56:03.184Z","desc":"基于Snowflake算法而写的一种unique id生成器","publishedAt":"2017-09-14T16:36:51.63Z","source":"web","type":"Android","url":"https://github.com/zjutkz/ASnowflake","used":true},{"_id":"59b8ad56421aa911878707ed","createdAt":"2017-09-13T12:00:22.260Z","desc":"看我逆向小米rom层应用做碎片化适配","publishedAt":"2017-09-14T16:36:51.63Z","source":"web","type":"Android","url":"https://mp.weixin.qq.com/s?__biz=MzIwMzYwMTk1NA==&mid=2247486906&idx=1&sn=d6f473abffdea7b2ece6f6bb1f72e084","used":true,"who":"陈宇明"},{"_id":"59b9dfc1421aa9118c8262c1","createdAt":"2017-09-14T09:47:45.123Z","desc":"一个用 Kotlin 写的查单词 idea 插件","images":["http://img.gank.io/4939784c-eea2-4b60-9c7a-d1b4d9f9c8b5"],"publishedAt":"2017-09-14T16:36:51.63Z","source":"web","type":"Android","url":"https://github.com/jungletian/BreakWords","used":true,"who":"ZhangTitanjum"},{"_id":"59ba01b6421aa911878707f5","createdAt":"2017-09-14T12:12:38.108Z","desc":"Floo 开源了,一个支持 AOP、栈控制、跨页面带信、和动态变更映射的 URL 路由","publishedAt":"2017-09-14T16:36:51.63Z","source":"web","type":"Android","url":"https://github.com/drakeet/Floo","used":true,"who":"drakeet"},{"_id":"59b667cf421aa9118887ac12","createdAt":"2017-09-11T18:39:11.631Z","desc":"用少量Rxjava代码,为retrofit添加退避重试功能","publishedAt":"2017-09-12T08:15:08.26Z","source":"web","type":"Android","url":"http://www.jianshu.com/p/fca90d0da2b5","used":true,"who":"小鄧子"},{"_id":"59b673ef421aa9118887ac13","createdAt":"2017-09-11T19:30:55.50Z","desc":"Android 端基于 OpenCV 的边框识别功能","publishedAt":"2017-09-12T08:15:08.26Z","source":"web","type":"Android","url":"https://pqpo.me/2017/09/11/opencv-border-recognition/","used":true,"who":"Linmin Qiu"},{"_id":"59b69738421aa9118c8262ad","createdAt":"2017-09-11T22:01:28.352Z","desc":"用ContentProvider初始化你的库","images":["http://img.gank.io/3b0b193d-6abf-4714-9d5a-5508404666f4"],"publishedAt":"2017-09-12T08:15:08.26Z","source":"web","type":"Android","url":"http://zjutkz.net/2017/09/11/%E4%B8%80%E4%B8%AA%E5%B0%8F%E6%8A%80%E5%B7%A7%E2%80%94%E2%80%94%E4%BD%BF%E7%94%A8ContentProvider%E5%88%9D%E5%A7%8B%E5%8C%96%E4%BD%A0%E7%9A%84Library/","used":true},{"_id":"5996c673421aa96729c57262","createdAt":"2017-08-18T18:50:27.254Z","desc":"UiAutomator2.0升级填坑记","publishedAt":"2017-09-07T13:25:26.977Z","source":"web","type":"Android","url":"http://skyseraph.com/2017/06/04/Android/UiAutomator2.0%E5%8D%87%E7%BA%A7%E5%A1%AB%E5%9D%91%E8%AE%B0/","used":true,"who":"SkySeraph"},{"_id":"59a37311421aa901bcb7dbb2","createdAt":"2017-08-28T09:34:09.768Z","desc":"Scrum:官僚者们的游戏","publishedAt":"2017-09-07T13:25:26.977Z","source":"web","type":"Android","url":"https://mp.weixin.qq.com/s?__biz=MzIwMzYwMTk1NA==&mid=2247486650&idx=1&sn=9c6e2e0fd5780aab2bcb02b823e7316e","used":true,"who":"陈宇明"}]
*/
private boolean error;
private List results;
public boolean isError() {
return error;
}
public void setError(boolean error) {
this.error = error;
}
public List getResults() {
return results;
}
public void setResults(List results) {
this.results = results;
}
public static class ResultsBean {
/**
* _id : 59b7a98e421aa911847a0392
* createdAt : 2017-09-12T17:31:58.794Z
* desc : ViewStub学习
* images : ["http://img.gank.io/9f51b7bc-73d3-4ce4-bd59-c61cb0c64325"]
* publishedAt : 2017-09-14T16:36:51.63Z
* source : web
* type : Android
* url : http://rkhcy.github.io/2017/09/12/ViewStub%E5%AD%A6%E4%B9%A0/
* used : true
* who : HuYounger
*/
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 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 getImages() {
return images;
}
public void setImages(List images) {
this.images = images;
}
}
}
package com.bwei.gepeng3;
import android.app.Application;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
public class App extends Application{
@Override
public void onCreate() {
super.onCreate();
//配置
ImageLoaderConfiguration aDefault = ImageLoaderConfiguration.createDefault(this);
//初始化
ImageLoader.getInstance().init(aDefault);
}
}
package com.bwei.gepeng3;
import java.util.List;
public class JsonBean {
/**
* error : false
* results : [{"_id":"59b7a98e421aa911847a0392","createdAt":"2017-09-12T17:31:58.794Z","desc":"ViewStub学习","images":["http://img.gank.io/9f51b7bc-73d3-4ce4-bd59-c61cb0c64325"],"publishedAt":"2017-09-14T16:36:51.63Z","source":"web","type":"Android","url":"http://rkhcy.github.io/2017/09/12/ViewStub%E5%AD%A6%E4%B9%A0/","used":true,"who":"HuYounger"},{"_id":"59b7d963421aa911847a0394","createdAt":"2017-09-12T20:56:03.184Z","desc":"基于Snowflake算法而写的一种unique id生成器","publishedAt":"2017-09-14T16:36:51.63Z","source":"web","type":"Android","url":"https://github.com/zjutkz/ASnowflake","used":true},{"_id":"59b8ad56421aa911878707ed","createdAt":"2017-09-13T12:00:22.260Z","desc":"看我逆向小米rom层应用做碎片化适配","publishedAt":"2017-09-14T16:36:51.63Z","source":"web","type":"Android","url":"https://mp.weixin.qq.com/s?__biz=MzIwMzYwMTk1NA==&mid=2247486906&idx=1&sn=d6f473abffdea7b2ece6f6bb1f72e084","used":true,"who":"陈宇明"},{"_id":"59b9dfc1421aa9118c8262c1","createdAt":"2017-09-14T09:47:45.123Z","desc":"一个用 Kotlin 写的查单词 idea 插件","images":["http://img.gank.io/4939784c-eea2-4b60-9c7a-d1b4d9f9c8b5"],"publishedAt":"2017-09-14T16:36:51.63Z","source":"web","type":"Android","url":"https://github.com/jungletian/BreakWords","used":true,"who":"ZhangTitanjum"},{"_id":"59ba01b6421aa911878707f5","createdAt":"2017-09-14T12:12:38.108Z","desc":"Floo 开源了,一个支持 AOP、栈控制、跨页面带信、和动态变更映射的 URL 路由","publishedAt":"2017-09-14T16:36:51.63Z","source":"web","type":"Android","url":"https://github.com/drakeet/Floo","used":true,"who":"drakeet"},{"_id":"59b667cf421aa9118887ac12","createdAt":"2017-09-11T18:39:11.631Z","desc":"用少量Rxjava代码,为retrofit添加退避重试功能","publishedAt":"2017-09-12T08:15:08.26Z","source":"web","type":"Android","url":"http://www.jianshu.com/p/fca90d0da2b5","used":true,"who":"小鄧子"},{"_id":"59b673ef421aa9118887ac13","createdAt":"2017-09-11T19:30:55.50Z","desc":"Android 端基于 OpenCV 的边框识别功能","publishedAt":"2017-09-12T08:15:08.26Z","source":"web","type":"Android","url":"https://pqpo.me/2017/09/11/opencv-border-recognition/","used":true,"who":"Linmin Qiu"},{"_id":"59b69738421aa9118c8262ad","createdAt":"2017-09-11T22:01:28.352Z","desc":"用ContentProvider初始化你的库","images":["http://img.gank.io/3b0b193d-6abf-4714-9d5a-5508404666f4"],"publishedAt":"2017-09-12T08:15:08.26Z","source":"web","type":"Android","url":"http://zjutkz.net/2017/09/11/%E4%B8%80%E4%B8%AA%E5%B0%8F%E6%8A%80%E5%B7%A7%E2%80%94%E2%80%94%E4%BD%BF%E7%94%A8ContentProvider%E5%88%9D%E5%A7%8B%E5%8C%96%E4%BD%A0%E7%9A%84Library/","used":true},{"_id":"5996c673421aa96729c57262","createdAt":"2017-08-18T18:50:27.254Z","desc":"UiAutomator2.0升级填坑记","publishedAt":"2017-09-07T13:25:26.977Z","source":"web","type":"Android","url":"http://skyseraph.com/2017/06/04/Android/UiAutomator2.0%E5%8D%87%E7%BA%A7%E5%A1%AB%E5%9D%91%E8%AE%B0/","used":true,"who":"SkySeraph"},{"_id":"59a37311421aa901bcb7dbb2","createdAt":"2017-08-28T09:34:09.768Z","desc":"Scrum:官僚者们的游戏","publishedAt":"2017-09-07T13:25:26.977Z","source":"web","type":"Android","url":"https://mp.weixin.qq.com/s?__biz=MzIwMzYwMTk1NA==&mid=2247486650&idx=1&sn=9c6e2e0fd5780aab2bcb02b823e7316e","used":true,"who":"陈宇明"}]
*/
private boolean error;
private List results;
public boolean isError() {
return error;
}
public void setError(boolean error) {
this.error = error;
}
public List getResults() {
return results;
}
public void setResults(List results) {
this.results = results;
}
public static class ResultsBean {
/**
* _id : 59b7a98e421aa911847a0392
* createdAt : 2017-09-12T17:31:58.794Z
* desc : ViewStub学习
* images : ["http://img.gank.io/9f51b7bc-73d3-4ce4-bd59-c61cb0c64325"]
* publishedAt : 2017-09-14T16:36:51.63Z
* source : web
* type : Android
* url : http://rkhcy.github.io/2017/09/12/ViewStub%E5%AD%A6%E4%B9%A0/
* used : true
* who : HuYounger
*/
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 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 getImages() {
return images;
}
public void setImages(List images) {
this.images = images;
}
}
}