MainActivity
package com.bwie.zhaojunhui20181119;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
private ViewPager viewPager;
private TabLayout tabLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化View
viewPager = findViewById(R.id.pager);
viewPager.setAdapter(new MainPageAdapter(getSupportFragmentManager()));
tabLayout = findViewById(R.id.tabLayout);
tabLayout.setupWithViewPager(viewPager);
}
}
MainPageAdapter适配器
package com.bwie.zhaojunhui20181119;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
public class MainPageAdapter extends FragmentPagerAdapter {
private String[] menus = new String[]{"推荐", "热门", "科技"};
public MainPageAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
switch (i){
case 0:
return new AFragment();
case 1:
return new BFragment();
default:
return new CFragment();
}
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return menus[position];
}
@Override
public int getCount() {
return menus.length;
}
}
FirstFragment
package com.bwie.zhaojunhui20181119;
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.ListView;
import android.widget.Toast;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import java.util.ArrayList;
import java.util.List;
public class AFragment extends Fragment {
private PullToRefreshListView listView;
private ListBaseAdapter mAdapter;
private int mPage;
private String urlStr = "http://api.expoon.com/AppNews/getNewsList/type/1/p/";
private String Banner = "http://www.zhaoapi.cn/quarter/getAd";
private int COUNT = 10;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment01, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
listView = view.findViewById(R.id.pull);
//创建适配器
mAdapter = new ListBaseAdapter(getActivity());
listView.setAdapter(mAdapter);
mPage = 1;
//支持加载,刷新
listView.setMode(PullToRefreshBase.Mode.BOTH);
listView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
mPage = 1;
initData();
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
initData();
}
});
initData();
}
private void initData() {
if (mPage == 1){
BannerData();
NewsData();
}else {
NewsData();
}
}
private void BannerData() {
NetUtil.getInstance().getRequest(Banner, BannerBean.class, new NetUtil.CallBack<BannerBean>() {
@Override
public void OnSuccess(BannerBean bannerBean) {
mAdapter.setBanner(bannerBean.getData());
}
});
}
private void NewsData() {
NetUtil.getInstance().getRequest(urlStr + mPage, NewsBean.class, new NetUtil.CallBack<NewsBean>() {
@Override
public void OnSuccess(NewsBean newsBean) {
if (newsBean == null || !newsBean.isSuccess()){
Toast.makeText(getActivity(), "请求错误", Toast.LENGTH_SHORT).show();
listView.onRefreshComplete();
return;
}
//数据库操作
if (mPage == 1) {
Dao.getInstance(getActivity()).delAll();
}
Dao.getInstance(getActivity()).addAll(newsBean.getData());
//请求成功加载数据
if (mPage == 1){
mAdapter.setDatas(newsBean.getData());
}else {
mAdapter.addDatas(newsBean.getData());
}
mPage ++;
listView.onRefreshComplete();
if (newsBean.getData().size() < COUNT){
listView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
}else {
listView.setMode(PullToRefreshBase.Mode.BOTH);
}
}
});
}
}
FirstBaseAdapter适配器
package com.bwie.zhaojunhui20181119;
import android.content.Context;
import android.provider.ContactsContract;
import android.support.v7.widget.RecyclerView;
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.nostra13.universalimageloader.core.ImageLoader;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.loader.ImageLoaderInterface;
import java.util.ArrayList;
import java.util.List;
public class ListBaseAdapter extends BaseAdapter {
private List<NewsBean.DataBean> mData;
private List<BannerBean.DataBean> mBanner;
private Context mContext;
public ListBaseAdapter(Context mContext) {
this.mContext = mContext;
mData = new ArrayList<>();
mBanner = new ArrayList<>();
}
public void setBanner(List<BannerBean.DataBean> data) {
mBanner.clear();
if (data != null) {
mBanner.addAll(data);
}
notifyDataSetChanged();
}
public void setDatas(List<NewsBean.DataBean> data) {
mData.clear();
if (data != null) {
mData.addAll(data);
}
notifyDataSetChanged();
}
public void addDatas(List<NewsBean.DataBean> data) {
if (data != null) {
mData.addAll(data);
}
notifyDataSetChanged();
}
private boolean hasBanner() {
return mBanner.size() > 0;
}
@Override
public int getCount() {
return hasBanner() ? mData.size() + 1 : mData.size();
}
@Override
public NewsBean.DataBean getItem(int position) {
return mData.get(hasBanner() ? position - 1 : position);
}
@Override
public long getItemId(int position) {
return position;
}
private final int COUNT = 2;
private final int BANNER = 0;
private final int NEWS = 1;
@Override
public int getViewTypeCount() {
return COUNT;
}
@Override
public int getItemViewType(int position) {
return position == 0 && hasBanner() ? BANNER : NEWS;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (getItemViewType(position) == BANNER) {
BannerHolder mHolder;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.banner_item, parent, false);
mHolder = new BannerHolder(convertView);
} else {
mHolder = (BannerHolder) convertView.getTag();
}
mHolder.bindData(mContext, mBanner);
} else {
ViewHolder mHolder;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.news_item, parent, false);
mHolder = new ViewHolder(convertView);
} else {
mHolder = (ViewHolder) convertView.getTag();
}
mHolder.bindData(getItem(position));
}
return convertView;
}
class BannerHolder {
Banner banner;
public BannerHolder(View itemView) {
banner = itemView.findViewById(R.id.banner);
itemView.setTag(this);
}
public void bindData(Context mContext, List<BannerBean.DataBean> mBanner) {
//设置banner样式
banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE_INSIDE);
//设置图图片的加载器
banner.setImageLoader(new ImageLoaderInterface<ImageView>() {
@Override
public void displayImage(Context context, Object path, ImageView imageView) {
BannerBean.DataBean mBanner = (BannerBean.DataBean) path;
ImageLoader.getInstance().displayImage(mBanner.getIconNoHttps(), imageView);
}
@Override
public ImageView createImageView(Context context) {
ImageView imageView = new ImageView(context);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
return imageView;
}
});
initData(mBanner);
}
private void initData(List<BannerBean.DataBean> mBanner) {
banner.setImages(mBanner);
banner.setBannerTitles(getTitles(mBanner));
banner.start();
}
private List<String> getTitles(List<BannerBean.DataBean> mBanner) {
List<String> result = new ArrayList<>();
for (BannerBean.DataBean banner : mBanner){
result.add(banner.getTitle());
}
return result;
}
}
class ViewHolder {
ImageView imageView;
TextView textView1, textView2;
public ViewHolder(View itemView) {
imageView = itemView.findViewById(R.id.image);
textView1 = itemView.findViewById(R.id.textview1);
textView2 = itemView.findViewById(R.id.textview2);
itemView.setTag(this);
}
public void bindData(NewsBean.DataBean item) {
textView1.setText(item.getNews_title());
textView2.setText(item.getNews_summary());
if (imageView != null) {
ImageLoader.getInstance().displayImage(item.getPic_url(), imageView);
}
}
}
}
Dao层
package com.bwie.zhaojunhui20181119;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import java.util.ArrayList;
import java.util.List;
public class Dao {
private static Dao instance;
private SQLiteDatabase database;
private Dao(Context context) {
database = new MyHelper(context).getWritableDatabase();
}
public static Dao getInstance(Context context) {
if (instance == null) {
instance = new Dao(context);
}
return instance;
}
public void add(NewsBean.DataBean bean) {
ContentValues values = new ContentValues();
values.put("news_id", bean.getNews_id());
values.put("news_title", bean.getNews_title());
values.put("news_summary", bean.getNews_summary());
values.put("pic_url", bean.getPic_url());
database.insert("users", null, values);
}
public void addAll(List<NewsBean.DataBean> datas) {
try {
database.beginTransaction();
for (NewsBean.DataBean bean : datas) {
add(bean);
}
database.setTransactionSuccessful();
} finally {
database.endTransaction();
}
}
public void del(String news_id) {
database.delete("users", "news_id=?", new String[]{news_id});
}
public void delAll() {
database.delete("users", null, null);
}
public List<NewsBean.DataBean> query() {
List<NewsBean.DataBean> result = new ArrayList<>();
Cursor cursor = database.query("users", null, null, null, null, null, null);
if(cursor != null) {
while (cursor.moveToNext()) {
String id = cursor.getString(cursor.getColumnIndex("news_id"));
String title = cursor.getString(cursor.getColumnIndex("news_title"));
String summary = cursor.getString(cursor.getColumnIndex("news_summary"));
String url = cursor.getString(cursor.getColumnIndex("pic_url"));
NewsBean.DataBean bean = new NewsBean.DataBean(id, title, summary, url);
result.add(bean);
}
cursor.close();
}
return result;
}
}
activity_main.xml布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabIndicatorHeight="1dp"
app:tabTextColor="@color/colorPrimary"
app:tabSelectedTextColor="@color/colorAccent"
app:tabIndicatorColor="@color/colorPrimaryDark"
/>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/tabLayout"
/>
</RelativeLayout>