**
MainActivity
**
package com.example.lx_zz.activity;
import android.annotation.SuppressLint;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.example.lx_zz.R;
import com.example.lx_zz.adapter.BujuAdapter;
public class MainActivity extends AppCompatActivity {
private TabLayout tab;
private ViewPager viewPager;
@SuppressLint("WrongViewCast")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取资源ID
tab = findViewById(R.id.tab);
viewPager = findViewById(R.id.viewpager);
//设置适配器
BujuAdapter bujuAdapter = new BujuAdapter(getSupportFragmentManager());
viewPager.setAdapter(bujuAdapter);
//根据viewpager设置tab
tab.setupWithViewPager(viewPager);
}
}
**
导航页的Adapter
**
package com.example.lx_zz.adapter;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import com.example.lx_zz.fragment.Fragmentone;
import com.example.lx_zz.fragment.Fragmentthree;
import com.example.lx_zz.fragment.Fragmenttwo;
import java.util.List;
public class BujuAdapter extends FragmentPagerAdapter {
//创建体格string数组
public String[] menus = new String[]{
"金融","传媒","数码","轻奢","服装","汽车"
};
public BujuAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
switch (i){
case 0:
return new Fragmentone();
case 1:
return new Fragmenttwo();
default:
return new Fragmentthree();
}
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return menus[position];
}
@Override
public int getCount() {
return menus.length;
}
}
**
Fragmentone页面
**
package com.example.lx_zz.fragment;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;
import com.example.lx_zz.R;
import com.example.lx_zz.adapter.LunAdapter;
import com.example.lx_zz.adapter.MyBase;
import com.example.lx_zz.sql.Dao;
import com.example.lx_zz.util.Bean;
import com.example.lx_zz.util.NetUtil;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import java.util.ArrayList;
import java.util.List;
public class Fragmentone extends Fragment {
private PullToRefreshListView pull;
private ViewPager viewPager;
private int page = 1;
private final int LOAD_COUNT = 10;
private String urlstr = "http://api.expoon.com/AppNews/getNewsList/type/1/p/";
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
viewPager.setCurrentItem(viewPager.getCurrentItem()+1);
handler.sendEmptyMessageDelayed(0,2000);
}
};
private MyBase myBase;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragmentone,container,false);
return view;
}
private List<Integer> list;
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//获取资源ID
pull = view.findViewById(R.id.pull);
viewPager = view.findViewById(R.id.pager);
//实例化
list = new ArrayList<>();
list.add(R.drawable.bbbb);
list.add(R.drawable.cccc);
list.add(R.drawable.dddd);
//给viewpager设置适配器
LunAdapter lunAdapter = new LunAdapter(getActivity());
lunAdapter.setList(list);
viewPager.setAdapter(lunAdapter);
//分割图片
viewPager.setCurrentItem(lunAdapter.getCount()/2);
//向handler发送消息
handler.sendEmptyMessageDelayed(0,2000);
//给pull设置适配器
myBase = new MyBase(getActivity());
pull.setAdapter(myBase);
//设置允许
pull.setMode(PullToRefreshListView.Mode.BOTH);
//设置监听
pull.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
page = 1;
getdata();
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
getdata();
}
});
getdata();
}
private void getdata(){
NetUtil.yibu(urlstr + page, Bean.class, new NetUtil.CallBack<Bean>() {
@Override
public void getdata(Bean bean) {
if (!NetUtil.panduan(getActivity()) || bean == null || !bean.ispanduan()){
Toast.makeText(getActivity(),"数据请求错误", Toast.LENGTH_SHORT).show();
pull.onRefreshComplete();
return;
}
if (page == 1){
Dao.getInstance(getActivity()).del();
}
Dao.getInstance(getActivity()).addAll(bean.getData());
if (page == 1){
myBase.setList(bean.getData());
}else {
myBase.addList(bean.getData());
}
page++;
//停止刷新
pull.onRefreshComplete();
if (bean.getData().size() < LOAD_COUNT){
pull.setMode(PullToRefreshListView.Mode.PULL_FROM_START);
}else {
pull.setMode(PullToRefreshListView.Mode.BOTH);
}
}
});
}
}
轮播图的Adapter
package com.example.lx_zz.adapter;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import java.util.ArrayList;
import java.util.List;
public class LunAdapter extends PagerAdapter {
private List<Integer> list;
private Context context;
public LunAdapter(Context context) {
this.context = context;
list = new ArrayList<>();
}
public void setList(List<Integer> slist) {
if (slist != null){
list.addAll(slist);
}
notifyDataSetChanged();
}
@Override
public int getCount() {
return 5000;
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view == object;
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
//创建imageview
ImageView imageView = new ImageView(context);
//设置延伸
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
//设置图片
imageView.setImageResource(list.get(position%list.size()));
container.addView(imageView);
return imageView;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((View) object);
}
}
**
上拉下拉刷新的Adapter
**
package com.example.lx_zz.adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.lx_zz.R;
import com.example.lx_zz.util.Bean;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.ArrayList;
import java.util.List;
public class MyBase extends BaseAdapter {
private List<Bean.Data> list;
private Context context;
public MyBase(Context context) {
this.context = context;
list = new ArrayList<>();
}
public void setList(List<Bean.Data> slist) {
list.clear();
if (slist != null){
list.addAll(slist);
}
notifyDataSetChanged();
}
public void addList(List<Bean.Data> slist){
if (slist != null){
list.addAll(slist);
}
notifyDataSetChanged();
}
@Override
public int getCount() {
return list.size();
}
@Override
public Bean.Data getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null){
convertView = LayoutInflater.from(context).inflate(R.layout.item,parent,false);
viewHolder = new ViewHolder();
viewHolder.imageView = convertView.findViewById(R.id.image);
viewHolder.title = convertView.findViewById(R.id.title);
viewHolder.summary = convertView.findViewById(R.id.summary);
convertView.setTag(viewHolder);
}else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.title.setText(getItem(position).getNews_title());
viewHolder.summary.setText(getItem(position).getNews_summary());
ImageLoader.getInstance().displayImage(getItem(position).getPic_url(), viewHolder.imageView);
return convertView;
}
class ViewHolder{
private ImageView imageView;
private TextView title,summary;
}
}
**
NetUtil页面
**
package com.example.lx_zz.util;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import com.google.gson.Gson;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class NetUtil {
//网络请求
public static boolean panduan(Context context){
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(context.CONNECTIVITY_SERVICE);
//获取网络状态权限
NetworkInfo activeNetworkInfo = cm.getActiveNetworkInfo();
return activeNetworkInfo!=null&&activeNetworkInfo.isAvailable();
}
//接口回调
public static interface CallBack<T>{
void getdata(T t);
}
//异步请求
public static void yibu(final String urlstr, final Class clazz, final CallBack callBack){
new AsyncTask<String, Void, Object>() {
@Override
protected Object doInBackground(String... strings) {
return jiexi(urlstr,clazz);
}
@Override
protected void onPostExecute(Object o) {
callBack.getdata(o);
}
}.execute(urlstr);
}
//网络解析
public static <T> T jiexi(String urlstr, Class clazz){
T t = (T) new Gson().fromJson(geturl(urlstr),clazz);
return t;
}
//网络请求
public static String geturl(String urlstr){
String result = "";
try {
URL url = new URL(urlstr);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setReadTimeout(5000);
urlConnection.setConnectTimeout(5000);
int responseCode = urlConnection.getResponseCode();
if (responseCode == 200){
result = stream(urlConnection.getInputStream());
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
//字符流转为字符串
public static String stream(InputStream inputStream) throws IOException {
StringBuilder stringBuilder = new StringBuilder();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
for (String tmp = bufferedReader.readLine(); tmp != null; tmp = bufferedReader.readLine()){
stringBuilder.append(tmp);
}
return stringBuilder.toString();
}
}
**
App页面
**
package com.example.lx_zz.util;
import android.app.Application;
import com.example.lx_zz.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
//全局配置
DisplayImageOptions options = new DisplayImageOptions.Builder()
.cacheOnDisk(true)
.cacheInMemory(true)
.showImageOnFail(R.mipmap.ic_launcher)
.showImageForEmptyUri(R.mipmap.ic_launcher)
.build();
ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(this)
.defaultDisplayImageOptions(options)
.memoryCacheSizePercentage(10)
.diskCacheSize(20*1024*1024)
.build();
//初始化
ImageLoader.getInstance().init(configuration);
}
}
**
Fragmenttwo页面
**
package com.example.lx_zz.fragment;
import android.os.Bundle;
import android.support.annotation.NonNull;
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.TextView;
import com.example.lx_zz.R;
import com.example.lx_zz.util.Bean;
import java.util.ArrayList;
import java.util.List;
public class Fragmenttwo extends Fragment {
private TextView text;
private String id;
private String title;
private String summary;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragmenttwo,container,false);
return view;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
text = view.findViewById(R.id.cc);
List<Bean.Data> list = new ArrayList<>();
for (int i = 0; i<list.size(); i++){
id = list.get(i).getNews_id();
title = list.get(i).getNews_title();
summary = list.get(i).getNews_summary();
}
text.setText(id+title+summary+list.size());
}
}
**
Fragmenttwo布局
**
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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">
<TextView
android:id="@+id/cc"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
/>
</android.support.constraint.ConstraintLayout>
**
Bean文件
**
package com.example.lx_zz.util;
import java.util.List;
public class Bean {
private int status;
private final int PAN = 1;
private List<Data> data;
public List<Data> getData() {
return data;
}
public int getStatus() {
return status;
}
public boolean ispanduan(){
return PAN == status;
}
public static class Data{
private String news_id;
private String news_summary;
private String news_title;
private String pic_url;
public Data(String news_id, String news_summary, String news_title) {
this.news_id = news_id;
this.news_summary = news_summary;
this.news_title = news_title;
}
public String getNews_id() {
return news_id;
}
public String getNews_summary() {
return news_summary;
}
public String getNews_title() {
return news_title;
}
public String getPic_url() {
return pic_url;
}
}
}
**
Androidmanifest文件
**
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.day18">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:name=".util.App"
android:theme="@style/AppTheme">
<activity android:name=".activity.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>