//在activity_main的布局
//在fragmentone中的布局
//导入依赖 compile ‘com.youth.banner:banner:1.4.10’
//在子布局中
//fragmentone中的另一个子布局
//Fragmentone中的代码 package ningjiaxin1.bwie.com.week3_2.Fragment;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
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 java.util.ArrayList;
import ningjiaxin1.bwie.com.week3_2.R;
public class Fragmentone extends Fragment {
private TabLayout tab;
private ViewPager pager;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragmentone, container, false);
tab = view.findViewById(R.id.tablatout);
pager = view.findViewById(R.id.pager);
final ArrayList tit=new ArrayList<>();
tit.add("正在上映");
tit.add("即将上映");
final ArrayList fragments=new ArrayList<>();
fragments.add(new Fragmenta());
fragments.add(new Fragmentb());
pager.setAdapter(new FragmentPagerAdapter(getActivity().getSupportFragmentManager()) {
@Override
public Fragment getItem(int i) {
return fragments.get(i);
}
@Override
public int getCount()
{
return fragments.size();
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return tit.get(position);
}
});
tab.setupWithViewPager(pager);
return view;
}
//Tab中的第一个页面
package com.example.zk3_demo03.fragment;
import android.content.ContentValues;
import android.database.Cursor;
import android.os.AsyncTask;
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.example.zk3_demo03.R;
import com.example.zk3_demo03.adapter.MyAdapter;
import com.example.zk3_demo03.bean.Bean;
import com.example.zk3_demo03.gg.Wangluo;
import com.example.zk3_demo03.netutil.NetUtil;
import com.example.zk3_demo03.sql.Dao;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import java.util.ArrayList;
import java.util.List;
public class Fragment_a extends Fragment {
i
nt page;
String urlString = "http://api.expoon.com/AppNews/getNewsList/type/1/p/";
ArrayList lists = new ArrayList();
private MyAdapter myAdapter;
private PullToRefreshListView pull;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_a, container, false);
pull = view.findViewById(R.id.pull);
//设置支持
pull.setMode(PullToRefreshListView.Mode.BOTH);
init(page);
//创建适配器
myAdapter = new MyAdapter(getActivity(), lists);
//设置适配器
pull.setAdapter(myAdapter);
//监听时间
pull.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2() {
@Override
public void onPullDownToRefresh(PullToRefreshBase pullToRefreshBase) {
lists.clear();
init(0);
}
@Override
public void onPullUpToRefresh(PullToRefreshBase pullToRefreshBase) {
init(page++);
}
});
return view;
}
private void init(int page) {
//有网状态
if(Wangluo.isGG(getActivity()))
{
new My().execute(urlString + page);
}
else
{
//无网络状态
Toast.makeText(getActivity(), "滚", Toast.LENGTH_SHORT).show();
//查询数据库
Dao dao = new Dao(getActivity());
Cursor query = dao.query("person", null, null, null, null, null, null);
//创建集合
ArrayList beans = new ArrayList<>();
//判断查询第一条是否有数据
if(query.moveToFirst())
{
do {
String news_title = query.getString(query.getColumnIndex("news_title"));
//创建对象
Bean.DataBean dataBean = new Bean.DataBean(news_title);
//将对象存入集合中
beans.add(dataBean);
}while (query.moveToNext());
}
lists.addAll(beans);
}
}
class My extends AsyncTask
{
private long insert;
@Override
protected String doInBackground(String... strings) {
String string = NetUtil.getJson(strings[0]);
return string;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
//开始解析
Gson gson = new Gson();
Bean bean = gson.fromJson(s, Bean.class);
List data = bean.getData();
//集合包集合
lists.addAll(data);
//刷新适配器
myAdapter.notifyDataSetChanged();
pull.onRefreshComplete();
//导入数据库
/**
* news_id : 13811
* news_title : 深港澳台千里连线,嘉年华会今夏入川
* news_summary : 6月17—20日,“2016成都深港澳台嘉年华会”(简称嘉年华会)将在成都世纪城国际会展中心举办。其主办方励展华博借力旗
* pic_url : http://f.expoon.com/sub/news/2016/01/21/887844_230x162_0.jpg
*/
Dao dao = new Dao(getActivity());
for (int i = 0; i < lists.size(); i++) {
Bean.DataBean bean1 = lists.get(i);
ContentValues values = new ContentValues();
values.put("news_id",bean1.getNews_id());
values.put("news_title",bean1.getNews_title());
values.put("news_summary",bean1.getNews_summary());
values.put("pic_url",bean1.getPic_url());
insert = dao.insert("person", null, values);
}
Toast.makeText(getActivity(), "添加数据库条数为:"+insert, Toast.LENGTH_SHORT).show();
}
}
}
//Tab中的第二个页面
}