//侧拉(Xlistview)和和导航滑动 public class MainActivity extends AppCompatActivity implements View.OnClickListener,XListView.IXListViewListener{private HorizontalScrollView hs; private LinearLayout show; private ViewPager vp; private String[] titles; private ArrayList<TextView> list; private XListView xlv; private List<Bean2.DataBean.ComicsBean> comics; private ArrayList<User2> list2; private ViewHolder vh; private View view; private Mydatd2 mydatd2; private Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); mydatd2.notifyDataSetChanged(); close(); } }; //关闭xlistview执行的上拉下拉 private void close() { xlv.stopLoadMore(); xlv.stopRefresh(); xlv.setRefreshTime("2017-09-17"); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); hs = (HorizontalScrollView) findViewById(R.id.hs); show = (LinearLayout) findViewById(R.id.showtitle); vp = (ViewPager) findViewById(R.id.vp); //xlistview xlv = (XListView) findViewById(R.id.xlv); xlv.setPullLoadEnable(true); xlv.setPullRefreshEnable(true); xlv.setXListViewListener(this); new Thread(){ private StringBuilder builder; @Override public void run() { super.run(); try { URL url = new URL("http://api.kkmh.com/v1/daily/comic_lists/0?since=0&gender=0&sa_event=eyJwcm9qZWN0Ijoia3VhaWthbl9hcHAiLCJ0aW1lIjoxNDg3NzQyMjQwNjE1LCJwcm9wZXJ0aWVzIjp7IkhvbWVwYWdlVGFiTmFtZSI6IueDremXqCIsIlZDb21tdW5pdHlUYWJOYW1lIjoi54Ot6ZeoIiwiJG9zX3ZlcnNpb24iOiI0LjQuMiIsIkdlbmRlclR5cGUiOiLlpbPniYgiLCJGcm9tSG9tZXBhZ2VUYWJOYW1lIjoi54Ot6ZeoIiwiJGxpYl92ZXJzaW9uIjoiMS42LjEzIiwiJG5ldHdvcmtfdHlwZSI6IldJRkkiLCIkd2lmaSI6dHJ1ZSwiJG1hbnVmYWN0dXJlciI6ImJpZ25veCIsIkZyb21Ib21lcGFnZVVwZGF0ZURhdGUiOjAsIiRzY3JlZW5faGVpZ2h0IjoxMjgwLCJIb21lcGFnZVVwZGF0ZURhdGUiOjAsIlByb3BlcnR5RXZlbnQiOiJSZWFkSG9tZVBhZ2UiLCJGaW5kVGFiTmFtZSI6IuaOqOiNkCIsImFidGVzdF9ncm91cCI6MTEsIiRzY3JlZW5fd2lkdGgiOjcyMCwiJG9zIjoiQW5kcm9pZCIsIlRyaWdnZXJQYWdlIjoiSG9tZVBhZ2UiLCIkY2FycmllciI6IkNoaW5hIE1vYmlsZSIsIiRtb2RlbCI6IlZQaG9uZSIsIiRhcHBfdmVyc2lvbiI6IjMuNi4yIn0sInR5cGUiOiJ0cmFjayIsImRpc3RpbmN0X2lkIjoiQTo2YWRkYzdhZTQ1MjUwMzY1Iiwib3JpZ2luYWxfaWQiOiJBOjZhZGRjN2FlNDUyNTAzNjUiLCJldmVudCI6IlJlYWRIb21lUGFnZSJ9"); try { URLConnection connection = url.openConnection(); InputStream inputStream = connection.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); builder = new StringBuilder(); String tem=""; while((tem=reader.readLine())!=null){ builder.append(tem); } Log.e("========读取到的数据=======", builder.toString()); } catch (IOException e) { e.printStackTrace(); } } catch (MalformedURLException e) { e.printStackTrace(); } runOnUiThread(new Runnable() { @Override public void run() { list2 = new ArrayList<>(); Gson gson = new Gson(); Bean2 bean2 = gson.fromJson(builder.toString(), Bean2.class); comics = bean2.getData().getComics(); for(int j=0;j<comics.size();j++){ String title = comics.get(j).getTitle(); Bean2.DataBean.ComicsBean.TopicBean topic = comics.get(j).getTopic(); String cover_image_url = topic.getCover_image_url(); list2.add(new User2(title,cover_image_url)); } mydatd2 = new Mydatd2(); xlv.setAdapter(mydatd2); } }); } }.start(); titles = new String[]{"头条", "社会", "国内", "国际", "娱乐", "体育", "军事", "科技", "财经", "时尚"}; list = new ArrayList<>(); for(int i=0;i<titles.length;i++){ TextView view = new TextView(this); view.setText(titles[i]); view.setTextSize(20); if(i==0){ view.setTextColor(Color.RED); }else{ view.setTextColor(Color.BLACK); } view.setOnClickListener(this); view.setId(i+1000); //设置textview控件的宽高 LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); params.setMargins(20,10,20,10);//设置内边距 show.addView(view,params);//将textview控件添加到滚动控件里面的LinearLayout布局 list.add(view);//添加到集合 } //开始做viewPager vp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) { @Override public Fragment getItem(int position) { if(position==0){ fragment2 fragment2 = new fragment2(); return fragment2; } return fragment.getin(list.get(position).getText().toString()); } @Override public int getCount() { return titles.length; } }); vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { 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 = list.get(position); int width = textView.getWidth();//获取宽度 //计算该滑到哪里 hs.scrollTo((width+20)*position,0);//viewpager滑动时与滚动控件想配合一起滑动 } @Override public void onPageScrollStateChanged(int state) { } }); } @Override public void onClick(View view) { int id = view.getId(); vp.setCurrentItem(id-1000); } @Override public void onRefresh() { handler.postDelayed(new Runnable() { @Override public void run() { list2.add(0,new User2("怪物恋人","http://f2.kkmh.com/image/170915/a56o0vmx8.webp-w640")); } },3000); handler.sendEmptyMessageDelayed(0,3000); } @Override public void onLoadMore() { handler.postDelayed(new Runnable() { @Override public void run() { list2.add(new User2("怪物恋人","http://f2.kkmh.com/image/170915/a56o0vmx8.webp-w640")); handler.sendEmptyMessage(0); } },3000); } class Mydatd2 extends BaseAdapter{ @Override public int getCount() { return list2.size(); } @Override public Object getItem(int i) { return list2.get(i); } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { if(view==null){ vh = new ViewHolder(); view = View.inflate(MainActivity.this, R.layout.xlistviewtext, null); vh.name = (TextView) view.findViewById(R.id.name); vh.img = (ImageView) view.findViewById(R.id.img); view.setTag(vh); }else{ vh= (ViewHolder) view.getTag(); } vh.name.setText(list2.get(i).name); ImageLoader.getInstance().displayImage(list2.get(i).imgg, vh.img); return view; } } class ViewHolder { TextView name; ImageView img; } } /** * Created by HQ on 2017/9/20. * 接参传参 * public class fragment extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { Bundle arguments = getArguments(); String sss = arguments.getString("sss"); TextView textView = new TextView(getActivity()); textView.setText(sss); return textView; } public static Fragment getin(String titles){ fragment fragment0 = new fragment(); Bundle bundle = new Bundle(); bundle.putString("sss",titles); fragment0.setArguments(bundle); return fragment0; } }/** * Created by HQ on 2017/9/20. * 实现pullToRefresh的上拉下拉,和无限轮播加原点 */ public class fragment2 extends Fragment { private PullToRefreshListView pt; private StringBuilder builder; int i = 1; private ptdata ptdata; private List<Bean.NewslistBean> newslist; List<User> list = new ArrayList<User>(); private ViewHolder vh; private ViewPager vp; private LinearLayout ll; private View view; private ArrayList<ImageView> imagelist; private ImageView view1; Handler handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); int i = vp.getCurrentItem() + 1; vp.setCurrentItem(i); handler.sendEmptyMessageDelayed(0, 2000); } }; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { view = inflater.inflate(R.layout.fragment, container, false); return view; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); pt = (PullToRefreshListView) view.findViewById(R.id.pt); //绘制viewPager子布局 View view = View.inflate(getActivity(), R.layout.vp, null); //获取控件 vp = (ViewPager) view.findViewById(R.id.vp); ll = (LinearLayout) view.findViewById(R.id.ll); //添加头布局 ListView listView = pt.getRefreshableView(); listView.addHeaderView(view); pt.setMode(PullToRefreshBase.Mode.BOTH); //适配器 ptdata = new ptdata(); pt.setAdapter(ptdata); handler.sendEmptyMessageDelayed(0, 2000); //异步线程操作 new Mysanysk().execute("https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10"); //pt监听 //上拉下拉 pt.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() { @Override public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) { i = 1; new Mysanysk().execute("https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10"); } @Override public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) { i++; new Mysanysk().execute("https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10" + i); } }); vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { for (int i = 0; i < imagelist.size(); i++) { if (position % newslist.size() == i) { imagelist.get(position % newslist.size()).setImageResource(R.drawable.dot_focuable); } else { imagelist.get(i).setImageResource(R.drawable.dot_normal); } } } @Override public void onPageScrollStateChanged(int state) { } }); } //加点 private void nodata() { ll.removeAllViews(); imagelist = new ArrayList<ImageView>(); if (imagelist != null) { imagelist.clear(); } for (int i = 0; i < newslist.size(); i++) { view1 = new ImageView(getActivity()); if (i == vp.getCurrentItem()) { view1.setImageResource(R.drawable.dot_focuable); } else { view1.setImageResource(R.drawable.dot_normal); } LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(20, 20); params.setMargins(10, 0, 10, 0); imagelist.add(view1); ll.addView(view1, params); } } //第一步 //异步 class Mysanysk extends AsyncTask<String, Void, String> { //读取数据 @Override protected String doInBackground(String... strings) { try { URL url = new URL(strings[0]); try { URLConnection connection = url.openConnection(); InputStream stream = connection.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(stream)); builder = new StringBuilder(); String tem = ""; while ((tem = reader.readLine()) != null) { builder.append(tem); } } catch (IOException e) { e.printStackTrace(); } } catch (MalformedURLException e) { e.printStackTrace(); } Log.e("=======读取到的数据==========", builder.toString()); return builder.toString(); } //解析数据 @Override protected void onPostExecute(String s) { super.onPostExecute(s); Gson gson = new Gson(); Bean bean = gson.fromJson(builder.toString(), Bean.class); newslist = bean.getNewslist(); for (int i = 0; i < newslist.size(); i++) { String picUrl = newslist.get(i).getPicUrl(); String title = newslist.get(i).getTitle(); list.add(new User(title, picUrl)); } vp.setAdapter(new vpdata()); nodata(); ptdata.notifyDataSetChanged(); pt.onRefreshComplete(); } } //pt的适配器 class ptdata 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; } @Override public View getView(int i, View view, ViewGroup viewGroup) { if (view == null) { vh = new ViewHolder(); view = View.inflate(getActivity(), R.layout.textview, null); vh.name = (TextView) view.findViewById(R.id.name); vh.img = (ImageView) view.findViewById(R.id.img); view.setTag(vh); } else { vh = (ViewHolder) view.getTag(); } vh.name.setText(list.get(i).name); ImageLoader.getInstance().displayImage(list.get(i).url, vh.img); return view; } } class ViewHolder { TextView name; ImageView img; } //vp的适配器 class vpdata extends PagerAdapter { private View view; @Override public int getCount() { return Integer.MAX_VALUE; } @Override public boolean isViewFromObject(View view, Object object) { return view == object; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object); //super.destroyItem(container, position, object); } @Override public Object instantiateItem(ViewGroup container, int position) { String picUrl = newslist.get(position % newslist.size()).getPicUrl(); view = View.inflate(getActivity(), R.layout.vptextview, null); ImageView img = (ImageView) view.findViewById(R.id.img2); ImageLoader.getInstance().displayImage(picUrl, img); container.addView(view); return view; } } } //Bean类public class User { public String name; public String url; public User(String name, String url) { this.name = name; this.url = url; } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", url='" + url + '\'' + '}'; } }public class Appi extends Application { @Override public void onCreate() { super.onCreate(); ImageLoaderConfiguration build = new ImageLoaderConfiguration.Builder(this).build(); ImageLoader.getInstance().init(build); } }//主布局 <?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout android:id="@+id/dl" 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="bbb.bwie.com.zonghelx.MainActivity"> <LinearLayout android:orientation="vertical" 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" android:background="@mipmap/ic_launcher_round" /> <HorizontalScrollView android:id="@+id/hs" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="30dip" > <LinearLayout android:id="@+id/showtitle" 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> </LinearLayout> <LinearLayout android:id="@+id/aaaa" android:orientation="horizontal" android:background="@mipmap/ic_launcher_round" android:layout_gravity="left" android:choiceMode="singleChoice" android:layout_width="match_parent" android:layout_height="match_parent"><bbb.bwie.com.zonghelx.XListView android:id="@+id/xlv" android:layout_width="match_parent" android:layout_height="match_parent"> </bbb.bwie.com.zonghelx.XListView>
</LinearLayout></android.support.v4.widget.DrawerLayout>
//xlistview展示的布局<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ImageView android:id="@+id/img" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
//PullToRefreshListView布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <com.handmark.pulltorefresh.library.PullToRefreshListView android:id="@+id/pt" android:layout_width="match_parent" android:layout_height="match_parent"> </com.handmark.pulltorefresh.library.PullToRefreshListView> </LinearLayout> //viewPager布局<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v4.view.ViewPager android:layout_width="match_parent" android:layout_height="300dp" android:id="@+id/vp" ></android.support.v4.view.ViewPager> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/ll" android:orientation="horizontal" ></LinearLayout> </LinearLayout> //pullTorefresh子布局<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/img" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/name" /> </LinearLayout> //图片子布局<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/img2" /></LinearLayout> (备注:导入Xlistview依赖和pullTorefresh的依赖,依赖是自己封装的,加权限)
Horizontal+异步+pullTorefresh+viewPager+侧滑(XListView)
最新推荐文章于 2019-02-22 10:19:32 发布