1.
<?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" tools:context="com.example.lx7.MainActivity"> <HorizontalScrollView android:id="@+id/hsv" android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:id="@+id/lt" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"></LinearLayout> </HorizontalScrollView> <android.support.v4.view.ViewPager android:id="@+id/vp" android:layout_width="match_parent" android:layout_height="match_parent"></android.support.v4.view.ViewPager> </RelativeLayout>
2
<?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"> <com.handmark.pulltorefresh.library.PullToRefreshListView android:id="@+id/pulltorefreshlistview" android:layout_width="match_parent" android:layout_height="match_parent" ></com.handmark.pulltorefresh.library.PullToRefreshListView> </LinearLayout>3<?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"> <ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>4Activtitypackage com.example.lx7; 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.net.URLEncoder; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { String baseUrl = "http://gank.io/api/data/"; private ViewPager vp; private LinearLayout lt; private HorizontalScrollView hsv; private List<TextView> list; private MyVPAdapter myVPAdapter; private String[] strings; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); FragmentManager supportFragmentManager = getSupportFragmentManager(); myVPAdapter = new MyVPAdapter(supportFragmentManager); vp.setAdapter(myVPAdapter); //监听 vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { for(int i=0;i<list.size();i++){ if(position==i){ list.get(i).setTextColor(Color.RED); }else{ list.get(i).setTextColor(Color.BLACK); } //获取textview TextView textView = list.get(position); int width = textView.getWidth() + 10; hsv.scrollTo(width*position,0); } } @Override public void onPageSelected(int position) { } @Override public void onPageScrollStateChanged(int state) { } }); } private void initView() { vp = (ViewPager) findViewById(R.id.vp); hsv = (HorizontalScrollView) findViewById(R.id.hsv); lt = (LinearLayout) findViewById(R.id.lt); //创建数组 strings = new String[]{"福利","Android","iOS","休息","视频","拓展","资源前端","all"}; //创建集合存放textview list = new ArrayList<>(); //创建TextView for(int i=0;i<strings.length;i++){ //创建textview TextView textView = new TextView(MainActivity.this); textView.setText(strings[i]); textView.setTextSize(20); textView.setId(i+1000); //点击事件 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); //添加到布局中 lt.addView(textView,layoutParams); //添加到集合 list.add(textView); } } //viewpager的适配器 class MyVPAdapter extends FragmentPagerAdapter{ public MyVPAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { return Fragment1.getins(baseUrl+ URLEncoder.encode(strings[position])+"/"); } @Override public int getCount() { return list.size(); } } }5Fragmentpackage com.example.lx7; import android.os.AsyncTask; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import com.bawei.wzq.wzlibrary.utils.NetWorkUtil; import com.handmark.pulltorefresh.library.PullToRefreshBase; import com.handmark.pulltorefresh.library.PullToRefreshListView; import com.nostra13.universalimageloader.core.ImageLoader; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.util.ArrayList; /** * author:Created by WangZhiQiang on 2017/9/20. */ public class Fragment1 extends Fragment { private int i = 1; private View view; private String url; private PullToRefreshListView pullToRefreshListView; private ArrayList<Bean2> list; private MAdapter mAdapter; public static Fragment1 getins(String title){ Fragment1 fragment1 = new Fragment1(); Bundle bundle = new Bundle(); bundle.putString("title",title); Log.e("wzq", "getins: "); fragment1.setArguments(bundle); return fragment1; } @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { view = inflater.inflate(R.layout.pull_foot,container,false); return view; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); Bundle arguments = getArguments(); url = arguments.getString("title"); pullToRefreshListView = (PullToRefreshListView)view.findViewById(R.id.pulltorefreshlistview); pullToRefreshListView.setMode(PullToRefreshBase.Mode.BOTH); list = new ArrayList<>(); mAdapter = new MAdapter(); pullToRefreshListView.setAdapter(mAdapter); new MyAsyncTask().execute(url+"10/1"); pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() { @Override public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) { list.clear(); i = 1; new MyAsyncTask().execute(url+"10/1"); } @Override public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) { i++; new MyAsyncTask().execute(url+"10/"+i); } }); } class MyAsyncTask extends AsyncTask<String,Void,String>{ @Override protected String doInBackground(String... strings) { String string = new NetWorkUtil().getNetString(strings[0]); return string; } @Override protected void onPostExecute(String s) { super.onPostExecute(s); try { JSONObject jsonObject = new JSONObject(s); JSONArray results = jsonObject.optJSONArray("results"); for (int i =0;i<results.length();i++){ JSONArray images = results.optJSONObject(i).optJSONArray("images"); if (images!=null){ list.add(new Bean2(results.optJSONObject(i).optString("desc"),(String)images.get(0))); } } } catch (JSONException e) { e.printStackTrace(); } mAdapter.notifyDataSetChanged(); pullToRefreshListView.onRefreshComplete(); } } //适配器 class MAdapter extends BaseAdapter{ @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; } class ViewHowder{ TextView text; ImageView img; } @Override public View getView(int i, View view, ViewGroup viewGroup) { int type = getItemViewType(i); Log.e("SKN--------------",list.get(i).getText()+"++++"+list.get(i).getImg()); ViewHowder vh; if(type==0){ if(view==null){ view = View.inflate(getActivity(),R.layout.pull_lv,null); vh = new ViewHowder(); vh.img=(ImageView) view.findViewById(R.id.img); vh.text=(TextView)view.findViewById(R.id.text); view.setTag(vh); }else{ vh = (ViewHowder) view.getTag(); } vh.text.setText(list.get(i).getText()); ImageLoader.getInstance().displayImage(list.get(i).getImg(),vh.img); } return view; } @Override public int getViewTypeCount() { return 2; } @Override public int getItemViewType(int position) { Bean2 bean2 = list.get(position); if (bean2.getImg()!=null){ return 0; }else{ return 1; } } } }6Beanpackage com.example.lx7; import java.util.List; /** * author:Created by WangZhiQiang on 2017/9/20. */ public class Bean { /** * error : false * results : [{"_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"},{"_id":"59c073e1421aa91187870812","createdAt":"2017-09-19T09:33:21.169Z","desc":"2017 Android插件化框架总结","publishedAt":"2017-09-19T12:07:31.405Z","source":"web","type":"Android","url":"https://mp.weixin.qq.com/s?__biz=MzIwMzYwMTk1NA==&mid=2247486927&idx=1&sn=29e7439c220387f34df232f04ab283ac","used":true,"who":"陈宇明"},{"_id":"59c08826421aa9118887ac42","createdAt":"2017-09-19T10:59:50.923Z","desc":"快速的 UI 修改工具,帮助你在开发阶段无需一次次修改小参数,编译看效果。","images":["http://img.gank.io/637974ea-4718-4d5a-899b-561856cb2ba0"],"publishedAt":"2017-09-19T12:07:31.405Z","source":"chrome","type":"Android","url":"https://github.com/material-foundation/material-remixer-android","used":true,"who":"代码家"},{"_id":"59c08919421aa911847a03b6","createdAt":"2017-09-19T11:03:53.911Z","desc":"常用、好用的 Android Studio 插件集合。","publishedAt":"2017-09-19T12:07:31.405Z","source":"chrome","type":"Android","url":"https://github.com/balsikandar/Android-Studio-Plugins","used":true,"who":"代码家"},{"_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 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 : 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 */ 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; } } }7.Bean2package com.example.lx7; /** * author:Created by WangZhiQiang on 2017/9/20. */ public class Bean2 { private String text; private String img; public Bean2(String text, String img) { this.text = text; this.img = img; } public Bean2(String text) { this.text = text; } public String getText() { return text; } public void setText(String text) { this.text = text; } public void setImg(String img) { this.img = img; } public String getImg() { return img; } }