public class MainActivity extends AppCompatActivity { private String[] titles; private ArrayList<TextView> textViewList; private HorizontalScrollView hsv; private ViewPager vp; private LinearLayout ll; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); hsv = (HorizontalScrollView) findViewById(R.id.hsv); vp = (ViewPager) findViewById(R.id.vp); ll = (LinearLayout) findViewById(R.id.ll); //横向滑动的内容 titles = new String[] { "福利", "Android", "iOS", "休息视频", "拓展资源", "前端", "all"}; textViewList = new ArrayList<TextView>(); //遍历添加到集合 for (int i = 0; i < titles.length; i++) { TextView textView = new TextView(this); textView.setText(titles[i]); // textView.setWidth(100); //设置字体大小和id textView.setTextSize(20); textView.setId(i+1000); //点击上方文字让frament跳转事件 textView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int id = view.getId(); vp.setCurrentItem(id-1000); } }); //设置第一个默认的颜色 if (i == 0) { textView.setTextColor(Color.RED); } else { textView.setTextColor(Color.BLACK); } //设置横向的边距 LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); layoutParams.setMargins(20,10,20,10); ll.addView(textView,layoutParams); textViewList.add(textView); } //连接iewpager的适配器 vp.setAdapter(new vpsp(getSupportFragmentManager())); //viewpager的监听滑动变色 vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { for (int i = 0; i < textViewList.size(); i++) { if(i==position){ textViewList.get(i).setTextColor(Color.RED); }else{ textViewList.get(i).setTextColor(Color.BLACK); } } //超出长度横向内容自动滚出 int width = textViewList.get(position).getWidth(); hsv.scrollTo((width+20)*position,0); } @Override public void onPageSelected(int position) { } @Override public void onPageScrollStateChanged(int state) { } }); } //viewpager的适配 class vpsp extends FragmentPagerAdapter { public vpsp(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { //使用新建的方法传横向滑动的值,目的新建frament return frament1.dz(textViewList.get(position).getText().toString()); } @Override public int getCount() { return textViewList.size(); } }}
//frament1
public class frament1 extends Fragment { ArrayList<ex> arr = new ArrayList<>(); private View view; private PullToRefreshListView lv; private int i=1; private Myadapter myadapter; private String title; //绘制试图 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { view = View.inflate(getActivity(), R.layout.onr, null); return view; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); Bundle arguments = getArguments(); // Log.e("chen", "onCreateView: 我是关注"+arguments.toString() ); //获取值 title = arguments.getString("title"); lv = view.findViewById(R.id.lv); //设置可以下拉 lv.setMode(PullToRefreshBase.Mode.BOTH); myadapter = new Myadapter(); lv.setAdapter(myadapter);http: //URLEncoder.encode(title)将中文得url转换格式才能解析 new MAsyncTask().execute("http://gank.io/api/data/"+ URLEncoder.encode(title)+"/10/"+i); lv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() { @Override //向下 public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) { arr.clear(); i++; new MAsyncTask().execute("http://gank.io/api/data/"+ URLEncoder.encode(title)+"/10/"+i); } //向上 @Override public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) { i++; new MAsyncTask().execute("http://gank.io/api/data/"+ URLEncoder.encode(title)+"/10/"+i); } }); } public static frament1 dz(String name){ frament1 fragment = new frament1(); Bundle bundle = new Bundle(); bundle.putString("title", name); fragment.setArguments(bundle); return fragment; } class Myadapter extends BaseAdapter{ @Override public int getCount() { return arr.size(); } @Override public Object getItem(int i) { return arr.get(i); } @Override public long getItemId(int i) { return i; } class chen{ ImageView tu; TextView yi; TextView er; } @Override public View getView(int i, View view, ViewGroup viewGroup) { ex ex = arr.get(i); chen chen = new chen(); if(view==null){ view = View.inflate(getActivity(), R.layout.lvbj, null); chen.tu=view.findViewById(R.id.img); chen.yi=view.findViewById(R.id.na); chen.er=view.findViewById(R.id.ti); view.setTag(chen); }else{ chen = (chen) view.getTag(); } // chen.tu.setImageResource(); new AsyncTaskimg(chen.tu).execute(ex.getTu()); chen.yi.setText(ex.getName()); chen.er.setText(ex.getTime()); return view; } } class AsyncTaskimg extends AsyncTask<String,Void,Bitmap> { private ImageView tt; public AsyncTaskimg(ImageView tt) { this.tt = tt; } private Bitmap bitmap; @Override protected Bitmap doInBackground(String... strings) { try { URL url = new URL(strings[0]); HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection(); //设置链接超时时间; urlConnection.setConnectTimeout(6000); //设置读取返回数据的超时时间 urlConnection.setReadTimeout(5000); int responseCode = urlConnection.getResponseCode(); //响应码200代表链接和返回数据成功 if(responseCode ==200){ InputStream inputStream = urlConnection.getInputStream(); bitmap = BitmapFactory.decodeStream(inputStream); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return bitmap; } @Override protected void onPostExecute(Bitmap bitmap) { super.onPostExecute(bitmap); tt.setImageBitmap(bitmap); } } class MAsyncTask extends AsyncTask<String,Void,String> { @Override protected String doInBackground(String... strings) { StringBuilder stringBuilder = new StringBuilder(); String s=""; try { URL url = new URL(strings[0]); URLConnection urlConnection = url.openConnection(); InputStream inputStream = urlConnection.getInputStream(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); while((s=bufferedReader.readLine())!=null){ stringBuilder.append(s); } String pjsj = stringBuilder.toString(); Log.e("chen", "doInBackground: 拼接的内容"+pjsj ); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return stringBuilder.toString(); } @Override protected void onPostExecute(String s) { //连接适配器 super.onPostExecute(s); Gson gson = new Gson(); bean bean = gson.fromJson(s, bean.class); List<com.example.chenxu1509b20170918.bean.ResultsBean> results = bean.getResults(); for (int i = 0; i < results.size(); i++) { if(results.get(i).getImages()!=null) { List<String> images = results.get(i).getImages(); String s1 = images.get(0); String desc = results.get(i).getDesc(); String publishedAt = results.get(i).getPublishedAt(); arr.add(new ex(s1,desc,publishedAt)); }else { String desc = results.get(i).getDesc(); String publishedAt = results.get(i).getPublishedAt(); arr.add(new ex( desc, publishedAt)); } } myadapter.notifyDataSetChanged(); lv.onRefreshComplete(); } } }//解析接口的bean类public class bean { /** * 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<ResultsBean> results; @Override public String toString() { return "bean{" + "error=" + error + ", results=" + results + '}'; } public bean(boolean error, List<ResultsBean> results) { this.error = error; this.results = 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 : 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<String> images; @Override public String toString() { return "ResultsBean{" + "_id='" + _id + '\'' + ", createdAt='" + createdAt + '\'' + ", desc='" + desc + '\'' + ", publishedAt='" + publishedAt + '\'' + ", source='" + source + '\'' + ", type='" + type + '\'' + ", url='" + url + '\'' + ", used=" + used + ", who='" + who + '\'' + ", images=" + images + '}'; } public ResultsBean(String _id, String createdAt, String desc, String publishedAt, String source, String type, String url, boolean used, String who, List<String> images) { this._id = _id; this.createdAt = createdAt; this.desc = desc; this.publishedAt = publishedAt; this.source = source; this.type = type; this.url = url; this.used = used; this.who = who; this.images = 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; } } }//集合李添加内容的泛型public class ex { private String tu; private String name; private String time; @Override public String toString() { return "ex{" + "tu='" + tu + '\'' + ", name='" + name + '\'' + ", time='" + time + '\'' + '}'; } public String getTu() { return tu; } public void setTu(String tu) { this.tu = tu; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getTime() { return time; } public void setTime(String time) { this.time = time; } public ex(String tu, String name, String time) { this.tu = tu; this.name = name; this.time = time; } public ex( String name, String time) { this.name = name; this.time = time; } }//Main的XML<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:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.chenxu1509b20170918.MainActivity"> <HorizontalScrollView android:id="@+id/hsv" android:background="@color/colorAccent" android:layout_width="match_parent" android:layout_height="35dp"> <LinearLayout android:id="@+id/ll" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> </LinearLayout> </HorizontalScrollView> <android.support.v4.view.ViewPager android:id="@+id/vp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>//frament1里面绘制的试图XML<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <com.handmark.pulltorefresh.library.PullToRefreshListView android:layout_width="match_parent" android:id="@+id/lv" android:layout_height="match_parent" /> </LinearLayout>//lvbj 的xml布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:orientation="horizontal" android:layout_height="match_parent"> <ImageView android:id="@+id/img" android:src="@mipmap/ic_launcher_round" android:layout_width="150dp" android:layout_height="150dp" /> <LinearLayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/na" android:text="dfcxc" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/ti" android:text="sdf" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout>