MainAcitvity 页面
package com.example.week3demo.activity;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.example.week3demo.R;
import com.example.week3demo.activity.BaseActivity;
import com.example.week3demo.adapter.MyFragmentAdapter;
import com.example.week3demo.fragment.MyFragment;
import com.example.week3demo.fragment.ShouFragment;
import java.util.ArrayList;
import java.util.List;
/**
-
TabLayout,DrawerLayout,ViewPager,Fragment
*/
public class MainActivity extends BaseActivity {
private DrawerLayout drawer_layout;
private ViewPager view_pager;
private TabLayout tab_layout;
private List list = new ArrayList();
private String[] titles = {“首页”,“我的”};
private MyFragmentAdapter adapter;@Override
protected int initLayout() {
return R.layout.activity_main;
}@Override
protected void initView() {
drawer_layout = fvbi(R.id.drawer_layout);
view_pager = fvbi(R.id.view_pager);
tab_layout = fvbi(R.id.tab_layout);
//侧拉可以不用设置
}@Override
protected void initData() {
list.add(new ShouFragment());
list.add(new MyFragment());
//创建适配器
adapter = new MyFragmentAdapter(getSupportFragmentManager(), list, titles);
view_pager.setAdapter(adapter);
//关联TabLayout
tab_layout.setupWithViewPager(view_pager);
}@Override
protected void initListener() {}
}
基类抽取
package weektest.baway.com.week3;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
public abstract class BaseAcitvity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(initLayout());
initView();
initData();
initListener();
}
//监听
protected abstract void initListener();
//初始化数据
protected abstract void initData();
//初始化控件
protected abstract void initView();
//初始化布局
protected abstract int initLayout();
protected <T extends View> T fvbi(int resId){
return (T)findViewById(resId);
}
}
Fragment1 首页页面
package weektest.baway.com.week3.fragment;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;
import java.util.ArrayList;
import java.util.List;
import weektest.baway.com.week3.R;
import weektest.baway.com.week3.adapter.MyAdapter;
import weektest.baway.com.week3.bean.News;
import weektest.baway.com.week3.bean.NewsBean;
import weektest.baway.com.week3.sql.SqlDao;
import weektest.baway.com.week3.util.Httputil;
public class Fragment1 extends BaseFragment{
private Banner my_banner;
private PullToRefreshListView p_listview;
private String url = “http://api.expoon.com/AppNews/getNewsList/type/1/p/”;
private List pic_list = new ArrayList<>();
private MyAdapter adapter;
private List list = new ArrayList<>();
private SqlDao dao;
private int page = 1;
//加载布局
@Override
protected void initView() {
my_banner = fvbi(R.id.banner);
p_listview = fvbi(R.id.p_listview);
//设置上拉刷新
p_listview.setMode(PullToRefreshBase.Mode.BOTH);
//设置刷新中支持滚动
p_listview.setScrollingWhileRefreshingEnabled(true);
}
@Override
protected void initData() {
//创建数据库工具类
dao = new SqlDao(getActivity());
//设置banner
setBanner();
//创建适配器
adapter = new MyAdapter(list, getActivity());
p_listview.setAdapter(adapter);
//判断网络
if (Httputil.isNetConnected(getActivity())){
//有网请求数据
getMyData(page);
}else{
Toast.makeText(getActivity(),"请检查网络连接",Toast.LENGTH_SHORT).show();
//查询数据库
String s = dao.selectData(url + page);
jsonParse(s);
}
}
private void getMyData(int p) {
final String mUrl = url + p;
Httputil.MyTask(mUrl, new Httputil.MyCallBack() {
@Override
public void getData(String str) {
//保存到数据库
dao.insertData(mUrl,str);
//解析
jsonParse(str);
}
});
}
private void jsonParse(String s) {
Gson gson = new Gson();
NewsBean newsBean = gson.fromJson(s, NewsBean.class);
list.addAll(newsBean.getData());
//刷新适配器
adapter.notifyDataSetChanged();
//关闭刷新
p_listview.onRefreshComplete();
}
private void setBanner() {
pic_list.add("http://ww4.sinaimg.cn/large/006uZZy8jw1faic21363tj30ci08ct96.jpg");
pic_list.add("http://ww4.sinaimg.cn/large/006uZZy8jw1faic259ohaj30ci08c74r.jpg");
pic_list.add("http://ww4.sinaimg.cn/large/006uZZy8jw1faic2b16zuj30ci08cwf4.jpg");
pic_list.add("http://ww4.sinaimg.cn/large/006uZZy8jw1faic2e7vsaj30ci08cglz.jpg");
//设置自动轮播
my_banner.isAutoPlay(true).setDelayTime(2500).setImages(pic_list).setImageLoader(new ImageLoader() {
@Override
public void displayImage(Context context, Object path, ImageView imageView) {
Glide.with(context).load(path).into(imageView);
}
}).start();
}
@Override
protected int initLayout() {
return R.layout.fragment1;
}
@Override
protected void initListener() {
Log.i("aaa","initListener : " + "aaaaa");
p_listview.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> pullToRefreshBase) {
//下拉
list.clear();
getMyData(page);
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> pullToRefreshBase) {
//上拉
page++;
getMyData(page);
}
});
}
}
BaseFragment 基类抽取Fragment
package weektest.baway.com.week3.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;
public abstract class BaseFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(initLayout() , container, false);
return view;
}
//初始化控件
protected abstract int initLayout();
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
initView();
}
protected abstract void initView();
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initData();
initListener();
}
//初始化数据
protected abstract void initData();
//findViewById
protected T fvbi (int resId){
return getView().findViewById(resId);
}
//监听
protected abstract void initListener();
}
MyFragmentAdapter 适配器
package weektest.baway.com.week3.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 java.util.List;
public class MyFragmentAdapter extends FragmentPagerAdapter {
private List<Fragment> list;
private String[] titles;
public MyFragmentAdapter(FragmentManager fm, List<Fragment> list, String[] titles) {
super(fm);
this.list = list;
this.titles = titles;
}
@Override
public Fragment getItem(int i) {
return list.get(i);
}
@Override
public int getCount() {
return list.size();
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return titles[position];
}
}
数据库创建与查询
创建
package weektest.baway.com.week3.helper;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class MyHelper extends SQLiteOpenHelper {
public MyHelper(Context context) {
super(context, “bawei.db”, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table news(url varchar(100),json varchar(800))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
添加查询
package weektest.baway.com.week3.sql;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import weektest.baway.com.week3.helper.MyHelper;
public class SqlDao {
private final MyHelper myHelper;
private String table = “news”;
public SqlDao(Context mcontext){
myHelper = new MyHelper(mcontext);
}
public void insertData(String url,String json){
SQLiteDatabase database = myHelper.getWritableDatabase();
//先删除数据
database.delete(table,"url=?" ,new String[]{url});
//添加数据
ContentValues values = new ContentValues();
values.put("url",url);
values.put("json",json);
database.insert(table,null,values);
}
//查询数据库
public String selectData(String url){
String json = "";
SQLiteDatabase database = myHelper.getWritableDatabase();
Cursor cursor = database.query(table, null, "url=?", new String[]{url}, null, null, null);
while (cursor.moveToNext()){
json = cursor.getString(cursor.getColumnIndex("json"));
}
return json;
}
}