RadioGroup+Fragment+PulltorefreshScrollView+GridView+侧拉菜单+TabLayout+ViewPager
来源:互联网 发布:惠普暗影精灵3怎么优化 编辑:程序博客网 时间:2024/06/05 16:16
在build.gradle添加依赖库
compile ‘com.android.support:design:25.2.0’
compile ‘com.github.userswlwork:pull-to-refresh:1.0.0’
compile ‘com.youth.banner:banner:1.4.10’
再导入gson和imgaloder jar包
在添加权限
网络权限和name
布局
activiti.xml
<?xml version="1.0" encoding="utf-8"?><android.support.v4.widget.DrawerLayout 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:id="@+id/drawer" > <!-- 主内容--> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <FrameLayout android:id="@+id/fl" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="9" > </FrameLayout> <RadioGroup android:id="@+id/rg" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal" android:layout_marginTop="10dp" > <RadioButton android:id="@+id/btn1" android:layout_width="0dp" android:layout_height="wrap_content" android:button="@null" android:text="首页" android:gravity="center_horizontal" android:textSize="20dp" android:layout_weight="1" android:checked="true" android:background="@drawable/dianji" /> <RadioButton android:id="@+id/btn2" android:layout_width="0dp" android:layout_height="wrap_content" android:button="@null" android:text="分类" android:gravity="center_horizontal" android:textSize="20dp" android:layout_weight="1" android:background="@drawable/dianji" /> <RadioButton android:id="@+id/btn3" android:layout_width="0dp" android:layout_height="wrap_content" android:button="@null" android:text="发现" android:gravity="center_horizontal" android:textSize="20dp" android:layout_weight="1" android:background="@drawable/dianji" /> <RadioButton android:id="@+id/btn4" android:layout_width="0dp" android:layout_height="wrap_content" android:button="@null" android:text="购物车" android:gravity="center_horizontal" android:textSize="20dp" android:layout_weight="1" android:background="@drawable/dianji" /> <RadioButton android:id="@+id/btn5" android:layout_width="0dp" android:layout_height="wrap_content" android:button="@null" android:text="我的" android:gravity="center_horizontal" android:textSize="20dp" android:layout_weight="1" android:background="@drawable/dianji" /> </RadioGroup> </LinearLayout> <!--侧滑菜单--> <LinearLayout android:id="@+id/lin" android:layout_width="300dp" android:layout_height="match_parent" android:background="#f89" android:layout_gravity="start" android:orientation="vertical" > <ImageView android:id="@+id/draimg" android:layout_marginTop="10dp" android:layout_width="290dp" android:layout_height="260dp" android:src="@mipmap/ic_launcher" /> <ListView android:id="@+id/dralv" android:layout_width="match_parent" android:layout_marginTop="30dp" android:layout_height="match_parent"> </ListView> </LinearLayout></android.support.v4.widget.DrawerLayout>
Fragment1
<?xml version="1.0" encoding="utf-8"?><com.handmark.pulltorefresh.library.PullToRefreshScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/pslv" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <com.youth.banner.Banner android:id="@+id/banner" android:layout_width="match_parent" android:layout_height="300dp" > </com.youth.banner.Banner> <com.example.zonghe.Mygridview android:id="@+id/gv" android:layout_width="match_parent" android:layout_height="match_parent" android:numColumns="2" > </com.example.zonghe.Mygridview> </LinearLayout></com.handmark.pulltorefresh.library.PullToRefreshScrollView>
Fragemn2
<?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"> <ListView android:id="@+id/f2_lv" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" > </ListView> <GridView android:id="@+id/f2_gv" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="3" android:numColumns="3" android:layout_marginLeft="10dp" > </GridView></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" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical"> <android.support.design.widget.TabLayout android:id="@+id/f3_tab" android:layout_width="match_parent" android:layout_height="50dp" app:tabGravity="center" app:tabIndicatorColor="@color/colorAccent" app:tabMode="scrollable" app:tabSelectedTextColor="@color/colorPrimaryDark" app:tabTextColor="@color/colorPrimary" > </android.support.design.widget.TabLayout> <android.support.v4.view.ViewPager android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/f3_vp" > </android.support.v4.view.ViewPager></LinearLayout>
4
<?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" > <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:orientation="horizontal" android:layout_weight="1" > <ImageView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:src="@mipmap/q4" /> <ImageView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:src="@mipmap/q5" android:layout_marginLeft="10dp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:orientation="horizontal" android:layout_weight="1" > <ImageView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:src="@mipmap/a10" /> <ImageView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:src="@mipmap/t8" android:layout_marginLeft="10dp" /> </LinearLayout></LinearLayout>
5
<?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"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:textSize="30dp" android:text="我在清晨的路上谁被我遗忘我在深夜里旅行谁被我遗忘肩上的破旧行囊能收藏多少坚强不如全身赤裸还给我那脆弱明知这是一场意外你要不要来明知这是一场重伤害你会不会来当疯狂慢慢从爱情离开还有什么你值得感慨如果风景早已都不存在我想我谁都不爱都不爱都不爱都不爱都不爱都不爱都不爱都不爱都不爱都不爱我在清晨的路上谁被我遗忘我在深夜里旅行谁被我遗忘肩上的破旧行囊能收藏多少坚强不如全身赤裸还给我那脆弱明知这是一场意外你要不要来明知这是一场重伤害你会不会来当疯狂慢慢从爱情离开还有什么你值得感慨如果风景早已都不存在我想我谁都不爱明知这是一场意外你要不要来明知这是一场重伤害你会不会来当疯狂慢慢从爱情离开还有什么你值得感慨如果风景早已都不存在我想我谁都不爱谁都不爱" android:gravity="center_horizontal" /></LinearLayout>
item
<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="100dp" android:layout_height="80dp" /> <TextView android:id="@+id/tv" android:layout_width="match_parent" android:layout_height="wrap_content" /></LinearLayout>
item2
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ImageView android:id="@+id/img" android:layout_width="100dp" android:layout_height="80dp" /> <TextView android:id="@+id/tv" android:layout_width="match_parent" android:layout_height="wrap_content" /></LinearLayout>
itemtitle
<?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"> <TextView android:id="@+id/title" android:layout_width="match_parent" android:layout_height="wrap_content" /></LinearLayout>
itemmuser
<?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"> <TextView android:id="@+id/tv" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="40dp" android:textSize="20dp" android:layout_gravity="center_horizontal" /></LinearLayout>
f3_fragent.xml
<?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/f3_plv" android:layout_width="match_parent" android:layout_height="match_parent"> </com.handmark.pulltorefresh.library.PullToRefreshListView></LinearLayout>
颜色布局
dianji
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_checked="true" android:drawable="@color/colorAccent" ></item> <item android:state_checked="false" android:drawable="@color/colorPrimary" ></item></selector>
MainActivity
import android.os.Bundle;import android.support.annotation.IdRes;import android.support.v4.widget.DrawerLayout;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.ArrayAdapter;import android.widget.FrameLayout;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.ListView;import android.widget.RadioGroup;import com.example.zonghe.fragment.Myfragment1;import com.example.zonghe.fragment.Myfragment2;import com.example.zonghe.fragment.Myfragment3;import com.example.zonghe.fragment.Myfragment4;import com.example.zonghe.fragment.Myfragment5;import java.util.ArrayList;import java.util.List;public class MainActivity extends AppCompatActivity { private DrawerLayout drawer; private LinearLayout lin; private ListView lv; private FrameLayout fl; private RadioGroup rg; List<String> li = new ArrayList<>(); private ImageView img; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); li.add("个人设置"); li.add("缓存"); li.add("夜间模式"); li.add("配置"); drawer = (DrawerLayout) findViewById(R.id.drawer); lin = (LinearLayout) findViewById(R.id.lin); lv = (ListView) findViewById(R.id.dralv); img = (ImageView) findViewById(R.id.draimg); ArrayAdapter<String> array = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, li); lv.setAdapter(array); img.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { drawer.closeDrawer(lin); } }); getSupportFragmentManager().beginTransaction().replace(R.id.fl, new Myfragment1()).commit(); rg = (RadioGroup) findViewById(R.id.rg); rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) { switch (i) { case R.id.btn1: getSupportFragmentManager().beginTransaction().replace(R.id.fl, new Myfragment1()).commit(); break; case R.id.btn2: getSupportFragmentManager().beginTransaction().replace(R.id.fl, new Myfragment2()).commit(); break; case R.id.btn3: getSupportFragmentManager().beginTransaction().replace(R.id.fl, new Myfragment3()).commit(); break; case R.id.btn4: getSupportFragmentManager().beginTransaction().replace(R.id.fl, new Myfragment4()).commit(); break; case R.id.btn5: getSupportFragmentManager().beginTransaction().replace(R.id.fl, new Myfragment5()).commit(); break; } } }); }}
Fragement1
package com.example.zonghe.fragment;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.GridView;import android.widget.ScrollView;import com.example.zonghe.R;import com.example.zonghe.adapter.MyAdapter;import com.example.zonghe.bean.DataDataBean;import com.example.zonghe.utils.Mybanner;import com.example.zonghe.utils.ShuJu;import com.example.zonghe.utils.Utils;import com.google.gson.Gson;import com.handmark.pulltorefresh.library.ILoadingLayout;import com.handmark.pulltorefresh.library.PullToRefreshBase;import com.handmark.pulltorefresh.library.PullToRefreshScrollView;import com.youth.banner.Banner;import java.util.ArrayList;import java.util.List;/** * Created by Administrator on 2017/11/20. */public class Myfragment1 extends Fragment { int index = 1; int type = 1; String Url = "http://www.yulin520.com/a2a/impressApi/news/mergeList?pageSize=10&page=" + index; private PullToRefreshScrollView pslv; private Banner ban; List<String> list = new ArrayList<>(); private GridView gv; List<DataDataBean.DataBean> data = new ArrayList<>(); private MyAdapter adapter; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View vi = inflater.inflate(R.layout.fragment1, container, false); list.add("http://pic8.nipic.com/20100701/5290458_114840036316_2.jpg"); list.add("http://pic2.nipic.com/20090424/1468853_230119053_2.jpg"); list.add("http://img3.3lian.com/2013/s1/20/d/57.jpg"); list.add("http://pic39.nipic.com/20140226/18071023_164300608000_2.jpg"); list.add("http://a0.att.hudong.com/15/08/300218769736132194086202411_950.jpg"); //设置无线轮播 ban = (Banner) vi.findViewById(R.id.banner); gv = (GridView) vi.findViewById(R.id.gv); ban.setImageLoader(new Mybanner());//添加自定义的图片加载器 ban.setImages(list);//设置图片资源 ban.start();//开始轮播 pslv = (PullToRefreshScrollView) vi.findViewById(R.id.pslv); pslv.setMode(PullToRefreshBase.Mode.BOTH); //3.通过getLoadingLayoutProxy 方法来指定上拉和下拉时显示的状态的区别(也就是设置向下拉的时候头部里面显示的文字) //此时这里设置的是下拉刷新的时候显示的文字,所以第一个设置true表示现在是刷新,第二个设置为false ILoadingLayout startLabels = pslv.getLoadingLayoutProxy(true, false); startLabels.setPullLabel("下拉刷新"); startLabels.setRefreshingLabel("正在刷新..."); startLabels.setReleaseLabel("放开刷新"); ILoadingLayout endLabels = pslv.getLoadingLayoutProxy(false, true); endLabels.setPullLabel("上拉刷新"); endLabels.setRefreshingLabel("正在载入..."); endLabels.setReleaseLabel("放开刷新..."); //设置监听 pslv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() { //刷新 @Override public void onPullDownToRefresh(PullToRefreshBase<ScrollView> pullToRefreshBase) { index = 1; type = 1; add(); } //加载 @Override public void onPullUpToRefresh(PullToRefreshBase<ScrollView> pullToRefreshBase) { index++; type = 2; add(); } }); add(); return vi; } //展示数据 public void add() { Utils.getData(Url, getActivity(), new ShuJu() { @Override public void getData(String s) { Gson gson = new Gson(); DataDataBean dataBean = gson.fromJson(s, DataDataBean.class); if (type == 1) { data.clear(); data.addAll(dataBean.getData()); } else { data.addAll(dataBean.getData()); } setAdapter(); //关闭刷新 new Handler().postDelayed(new Runnable() { @Override public void run() { pslv.onRefreshComplete(); } }, 1000); } }); } //设置适配器 public void setAdapter() { if (adapter == null) { adapter = new MyAdapter(data, getActivity()); gv.setAdapter(adapter); } else { adapter.notifyDataSetChanged(); } }}
fragemnt02
package com.example.zonghe.fragment;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.AdapterView;import android.widget.GridView;import android.widget.ListView;import com.example.zonghe.R;import com.example.zonghe.adapter.FerAdapter;import com.example.zonghe.adapter.MyF2ListAdapter;import com.example.zonghe.bean.DataBean;import com.example.zonghe.bean.User;import com.example.zonghe.utils.ShuJu;import com.example.zonghe.utils.Utils;import com.google.gson.Gson;import java.util.ArrayList;import java.util.List;/** * Created by Administrator on 2017/11/20. */public class Myfragment2 extends Fragment { private GridView gv; private ListView lv; List<User> li = new ArrayList<>(); private String path; private List<DataBean.NewslistBean> newslist; private FerAdapter ferAdapter; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View vi = inflater.inflate(R.layout.fragment2, container, false); lv = (ListView) vi.findViewById(R.id.f2_lv); gv = (GridView) vi.findViewById(R.id.f2_gv); put(); MyF2ListAdapter array = new MyF2ListAdapter(li, getActivity()); lv.setAdapter(array); lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { User user = li.get(i); int id = user.getId(); Log.d("qqq", "+++++++++++" + id); path = "http://api.tianapi.com/huabian/?key=56059a1b935aa448c764a0c3b9e63b7c&num=" + id; add(); } }); return vi; } public void add() { Utils.getData(path, getActivity(), new ShuJu() { @Override public void getData(String s) { Gson gson = new Gson(); DataBean dataBean = gson.fromJson(s, DataBean.class); newslist = dataBean.getNewslist(); setAdapter(); } }); } //设置适配器 public void setAdapter() { if (ferAdapter == null) { ferAdapter = new FerAdapter(newslist, getActivity()); gv.setAdapter(ferAdapter); } else { ferAdapter.notifyDataSetChanged(); } } //添加listview的数据 public void put() { User us1 = new User("钱包手袋", 10); User us2 = new User("美妆个护", 20); User us3 = new User("钟表珠宝", 30); User us4 = new User("手机数码", 40); User us5 = new User("电脑办公", 50); User us6 = new User("家用电器", 60); User us7 = new User("酒水饮料", 70); li.add(us1); li.add(us2); li.add(us3); li.add(us4); li.add(us5); li.add(us6); li.add(us7); }}
fragment03
package com.example.zonghe.fragment;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.FragmentManager;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 com.example.zonghe.R;import java.util.ArrayList;import java.util.List;/** * Created by Administrator on 2017/11/20. */public class Myfragment3 extends Fragment { List<String> li = new ArrayList<>(); List<Fragment> frag = new ArrayList<>(); private TabLayout tab; private ViewPager vp; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View vi = inflater.inflate(R.layout.fragment3, container, false); tab = (TabLayout) vi.findViewById(R.id.f3_tab); vp = (ViewPager) vi.findViewById(R.id.f3_vp); li.add("精选"); li.add("英伦风"); li.add("直播"); li.add("订阅"); li.add("视频购"); li.add("阅答"); vp.setAdapter(new Myf3Adapter(getFragmentManager())); tab.setupWithViewPager(vp); vp.setOffscreenPageLimit(li.size()); return vi; } class Myf3Adapter extends FragmentPagerAdapter { public Myf3Adapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { return new MyFragment31(); } @Override public int getCount() { return li.size(); } @Override public CharSequence getPageTitle(int position) { return li.get(position); } }}
fragment04
package com.example.zonghe.fragment;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 com.example.zonghe.R;/** * Created by Administrator on 2017/11/20. */public class Myfragment4 extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View vi = inflater.inflate(R.layout.fragment4, container, false); return vi; }}
fragment05
package com.example.zonghe.fragment;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 com.example.zonghe.R;/** * Created by Administrator on 2017/11/20. */public class Myfragment5 extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View vi = inflater.inflate(R.layout.fragment5, container, false); return vi; }}
MyFragment31`
package com.example.zonghe.fragment;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.ListView;import com.example.zonghe.R;import com.example.zonghe.adapter.DuotiaomuAdapter;import com.example.zonghe.bean.DataDataBean;import com.example.zonghe.utils.ShuJu;import com.example.zonghe.utils.Utils;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.util.ArrayList;import java.util.List;/** * Created by Administrator on 2017/11/20. */public class MyFragment31 extends Fragment { private PullToRefreshListView plv; int index = 1; int type = 1; String Url = "http://www.yulin520.com/a2a/impressApi/news/mergeList?pageSize=10&page=" + index; List<DataDataBean.DataBean> data = new ArrayList<>(); private DuotiaomuAdapter adapter; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View vi = inflater.inflate(R.layout.f3_fragment, container, false); plv = (PullToRefreshListView) vi.findViewById(R.id.f3_plv); plv.setMode(PullToRefreshBase.Mode.BOTH); //3.通过getLoadingLayoutProxy 方法来指定上拉和下拉时显示的状态的区别(也就是设置向下拉的时候头部里面显示的文字) //此时这里设置的是下拉刷新的时候显示的文字,所以第一个设置true表示现在是刷新,第二个设置为false ILoadingLayout startLabels = plv.getLoadingLayoutProxy(true, false); startLabels.setPullLabel("下拉刷新"); startLabels.setRefreshingLabel("正在刷新..."); startLabels.setReleaseLabel("放开刷新"); ILoadingLayout endLabels = plv.getLoadingLayoutProxy(false, true); endLabels.setPullLabel("上拉刷新"); endLabels.setRefreshingLabel("正在载入..."); endLabels.setReleaseLabel("放开刷新..."); //设置监听 plv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() { @Override public void onPullDownToRefresh(PullToRefreshBase<ListView> pullToRefreshBase) { index = 1; type = 1; add(); } @Override public void onPullUpToRefresh(PullToRefreshBase<ListView> pullToRefreshBase) { index++; type = 2; add(); } }); add(); return vi; } //展示数据 public void add() { Utils.getData(Url, getActivity(), new ShuJu() { @Override public void getData(String s) { Gson gson = new Gson(); DataDataBean dataBean = gson.fromJson(s, DataDataBean.class); if (type == 1) { data.clear(); data.addAll(dataBean.getData()); } else { data.addAll(dataBean.getData()); } setAdapter(); //关闭刷新 new Handler().postDelayed(new Runnable() { @Override public void run() { plv.onRefreshComplete(); } }, 1000); } }); } //设置适配器 public void setAdapter() { if (adapter == null) { adapter = new DuotiaomuAdapter(data, getActivity()); plv.setAdapter(adapter); } else { adapter.notifyDataSetChanged(); } }}
适配器
MyAdapter
package com.example.zonghe.adapter;import android.content.Context;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.TextView;import com.example.zonghe.R;import com.example.zonghe.bean.DataDataBean;import com.example.zonghe.utils.Utils;import com.nostra13.universalimageloader.core.ImageLoader;import java.util.List;/** * 适配器 * <p> * Created by Administrator on 2017/11/20. */public class MyAdapter extends BaseAdapter { List<DataDataBean.DataBean> li; Context ct; public MyAdapter(List<DataDataBean.DataBean> li, Context ct) { this.li = li; this.ct = ct; } @Override public int getCount() { return li.size(); } @Override public Object getItem(int i) { return li.get(i); } @Override public long getItemId(int i) { return i; } //优化 @Override public View getView(int i, View view, ViewGroup viewGroup) { ViewHouder vh; if (view == null) { vh = new ViewHouder(); view = View.inflate(ct, R.layout.item, null); vh.img = (ImageView) view.findViewById(R.id.img); vh.tv = (TextView) view.findViewById(R.id.tv); view.setTag(vh); } else { vh = (ViewHouder) view.getTag(); } ImageLoader.getInstance().displayImage(li.get(i).getImg(), vh.img, Utils.getTuXing()); vh.tv.setText(li.get(i).getIntroduction()); return view; } class ViewHouder { ImageView img; TextView tv; }}
MyF2ListAdapter
package com.example.zonghe.adapter;import android.content.Context;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.TextView;import com.example.zonghe.R;import com.example.zonghe.bean.User;import java.util.List;/** * 适配器 * * Created by Administrator on 2017/11/20. */public class MyF2ListAdapter extends BaseAdapter { List<User> li; Context ct; public MyF2ListAdapter(List<User> li, Context ct) { this.li = li; this.ct = ct; } @Override public int getCount() { return li.size(); } @Override public Object getItem(int i) { return li.get(i); } @Override public long getItemId(int i) { return i; } //优化 @Override public View getView(int i, View view, ViewGroup viewGroup) { ViewHouder vh; if(view==null){ vh=new ViewHouder(); view=View.inflate(ct, R.layout.itemuser,null); vh.tv= (TextView) view.findViewById(R.id.tv); view.setTag(vh); }else{ vh = (ViewHouder) view.getTag(); } vh.tv.setText(li.get(i).getName()); return view; } class ViewHouder{ TextView tv; }}
FerAdapter
package com.example.zonghe.adapter;import android.content.Context;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.TextView;import com.example.zonghe.R;import com.example.zonghe.bean.DataBean;import com.example.zonghe.utils.Utils;import com.nostra13.universalimageloader.core.ImageLoader;import java.util.List;/** * 适配器 * <p> * Created by Administrator on 2017/11/20. */public class FerAdapter extends BaseAdapter { List<DataBean.NewslistBean> li; Context ct; public FerAdapter(List<DataBean.NewslistBean> li, Context ct) { this.li = li; this.ct = ct; } @Override public int getCount() { return li.size(); } @Override public Object getItem(int i) { return li.get(i); } @Override public long getItemId(int i) { return i; } //优化 @Override public View getView(int i, View view, ViewGroup viewGroup) { ViewHouder vh; if (view == null) { vh = new ViewHouder(); view = View.inflate(ct, R.layout.item2, null); vh.img = (ImageView) view.findViewById(R.id.img); vh.tv = (TextView) view.findViewById(R.id.tv); view.setTag(vh); } else { vh = (ViewHouder) view.getTag(); }ImageLoader.getInstance().displayImage(li.get(i).getPicUrl(), vh.img, Utils.getTuXing()); vh.tv.setText(li.get(i).getTitle()); return view; } class ViewHouder { ImageView img; TextView tv; }}
DuotiaomuAdapter
package com.example.zonghe.adapter;import android.content.Context;import android.util.Log;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.TextView;import com.example.zonghe.R;import com.example.zonghe.bean.DataDataBean;import com.example.zonghe.utils.Utils;import com.nostra13.universalimageloader.core.ImageLoader;import java.util.List;/** * 适配器 * * Created by Administrator on 2017/11/20. */public class DuotiaomuAdapter extends BaseAdapter { List<DataDataBean.DataBean> li; Context ct; int wu=0; int you=1; public DuotiaomuAdapter(List<DataDataBean.DataBean> li, Context ct) { this.li = li; this.ct = ct; } @Override public int getViewTypeCount() { return 2; } @Override public int getItemViewType(int position) { String img = li.get(position).getImg(); if (img==null){ return wu; }else{ return you; } } @Override public int getCount() { return li.size(); } @Override public Object getItem(int i) { return li.get(i); } @Override public long getItemId(int i) { return i; } //优化 @Override public View getView(int i, View view, ViewGroup viewGroup) { int type = getItemViewType(i); if (type==wu){ Log.d("qqq","进入无图"); ViewHoudertitle vh; if(view==null){ vh=new ViewHoudertitle(); view=View.inflate(ct, R.layout.itemtitle,null); vh.tv= (TextView) view.findViewById(R.id.tv); view.setTag(vh); }else{ vh = (ViewHoudertitle) view.getTag(); } vh.tv.setText(li.get(i).getIntroduction()); return view; }else{ Log.d("qqq","进入有图"); ViewHouder vh; if(view==null){ vh=new ViewHouder(); view=View.inflate(ct, R.layout.item,null); vh.img= (ImageView) view.findViewById(R.id.img); vh.tv= (TextView) view.findViewById(R.id.tv); view.setTag(vh); }else{ vh = (ViewHouder) view.getTag(); } ImageLoader.getInstance().displayImage(li.get(i).getImg(),vh.img, Utils.getTuXing()); vh.tv.setText(li.get(i).getIntroduction()); return view; } } class ViewHouder{ ImageView img; TextView tv; } class ViewHoudertitle{ TextView tv; }}
User
package com.example.zonghe.bean;/** * Created by Administrator on 2017/11/20. */public class User { private String name; private int id; public User(String name, int id) { this.name = name; this.id = id; } public String getName() { return name; } public int getId() { return id; } public void setName(String name) { this.name = name; } public void setId(int id) { this.id = id; }}
DataDataBean
package com.example.zonghe.bean;import java.util.List;/** * Created by Administrator on 2017/11/20. */public class DataDataBean { /** * code : 1 * width : 0 * success : true * height : 0 * data : [{"topTime":1469503026000,"recommend":0,"url":"http://www.yulin520.com/a2a/h5/impress/i1/334?a=1511162126397","createTime":1466160199000,"hxKey":"60fd9c2377f81ba1ccca450fb794e016","img":"http://img1.yulin520.com/news/HPUCVP74IFR0OKCZ9QQH.jpg#405_304","yulin":"81373","star":58,"title":"刘峰鸣 :能文能武,Call Me峰鸣哥","occupation":"证劵投资","userImg":"http://img1.yulin520.com/news/HPUCVP74IFR0OKCZ9QQH.jpg#405_304","impressEditId":334,"impressType":0,"remark":"","click":0,"introduction":"83年/巨蟹座/ 开朗幽默,热情善良,追求刺激/喜欢温柔,体贴,可爱,善良的女孩/金融","replyTimes":17,"userAge":33,"userName":"刘LLL","source":0,"reporter":0,"status":1,"content":""},{"topTime":1469503022000,"recommend":0,"url":"http://www.yulin520.com/a2a/h5/impress/i1/319?a=1511162126397","createTime":1465380170000,"hxKey":"dac884f7cc81691ba28a004091cbe122","img":"http://img1.yulin520.com/news/BPKZUX0MNFR0OT0WLCOD.png#598_450","yulin":"80821","star":77,"title":"刘佳怡 :直白女汉子也有个文艺梦","occupation":"保险","userImg":"http://img1.yulin520.com/news/BPKZUX0MNFR0OT0WLCOD.png#598_450","impressEditId":319,"impressType":0,"remark":"","click":0,"introduction":"91年/处女座/性格直白,爱好广泛,有点女汉子/喜欢性格开朗,三观契合,高冷文艺范,有肌肉,175+/公司职业","replyTimes":12,"userAge":25,"userName":"嘉咦咦咦","source":0,"reporter":0,"status":1,"content":""},{"topTime":1468980785000,"recommend":0,"url":"http://www.yulin520.com/a2a/h5/impress/i1/324?a=1511162126397","createTime":1465722569000,"hxKey":"ebf75ccd1fba85f248988d40e572914e","img":"http://img1.yulin520.com/news/UPPN83L0IFR0OBC1A4AK.jpg#800_600","yulin":"80569","star":52,"title":"陈松 :爱搞新花样的设计师","occupation":"互联网","userImg":"http://img1.yulin520.com/news/UPPN83L0IFR0OBC1A4AK.jpg#800_600","impressEditId":324,"impressType":0,"remark":"","click":0,"introduction":"87年/处女座/善良可爱,安静温柔,爱独立思考/喜欢三观相似,经历相似,性格活泼的女孩/互联网设计师 ","replyTimes":13,"userAge":29,"userName":"Song","source":0,"reporter":0,"status":1,"content":""},{"topTime":1468980773000,"recommend":0,"url":"http://www.yulin520.com/a2a/h5/impress/i1/323?a=1511162126397","createTime":1465721939000,"hxKey":"580786532670bc072b0aeb795bd6ddc7","img":"http://img1.yulin520.com/news/SPPW8T9QHFR0OM3HID0X.jpg#1280_960","yulin":"80983","star":101,"title":"秦心 :外形不娇小,心思很细腻","occupation":"银行","userImg":"http://img1.yulin520.com/news/SPPW8T9QHFR0OM3HID0X.jpg#1280_960","impressEditId":323,"impressType":0,"remark":"","click":0,"introduction":"90年/天蝎座/爱美食,爱分享,有点内向/喜欢阳光,干净,175+的男孩子/银行接待","replyTimes":16,"userAge":26,"userName":"秦心","source":0,"reporter":0,"status":1,"content":""},{"topTime":1468574992000,"recommend":0,"url":"http://www.yulin520.com/a2a/h5/impress/i1/337?a=1511162126397","createTime":1466162998000,"hxKey":"37dee7aaed1862c64c4fe38d75ccd52a","img":"http://img1.yulin520.com/news/XPW8OZXMSFR0O015UA7D.jpg#598_597","yulin":"81316","star":55,"title":"温其堂 :细心温柔IT\u201c熊猫\u201d男","occupation":"互联网","userImg":"http://img1.yulin520.com/news/XPW8OZXMSFR0O015UA7D.jpg#598_597","impressEditId":337,"impressType":0,"remark":"","click":0,"introduction":"93年/水瓶座/温柔体贴,踏实细心/喜欢三观相符的女孩子/前端工程师","replyTimes":17,"userAge":23,"userName":"榴莲","source":0,"reporter":0,"status":1,"content":""},{"topTime":1468574986000,"recommend":0,"url":"http://www.yulin520.com/a2a/h5/impress/i1/340?a=1511162126397","createTime":1466500236000,"hxKey":"2a294b9ffd9fb38d7b5b8739c821720f","img":"http://img1.yulin520.com/news/RPZ58LLNXFR0OKFGFHGK.jpg#616_695","yulin":"81370","star":72,"title":"曾思慧: 爱冒险爱尖叫的真性情Girl","occupation":"银行","userImg":"http://img1.yulin520.com/news/RPZ58LLNXFR0OKFGFHGK.jpg#616_695","impressEditId":340,"impressType":0,"remark":"","click":0,"introduction":"92年/金牛座 /性格随和,自信大方/喜欢脾气好,爱运动,对人友好的男生/公司职员","replyTimes":13,"userAge":24,"userName":"pony","source":0,"reporter":0,"status":1,"content":""},{"topTime":1468203095000,"recommend":0,"url":"http://www.yulin520.com/a2a/h5/impress/i1/341?a=1511162126397","createTime":1466678686000,"hxKey":"9f2d5f4346dee0c4983cb77f9a1892e7","img":"http://img1.yulin520.com/news/NQ1HJD75IFR0OARCWFEG.jpg#501_499","yulin":"80908","star":34,"title":"杨辉: 挑战自我从早起开始","occupation":"市场推广","userImg":"http://img1.yulin520.com/news/NQ1HJD75IFR0OARCWFEG.jpg#501_499","impressEditId":341,"impressType":0,"remark":"","click":0,"introduction":"91年/巨蟹座/乐观开朗,沉稳内敛,机智幽默/喜欢温柔,体贴,孝顺的女生","replyTimes":11,"userAge":25,"userName":"拯救","source":0,"reporter":0,"status":1,"content":""},{"topTime":1467618377000,"recommend":0,"url":"http://www.yulin520.com/a2a/h5/impress/i1/244?a=1511162126397","createTime":1461738088000,"hxKey":"751120bb7102fa563f098ee8c33ecb67","img":"http://img1.yulin520.com/news/SO9EZSX0QC90ONZY8SVZ.jpg#619_650","yulin":"77323","star":46,"title":"蔡嘉纯:咱东北菇凉特开朗","occupation":"市场推广","userImg":"http://img1.yulin520.com/news/SO9EZSX0QC90ONZY8SVZ.jpg#619_650","impressEditId":244,"impressType":0,"remark":"","click":0,"introduction":"92年 狮子座/开朗东北妞/想开书店/公司职员","replyTimes":9,"userAge":24,"userName":"林bibibi","source":0,"reporter":0,"status":1,"content":""},{"topTime":1467367384000,"recommend":0,"url":"http://www.yulin520.com/a2a/h5/impress/i1/246?a=1511162126397","createTime":1461749927000,"hxKey":"785fd3ce68d2a280a99896946dc67bf2","img":"http://img1.yulin520.com/news/OOAA5UE7EC90OM4X4AQ7.jpg#800_600","yulin":"77479","star":41,"title":"李林:曾经的淘气小子","occupation":"信贷","userImg":"http://img1.yulin520.com/news/OOAA5UE7EC90OM4X4AQ7.jpg#800_600","impressEditId":246,"impressType":0,"remark":"","click":0,"introduction":"94年 水瓶座/曾经是淘气包/人缘好讲义气/金融业客户经理","replyTimes":11,"userAge":22,"userName":"李林","source":0,"reporter":0,"status":1,"content":""},{"topTime":1467367368000,"recommend":0,"url":"http://www.yulin520.com/a2a/h5/impress/i1/218?a=1511162126397","createTime":1460446369000,"hxKey":"406e5f1f020f65203d200b686cd11da3","img":"http://img1.yulin520.com/news/FNTQDAWAJKF0OGYIUMRO.jpg#800_600","yulin":"76330","star":53,"title":"唐政:眉清目秀好少年","occupation":"互联网","userImg":"http://img1.yulin520.com/news/FNTQDAWAJKF0OGYIUMRO.jpg#800_600","impressEditId":218,"impressType":0,"remark":"","click":0,"introduction":"91年 天蝎座/积极乐观/追求新奇事物/互联网销售经理","replyTimes":16,"userAge":25,"userName":"Truman✨","source":0,"reporter":0,"status":1,"content":""}] * message : null */ private int code; private int width; private boolean success; private int height; private Object message; private List<DataBean> data; public int getCode() { return code; } public void setCode(int code) { this.code = code; } public int getWidth() { return width; } public void setWidth(int width) { this.width = width; } public boolean isSuccess() { return success; } public void setSuccess(boolean success) { this.success = success; } public int getHeight() { return height; } public void setHeight(int height) { this.height = height; } public Object getMessage() { return message; } public void setMessage(Object message) { this.message = message; } public List<DataBean> getData() { return data; } public void setData(List<DataBean> data) { this.data = data; } public static class DataBean { /** * topTime : 1469503026000 * recommend : 0 * url : http://www.yulin520.com/a2a/h5/impress/i1/334?a=1511162126397 * createTime : 1466160199000 * hxKey : 60fd9c2377f81ba1ccca450fb794e016 * img : http://img1.yulin520.com/news/HPUCVP74IFR0OKCZ9QQH.jpg#405_304 * yulin : 81373 * star : 58 * title : 刘峰鸣 :能文能武,Call Me峰鸣哥 * occupation : 证劵投资 * userImg : http://img1.yulin520.com/news/HPUCVP74IFR0OKCZ9QQH.jpg#405_304 * impressEditId : 334 * impressType : 0 * remark : * click : 0 * introduction : 83年/巨蟹座/ 开朗幽默,热情善良,追求刺激/喜欢温柔,体贴,可爱,善良的女孩/金融 * replyTimes : 17 * userAge : 33 * userName : 刘LLL * source : 0 * reporter : 0 * status : 1 * content : */ private long topTime; private int recommend; private String url; private long createTime; private String hxKey; private String img; private String yulin; private int star; private String title; private String occupation; private String userImg; private int impressEditId; private int impressType; private String remark; private int click; private String introduction; private int replyTimes; private int userAge; private String userName; private int source; private int reporter; private int status; private String content; public long getTopTime() { return topTime; } public void setTopTime(long topTime) { this.topTime = topTime; } public int getRecommend() { return recommend; } public void setRecommend(int recommend) { this.recommend = recommend; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public long getCreateTime() { return createTime; } public void setCreateTime(long createTime) { this.createTime = createTime; } public String getHxKey() { return hxKey; } public void setHxKey(String hxKey) { this.hxKey = hxKey; } public String getImg() { return img; } public void setImg(String img) { this.img = img; } public String getYulin() { return yulin; } public void setYulin(String yulin) { this.yulin = yulin; } public int getStar() { return star; } public void setStar(int star) { this.star = star; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getOccupation() { return occupation; } public void setOccupation(String occupation) { this.occupation = occupation; } public String getUserImg() { return userImg; } public void setUserImg(String userImg) { this.userImg = userImg; } public int getImpressEditId() { return impressEditId; } public void setImpressEditId(int impressEditId) { this.impressEditId = impressEditId; } public int getImpressType() { return impressType; } public void setImpressType(int impressType) { this.impressType = impressType; } public String getRemark() { return remark; } public void setRemark(String remark) { this.remark = remark; } public int getClick() { return click; } public void setClick(int click) { this.click = click; } public String getIntroduction() { return introduction; } public void setIntroduction(String introduction) { this.introduction = introduction; } public int getReplyTimes() { return replyTimes; } public void setReplyTimes(int replyTimes) { this.replyTimes = replyTimes; } public int getUserAge() { return userAge; } public void setUserAge(int userAge) { this.userAge = userAge; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public int getSource() { return source; } public void setSource(int source) { this.source = source; } public int getReporter() { return reporter; } public void setReporter(int reporter) { this.reporter = reporter; } public int getStatus() { return status; } public void setStatus(int status) { this.status = status; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } }}
DataBean
package com.example.zonghe.bean;import java.util.List;/** * Created by Administrator on 2017/11/20. */public class DataBean { /** * code : 200 * msg : success * newslist : [{"ctime":"2017-06-06 15:00","title":"真是很少见!日本一家银行只存姑娘不存钱","description":"明星花边","picUrl":"http://www.luanbian.com/d/file/lishi/yiyu/2017-06-05/small8681bec69f9c1d6a43ef920082d04bf21496647674.jpg","url":"http://www.luanbian.com//lishi/yiyu/25329.html"},{"ctime":"2017-06-06 14:08","title":"一曲成名红过刀郎,花200元赚得一亿利润!","description":"明星花边","picUrl":"http://www.luanbian.com/d/file/lishi/yiyu/2017-06-05/c7b6e163ab0cd2e73d73fa22d269cceb.jpg","url":"http://www.luanbian.com//lishi/yiyu/25331.html"},{"ctime":"2017-06-06 15:00","title":"香港四大天王谁居首?年轻时张学友颜值最低,老了却成功逆袭","description":"明星花边","picUrl":"http://www.luanbian.com/d/file/lishi/yiyu/2017-06-05/5da421cae36b825d7c19e00522c78654.jpg","url":"http://www.luanbian.com//lishi/yiyu/25332.html"},{"ctime":"2017-06-06 14:14","title":"安以轩嫁入千亿豪门,大S微博发酸这样说","description":"明星花边","picUrl":"http://www.luanbian.com/d/file/lishi/yiyu/2017-06-06/smalle2a960a265d59f8786c9e2f713266dc91496717348.jpg","url":"http://www.luanbian.com//lishi/yiyu/25333.html"},{"ctime":"2017-06-06 15:00","title":"\u201c国际明星\u201d英达父亲去世,不让亲儿子巴图参加","description":"明星花边","picUrl":"http://www.luanbian.com/d/file/lishi/yiyu/2017-06-06/smalla64e8251141b3ca2ac158247952993721496719684.jpg","url":"http://www.luanbian.com//lishi/yiyu/25334.html"},{"ctime":"2017-06-06 14:39","title":"村中寻一座明代古石桥,竟被告知在墙上?","description":"明星花边","picUrl":"http://www.luanbian.com/d/file/lishi/yiyu/2017-06-06/small0ba83f51849d3c9fbd73cc99fa2017a11496721103.jpg","url":"http://www.luanbian.com//lishi/yiyu/25335.html"},{"ctime":"2017-06-06 15:00","title":"流浪狗叼着刚出生的弃婴向路人求救,感动落泪","description":"明星花边","picUrl":"http://www.luanbian.com/d/file/lishi/yiyu/2017-06-06/small7c1f47cd2a87c73bcf9ab9352e5d25fd1496729645.jpg","url":"http://www.luanbian.com//lishi/yiyu/25336.html"},{"ctime":"2017-06-06 14:41","title":"入赘豪门做女婿的8大男星,刘德华周润发均在列","description":"明星花边","picUrl":"http://www.luanbian.com/d/file/lishi/yiyu/2017-06-02/fba8bd9a885bc13cfcfc3454f3d0889f.jpg","url":"http://www.luanbian.com//lishi/yiyu/25319.html"},{"ctime":"2017-06-06 15:00","title":"破洞牛仔裤隐藏巨大的危险,工人付出惨痛代价","description":"明星花边","picUrl":"http://www.luanbian.com/d/file/lishi/yiyu/2017-06-05/d85c3437f009bee056a06a8a7bcd0fb9.jpg","url":"http://www.luanbian.com//lishi/yiyu/25328.html"},{"ctime":"2017-06-06 15:00","title":"一条鱼抵一辆保时捷 标价268万元惊呆了网友","description":"明星花边","picUrl":"http://www.luanbian.com/d/file/lishi/yiyu/2017-06-05/small535838c27374b6be43a7ec865d2974281496650726.jpg","url":"http://www.luanbian.com//lishi/yiyu/25330.html"}] */ private int code; private String msg; private List<NewslistBean> newslist; public int getCode() { return code; } public void setCode(int code) { this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public List<NewslistBean> getNewslist() { return newslist; } public void setNewslist(List<NewslistBean> newslist) { this.newslist = newslist; } public static class NewslistBean { /** * ctime : 2017-06-06 15:00 * title : 真是很少见!日本一家银行只存姑娘不存钱 * description : 明星花边 * picUrl : http://www.luanbian.com/d/file/lishi/yiyu/2017-06-05/small8681bec69f9c1d6a43ef920082d04bf21496647674.jpg * url : http://www.luanbian.com//lishi/yiyu/25329.html */ private String ctime; private String title; private String description; private String picUrl; private String url; public String getCtime() { return ctime; } public void setCtime(String ctime) { this.ctime = ctime; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public String getPicUrl() { return picUrl; } public void setPicUrl(String picUrl) { this.picUrl = picUrl; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } }}
工具类
Utils
package com.example.zonghe.utils;import android.app.AlertDialog;import android.content.Context;import android.content.DialogInterface;import android.content.Intent;import android.graphics.Bitmap;import android.net.ConnectivityManager;import android.net.NetworkInfo;import android.os.AsyncTask;import com.example.zonghe.R;import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;import com.nostra13.universalimageloader.core.DisplayImageOptions;import com.nostra13.universalimageloader.core.ImageLoader;import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;import com.nostra13.universalimageloader.core.assist.ImageScaleType;import com.nostra13.universalimageloader.core.assist.QueueProcessingType;import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;import com.nostra13.universalimageloader.utils.StorageUtils;import java.io.BufferedReader;import java.io.File;import java.io.InputStream;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.URL;/** * 封装类 * * Created by Administrator on 2017/11/20. */public class Utils { /** * 网上获取数据的方法 * @param path URL网址 * @param context 上下文 * @param shuju 接口回调 */ public static void getData(final String path, Context context, final ShuJu shuju){ AsyncTask<Void, Void, String> asyncTask = new AsyncTask<Void, Void, String>() { @Override protected String doInBackground(Void... voids) { 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 null; } @Override protected void onPostExecute(String s) { shuju.getData(s); } }; //执行 asyncTask.execute(); } /** * 字节流转换为字符流的方法 * @param inputStream 要转换的字节流 * @param charset 编码格式 * @return */ private static 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; } /** * 图片异步加载的方法 --> ImageLoader * 需要设置依赖 nostra * @param context 上下文 */ public static void init(Context context){ File cacheDir = StorageUtils.getCacheDirectory(context); //指定默认的硬盘(sd卡)的缓存文件夹路径 //1.创建一个ImageLoaderConfiguration,,,图片加载器的配置对象 ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context) .threadPoolSize(3) // default 线程池内加载的数量 .threadPriority(Thread.NORM_PRIORITY - 2) // default 设置当前线程的优先级 .tasksProcessingOrder(QueueProcessingType.FIFO) // default .denyCacheImageMultipleSizesInMemory() //1 TB = 1024GB 1G=1024MB 1M = 1024KB 1KB = 1024BYTE //LruMemoryCache,,lru最近最少使用算法...内部维护的是LinkedHashMap,,当一张图片最近很少使用的时候会从mao集合里面移除 .memoryCache(new LruMemoryCache(2 * 1024 * 1024)) //指定内存缓存的大小,,2M,,可以通过自己的内存缓存实现 .memoryCacheSize(2 * 1024 * 1024) // 内存缓存的最大值 .memoryCacheSizePercentage(13) // default //指定硬盘/磁盘缓存的路径 // .diskCache(new UnlimitedDiskCache(cacheDir)) // default 可以自定义缓存路径 .diskCacheSize(50 * 1024 * 1024) // 50 Mb sd卡(本地)缓存的最大值 .diskCacheFileCount(100) // 可以缓存的文件数量 .defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // default .writeDebugLogs() // 打印debug log .build(); //开始构建 //2.对上面配置对象的初始化 ImageLoader.getInstance().init(config); } /** * 适配器获得图片的方法 * @return */ public static DisplayImageOptions getTuXing(){ DisplayImageOptions options = new DisplayImageOptions.Builder() .showImageOnLoading(R.mipmap.ic_launcher) // 设置图片下载期间显示的图片 .showImageForEmptyUri(R.mipmap.ic_launcher) // 设置图片Uri为空或是错误的时候显示的图片 .showImageOnFail(R.mipmap.ic_launcher) // 设置图片加载或解码过程中发生错误显示的图片 .resetViewBeforeLoading(true) // default 设置图片在加载前是否重置、复位 .cacheInMemory(true) // default 设置下载的图片是否缓存在内存中 .cacheOnDisk(true) // default 设置下载的图片是否缓存在SD卡中 .considerExifParams(true) // default .imageScaleType(ImageScaleType.EXACTLY_STRETCHED) // default 设置图片以如何的编码方式显示 .bitmapConfig(Bitmap.Config.RGB_565) // default 设置图片的解码类型 .displayer(new SimpleBitmapDisplayer()) // default 还可以设置圆角图片new RoundedBitmapDisplayer(20),,圆形 new CircleBitmapDisplayer() .build(); return options; } /** * 判断网络连接是否已开 * true 已打开 false 未打开 */ public static boolean isConn(Context context){ boolean bisConnFlag=false; ConnectivityManager conManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo network = conManager.getActiveNetworkInfo(); if(network!=null){ bisConnFlag=conManager.getActiveNetworkInfo().isAvailable(); } return bisConnFlag; } /** * 当判断当前手机没有网络时选择是否打开网络设置 * @param context */ public static void showNoNetWorkDlg(final Context context) { AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setIcon(R.mipmap.ic_launcher) // .setTitle(R.string.app_name) // .setMessage("当前无网络").setPositiveButton("设置", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 跳转到系统的网络设置界面 Intent intent = null; // 先判断当前系统版本 if(android.os.Build.VERSION.SDK_INT > 10){ // 3.0以上 intent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS); }else{ intent = new Intent(); intent.setClassName("com.android.settings", "com.android.settings.WirelessSettings"); } context.startActivity(intent); } }).setNegativeButton("知道了", null).show(); }}
ShuJu
package com.example.zonghe.utils;/** * Created by Administrator on 2017/11/20. */public interface ShuJu { public void getData(String s);}
Mybanner
package com.example.zonghe.utils;import android.content.Context;import android.widget.ImageView;import com.youth.banner.loader.ImageLoader;/** * Created by Administrator on 2017/11/20. */public class Mybanner extends ImageLoader{ @Override public void displayImage(Context context, Object path, ImageView imageView) { com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(path.toString(),imageView); }}
Mygridview
package com.example.zonghe;import android.content.Context;import android.util.AttributeSet;import android.widget.GridView;/** * Created by Administrator on 2017/11/20. */public class Mygridview extends GridView { public Mygridview(Context context) { super(context); } public Mygridview(Context context, AttributeSet attrs) { super(context, attrs); } public Mygridview(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int i = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, i); }}
Myapp
package com.example.zonghe;import android.app.Application;import com.example.zonghe.utils.Utils;/** * Created by Administrator on 2017/11/20. */public class Myapp extends Application { @Override public void onCreate() { super.onCreate(); Utils.init(this); }}
阅读全文
0 0
- RadioGroup+Fragment+PulltorefreshScrollView+GridView+侧拉菜单+TabLayout+ViewPager
- ViewPager,RadioGroup,FragmentManager 详解 <四> Fragment,ViewPager 和TabLayout
- 横向菜单滑动选择viewpager+tablayout+fragment
- listview多条目 GridView TabLayout pullToRefresh radioGroup +fragment
- fragment+viewpager+侧拉
- ViewPager+Fragment+侧拉
- Android软件架构之RadioGroup + Fragment , TabLayout + ViewPager +Fragment 覆盖市场大部分商业软件架构
- RadioGroup+ViewPager+Fragment
- radiogroup+viewpager+fragment联动,
- 网络判断+Fragment+ViewPager+XListView+TabLayout横向滑动菜单 Z2
- TabLayout+Fragment+ViewPager顶部菜单可滑动切换
- TabLayout、ViewPager、fragment实现可滑动的顶部菜单
- 侧滑菜单栏+viewpager+tablayout+fragment
- ViewPager + Fragment + TabLayout
- TabLayout 实现ViewPager+fragment
- TabLayout ViewPager 复用Fragment
- Tablayout+ViewPager+Fragment(Android)
- ViewPager+Fragment+TabLayout
- /bin/sh: /tmp/xxx.sh: Permission denied
- 键盘遥控小车,代码复用
- C语言小练习7
- 谷歌搞事情,Fuchsia OS 操作系统运行苹果 Swift 代码
- Smarty使用section语句循环输出数组
- RadioGroup+Fragment+PulltorefreshScrollView+GridView+侧拉菜单+TabLayout+ViewPager
- Myeclipse导出war包出错
- Angular环境搭建
- Smart网站公告
- C++头文件<climits>使用相应数据类型的极限值
- oracle 索引失效原因及解决方法
- ThinkPHP自动生成目录
- ThinkPHP项目构建流程
- JDK(二)java源码分析之ArrayList