主界面:
public class MainActivity extends AppCompatActivity { private TabLayout tabLayout; private ViewPager viewPager; private ImageView login; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tabLayout = (TabLayout) findViewById(R.id.layout); viewPager = (ViewPager) findViewById(R.id.viewpager); viewPager.setOffscreenPageLimit(7); login = (ImageView) findViewById(R.id.login); login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { UMShareAPI.get(MainActivity.this).getPlatformInfo(MainActivity.this, SHARE_MEDIA.QQ, umAuthListener); } }); viewPager.setAdapter(new MyAdapter(getSupportFragmentManager())); tabLayout.setupWithViewPager(viewPager); } private UMAuthListener umAuthListener = new UMAuthListener() { @Override public void onStart(SHARE_MEDIA platform) { //授权开始的回调 } @Override public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) { Toast.makeText(getApplicationContext(), "Authorize succeed", Toast.LENGTH_SHORT).show(); ImageView login = (ImageView) findViewById(R.id.login); TextView text = (TextView) findViewById(R.id.name); DisplayImageOptions options = new DisplayImageOptions.Builder() .cacheInMemory(true) .cacheOnDisk(true) .displayer(new CircleBitmapDisplayer()) .build(); String name = data.get("name"); String tu = data.get("iconurl"); text.setText(name); ImageLoader.getInstance().displayImage(tu,login,options); } @Override public void onError(SHARE_MEDIA platform, int action, Throwable t) { Toast.makeText( getApplicationContext(), "Authorize fail", Toast.LENGTH_SHORT).show(); } @Override public void onCancel(SHARE_MEDIA platform, int action) { Toast.makeText( getApplicationContext(), "Authorize cancel", Toast.LENGTH_SHORT).show(); } }; @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data); } }MyAdapter类:
public class MyAdapter extends FragmentPagerAdapter { private String[] title = {"推荐","热点","北京","视频","热点","北京","视频"}; public MyAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { MyFragment myFragment = new MyFragment(); Bundle bundle = new Bundle(); bundle.putString("text",title[position]); myFragment.setArguments(bundle); return myFragment; } @Override public int getCount() { return title != null?title.length:0; } @Override public CharSequence getPageTitle(int position) { return title[position]; } }
MyApplication类:
public class
MyApplication
extends
Application
{
private static
MyApplication
myApplication
;
{
PlatformConfig
.
setQQZone
(
"100424468"
,
"c7394704798a158208a74ab60104f0ba"
)
;
Config
.
DEBUG
=
true
;
}
@Override
public void
onCreate
() {
super
.
onCreate
()
;
x.Ext
.
init
(
this
)
;
initImageLoader
(
getApplicationContext
())
;
UMShareAPI
.
get
(
this
)
;
}
public static void
initImageLoader
(
Context
context
) {
File
cacheDir
=
StorageUtils
.
getOwnCacheDirectory
(
context
,
"topnews/Cache"
)
;
//
获取到缓存的目录地址
Log
.
d
(
"cacheDir"
,
cacheDir
.
getPath
())
;
//
创建配置
ImageLoader(
所有的选项都是可选的
,
只使用那些你真的想定制
)
,这个可以设定在
APPLACATION
里面,设置为全局的配置参数
ImageLoaderConfiguration
config
=
new
ImageLoaderConfiguration
.Builder
(
context
)
.
threadPoolSize
(
3
)
//
线程池内加载的数量
.
denyCacheImageMultipleSizesInMemory
()
//.memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 1024)) // You can pass your own memory cache implementation
你可以通过自己的内存缓存实现
.
memoryCacheSize
(
2
*
1024
*
1024
)
.
diskCacheSize
(
50
*
1024
*
1024
)
.
diskCacheFileNameGenerator
(
new
Md5FileNameGenerator
())
//
将保存的时候的
URI
名称用
MD5
加密
.
tasksProcessingOrder
(
QueueProcessingType
.
LIFO
)
.
diskCache
(
new
UnlimitedDiskCache
(
cacheDir
))
//
自定义缓存路径
//.imageDownloader(new BaseImageDownloader(context, 5 * 1000, 30 * 1000)) // connectTimeout (5 s), readTimeout (30 s)
超时时间
.
build
()
;
ImageLoader
.
getInstance
()
.
init
(
config
)
;
//
全局初始化此配置
}
} MyFragment:
public class MyFragment extends Fragment implements XListView.IXListViewListener { private String text; private MAdapter adapter; private boolean flag; private int index = 1; private XListView xlistview; @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); View view = getView(); xlistview = (XListView) view.findViewById(R.id.xlistview); xlistview.setPullLoadEnable(true); xlistview.setXListViewListener(this); qingqiu(); } @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.myfragment,container,false); } @Override public void onRefresh() { ++index; qingqiu(); flag=true; xlistview.stopRefresh(true); } @Override public void onLoadMore() { ++index; qingqiu(); flag=false; xlistview.stopLoadMore(); } class MAdapter extends BaseAdapter{ ImageOptions options = new ImageOptions.Builder() .setLoadingDrawableId(R.mipmap.ic_launcher) .setUseMemCache(true) .setSize(200,200) .setCircular(true) .build(); private List<Bean.ResultBean.DataBean> data; public MAdapter(List<Bean.ResultBean.DataBean> data){ this.data=data; } public void more(List<Bean.ResultBean.DataBean> datas,boolean flag){ for (Bean.ResultBean.DataBean bean:datas) { if(flag){ data.add(0,bean); }else { data.add(bean); } } } @Override public int getCount() { return data != null?data.size():0; } @Override public Object getItem(int position) { return data.get(position); } @Override public long getItemId(int position) { return position; } @Override public int getItemViewType(int position) { if(position % 2 == 0){ return 0; }else { return 1; } } @Override public int getViewTypeCount() { return 2; } @Override public View getView(int position, View convertView, ViewGroup parent) { int type = getItemViewType(position); ViewHolder1 holder1 = null; ViewHolder2 holder2 = null; switch (type){ case 0:{ if(convertView == null){ convertView = View.inflate(getContext(),R.layout.left,null); holder1 = new ViewHolder1(); holder1.wen1 = (TextView) convertView.findViewById(R.id.wen1); holder1.tu1 = (ImageView) convertView.findViewById(R.id.tu1); convertView.setTag(holder1); }else { holder1 = (ViewHolder1) convertView.getTag(); } holder1.wen1.setText(data.get(position).getTitle()); x.image().bind(holder1.tu1,data.get(position).getAlbums().get(0),options); } break; case 1:{ if(convertView == null){ convertView = View.inflate(getContext(),R.layout.right,null); holder2 = new ViewHolder2(); holder2.wen2 = (TextView) convertView.findViewById(R.id.wen2); holder2.tu2 = (ImageView) convertView.findViewById(R.id.tu2); convertView.setTag(holder2); }else { holder2 = (ViewHolder2) convertView.getTag(); } holder2.wen2.setText(data.get(position).getTitle()); x.image().bind(holder2.tu2,data.get(position).getAlbums().get(0),options); } break; } return convertView; } } private void qingqiu(){ String url = "http://apis.juhe.cn/cook/query.php"; RequestParams params = new RequestParams(url); params.addBodyParameter("key","1c4789006d667db3a36f20f7cbd6aa02"); params.addBodyParameter("pn",index+""); params.addBodyParameter("rn","10"); params.addBodyParameter("menu","西红柿鸡蛋"); x.http().post(params, new Callback.CommonCallback<String>() { @Override public void onSuccess(String result) { Gson gson = new Gson(); Bean bean = gson.fromJson(result, Bean.class); List<Bean.ResultBean.DataBean> data = bean.getResult().getData(); if(adapter == null){ adapter = new MAdapter(data); xlistview.setAdapter(adapter); }else { adapter.more(data,flag); adapter.notifyDataSetChanged(); } } @Override public void onError(Throwable ex, boolean isOnCallback) { } @Override public void onCancelled(CancelledException cex) { } @Override public void onFinished() { } }); } class ViewHolder1{ TextView wen1; ImageView tu1; } class ViewHolder2{ TextView wen2; ImageView tu2; } }TabLayout布局文件:
<android.support.design.widget.TabLayout android:id="@+id/layout" android:layout_width="match_parent" android:layout_height="50dp" app:tabMode="scrollable" app:tabIndicatorHeight="1dp" app:tabIndicatorColor="#000000" app:tabTextAppearance="@style/title" app:tabTextColor="#0000cc" app:tabSelectedTextColor="#ffff00"> android.support.design.widget.TabLayout>