今天给大家带来一篇本月所学习的综合内容,把本月学习的知识点融合在一起,写一个类似于简单的微信界面的小案例
首先写代码之前,我们要准备一下前期的准备工作.
1.网络权限
2.依赖
3.图标
4.工具类
首先第一步,布局文件
===============MainActivity布局文件==============================
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<FrameLayout
android:id="@+id/frag"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<RadioGroup
android:id="@+id/rg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rb1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:checked="true"
android:drawableTop="@drawable/sel"
android:gravity="center"
android:text="Fragment1" />
<RadioButton
android:id="@+id/rb2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:drawableTop="@drawable/sel"
android:gravity="center"
android:text="Fragment2" />
<RadioButton
android:id="@+id/rb3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:drawableTop="@drawable/sel"
android:gravity="center"
android:text="Fragment3" />
</RadioGroup>
</LinearLayout>
=================Fragment1布局文件============================
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<android.support.design.widget.TabLayout
android:id="@+id/tab"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_weight="1"
/>
<ImageView
android:id="@+id/imageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@mipmap/ic_launcher"
/>
</LinearLayout>
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
=================Fragment2布局文件============================
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.youth.banner.Banner
android:id="@+id/banner"
android:layout_width="match_parent"
android:layout_height="200dp"/>
</LinearLayout>
=================Fragment3布局文件============================
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/draw"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="我是text,没啥大用" />
</LinearLayout>
<!--=======侧拉============-->
<LinearLayout
android:layout_width="150dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:orientation="vertical">
<ImageView
android:id="@+id/touxiang"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center"
/>
<ListView
android:id="@+id/liebiao"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
=================抽基类BaseActivity文件============================
public abstract class BaseActivity extends FragmentActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//绑定视图
setContentView(bindLayout());
initView();
initData();
bindEvent();
}
protected abstract int bindLayout();
protected abstract void initView();
protected abstract void initData();
protected abstract void bindEvent();
protected <T extends View> T bindView(int resid){
return findViewById(resid);
}
}
=================抽基类BaseFragment文件============================
public abstract class BaseFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(bindLayout(),container,false);
}
protected abstract int bindLayout();
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initView();
initData();
bindEvent();
}
protected abstract void initView();
protected abstract void initData();
protected abstract void bindEvent();
protected <T extends View> T bindView(int resid){
return (T)getView().findViewById(resid);
}
}
=================MainActivity文件============================
public class MainActivity extends BaseActivity {
private String bannerurl = "https://api.apiopen.top/videoCategory";
private String listviewurl = "http://172.17.8.100/small/commodity/v1/commodityList";
private RadioGroup rg;
private Fragment_01 fragment_01;
private Fragment_02 fragment_02;
private Fragment_03 fragment_03;
private FragmentManager manager;
//绑定视图
@Override
protected int bindLayout() {
return R.layout.activity_main;
}
//绑定控件
@Override
protected void initView() {
rg = findViewById(R.id.rg);
}
//操作数据
@Override
protected void initData() {
//创建Fragment 3个
fragment_01 = new Fragment_01();
fragment_02 = new Fragment_02();
fragment_03 = new Fragment_03();
//开启事务
manager = getSupportFragmentManager();
//运行事务
manager.beginTransaction().
add(R.id.frag,fragment_01).add(R.id.frag,fragment_02).add(R.id.frag,fragment_03).show(fragment_01).hide(fragment_02).hide(fragment_03).commit();
}
@Override
protected void bindEvent() {
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
switch (checkedId){
case R.id.rb1:
transaction.show(fragment_01).hide(fragment_02).hide(fragment_03);
break;
case R.id.rb2:
transaction.show(fragment_02).hide(fragment_01).hide(fragment_03);
break;
case R.id.rb3:
transaction.show(fragment_03).hide(fragment_02).hide(fragment_01);
break;
}
transaction.commit();
}
});
}
}
=================fragment包下Fragment1文件============================
public class Fragment_01 extends BaseFragment {
private ViewPager vp;
private ImageView imageView;
private TabPageAdapter adapter;
private TabLayout tab;
private ArrayList<Fragment> listVP;
private TabPageAdapter tadadapter;
@Override
protected int bindLayout() {
return R.layout.fragment_01;
}
@Override
protected void initView() {
//查找控件
vp = bindView(R.id.vp);
tab = bindView(R.id.tab);
imageView = bindView(R.id.imageView);
}
@Override
protected void initData() {
//创建一个集合储存Tab下viewpager的数据
listVP = new ArrayList<>();
listVP.add(new Tab01());
listVP.add(new Tab02());
listVP.add(new Tab03());
listVP.add(new Tab04());
listVP.add(new Tab05());
//tab数据
ArrayList<String> listTab = new ArrayList<>();
for (int i=0;i<listVP.size();i++){
listTab.add("新闻"+i);
}
tadadapter = new TabPageAdapter(getActivity().getSupportFragmentManager(),listVP,listTab);
vp.setAdapter(tadadapter);
tab.setupWithViewPager(vp);
}
@Override
protected void bindEvent() {
}
}
=================fragment包下Fragment2文件============================
public class Fragment_02 extends BaseFragment {
private Banner banner;
private String bannerurl = "https://api.apiopen.top/videoCategory";
private List<LunBean.ResultBean.ItemListBean> list;
@Override
protected int bindLayout() {
return R.layout.fragment_02;
}
@Override
protected void initView() {
banner = bindView(R.id.banner);
}
@Override
protected void initData() {
HttpUtils.httpAsynTask(bannerurl, new HttpUtils.CallBackString() {
@Override
public void getData(String s) {
Gson gson = new Gson();
LunBean lunBean = gson.fromJson(s, LunBean.class);
list = lunBean.getResult().getItemList();
//设置轮播图
banner.setImages(list);
banner.setImageLoader(new ImageLoader() {
@Override
public void displayImage(Context context, Object path, ImageView imageView) {
LunBean.ResultBean.ItemListBean bean= (LunBean.ResultBean.ItemListBean) path;
Glide.with(context).load(bean.getData().getIcon()).into(imageView);
}
});
//是否轮播
banner.isAutoPlay(true);
//轮播时间
banner.setDelayTime(3000);
//开始轮播
banner.start();
}
});
}
@Override
protected void bindEvent() {
}
}
=================tabLayout包下tab1文件============================
public class Tab01 extends BaseFragment {
private String listviewurl = "https://api.apiopen.top/getImages";
private SmartRefreshLayout sma;
private ListView listView;
private List<jsonBean.ResultBean> list;
private MyAdapter adapter;
@Override
protected int bindLayout() {
return R.layout.tab1;
}
@Override
protected void initView() {
sma = bindView(R.id.sma);
listView = bindView(R.id.listview);
}
@Override
protected void initData() {
//如果有网就加载数据
if(HttpUtils.isNetworkConnected(getActivity())){
HttpUtils.httpAsynTask(listviewurl, new HttpUtils.CallBackString() {
@Override
public void getData(String s) {
Gson gson = new Gson();
jsonBean jsonBean = gson.fromJson(s, jsonBean.class);
list = jsonBean.getResult();
adapter = new MyAdapter(getActivity(), list);
listView.setAdapter(adapter);
}
});
}
}
@Override
protected void bindEvent() {
sma.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh(@NonNull RefreshLayout refreshLayout) {
HttpUtils.httpAsynTask(listviewurl, new HttpUtils.CallBackString() {
@Override
public void getData(String s) {
Gson gson = new Gson();
jsonBean jsonBean = gson.fromJson(s, jsonBean.class);
list = jsonBean.getResult();
adapter = new MyAdapter(getActivity(),list);
listView.setAdapter(adapter);
sma.finishRefresh();
}
});
}
});
sma.setOnLoadMoreListener(new OnLoadMoreListener() {
@Override
public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
HttpUtils.httpAsynTask(listviewurl, new HttpUtils.CallBackString() {
@Override
public void getData(String s) {
Gson gson = new Gson();
jsonBean jsonBean = gson.fromJson(s, jsonBean.class);
List<com.example.yuekao001.bean.jsonBean.ResultBean> list2 = jsonBean.getResult();
list.addAll(list2);
adapter.notifyDataSetChanged();
sma.finishLoadMore();
}
});
}
});
}
}
这里注意一下,tabLayout包下面有几个类,就会for循环遍历出来几个tab展示
=================item1文件============================
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="213213213213213213213213"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/image11"
android:layout_width="70dp"
android:layout_height="70dp"
android:src="@mipmap/ic_launcher"
/>
</LinearLayout>
</LinearLayout>
=================布局文件中tab1文件============================
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.scwang.smartrefresh.layout.SmartRefreshLayout
android:id="@+id/sma"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.scwang.smartrefresh.layout.header.ClassicsHeader
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
<com.scwang.smartrefresh.layout.footer.ClassicsFooter
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.scwang.smartrefresh.layout.SmartRefreshLayout>
</LinearLayout>
=================tab文件夹下tab1文件============================
public class Tab01 extends BaseFragment {
private String listviewurl = "https://api.apiopen.top/getImages";
private SmartRefreshLayout sma;
private ListView listView;
private List<jsonBean.ResultBean> list;
private MyAdapter adapter;
@Override
protected int bindLayout() {
return R.layout.tab1;
}
@Override
protected void initView() {
sma = bindView(R.id.sma);
listView = bindView(R.id.listview);
}
@Override
protected void initData() {
//如果有网就加载数据
if(HttpUtils.isNetworkConnected(getActivity())){
HttpUtils.httpAsynTask(listviewurl, new HttpUtils.CallBackString() {
@Override
public void getData(String s) {
Gson gson = new Gson();
jsonBean jsonBean = gson.fromJson(s, jsonBean.class);
list = jsonBean.getResult();
adapter = new MyAdapter(getActivity(), list);
listView.setAdapter(adapter);
}
});
}
}
@Override
protected void bindEvent() {
sma.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh(@NonNull RefreshLayout refreshLayout) {
HttpUtils.httpAsynTask(listviewurl, new HttpUtils.CallBackString() {
@Override
public void getData(String s) {
Gson gson = new Gson();
jsonBean jsonBean = gson.fromJson(s, jsonBean.class);
list = jsonBean.getResult();
adapter = new MyAdapter(getActivity(),list);
listView.setAdapter(adapter);
sma.finishRefresh();
}
});
}
});
sma.setOnLoadMoreListener(new OnLoadMoreListener() {
@Override
public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
HttpUtils.httpAsynTask(listviewurl, new HttpUtils.CallBackString() {
@Override
public void getData(String s) {
Gson gson = new Gson();
jsonBean jsonBean = gson.fromJson(s, jsonBean.class);
List<com.example.yuekao001.bean.jsonBean.ResultBean> list2 = jsonBean.getResult();
list.addAll(list2);
adapter.notifyDataSetChanged();
sma.finishLoadMore();
}
});
}
});
}
}
=================TabPageAdapter文件============================
public class TabPageAdapter extends FragmentPagerAdapter {
private ArrayList<Fragment> listTab;
private ArrayList<String> listVP;
public TabPageAdapter(FragmentManager fm, ArrayList<Fragment> listTab, ArrayList<String> listVP) {
super(fm);
this.listTab = listTab;
this.listVP = listVP;
}
@Override
public Fragment getItem(int i) {
return listTab.get(i);
}
@Override
public int getCount() {
return listVP.size();
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return listVP.get(position);
}
}
=================MyAdapter文件============================
public class MyAdapter extends BaseAdapter {
private Context context;
private List<jsonBean.ResultBean> list;
public MyAdapter(Context context, List<jsonBean.ResultBean> list) {
this.context = context;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object 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) {
int type = getItemViewType(position);
switch (type){
case 0:
ViewHolder1 holder1 = null;
if(convertView==null){
holder1 = new ViewHolder1();
convertView=View.inflate(context,R.layout.item01,null);
holder1.image11 = convertView.findViewById(R.id.image11);
holder1.tv1 = convertView.findViewById(R.id.tv1);
convertView.setTag(holder1);
}else{
holder1 = (ViewHolder1) convertView.getTag();
}
holder1.tv1.setText(list.get(position).getTime());
Glide.with(context).load(list.get(position).getImg()).into(holder1.image11);
break;
case 1:
ViewHolder2 holder2 = null;
if(convertView==null){
holder2 = new ViewHolder2();
convertView=View.inflate(context,R.layout.item02,null);
holder2.image21 = convertView.findViewById(R.id.image21);
holder2.image22 = convertView.findViewById(R.id.image22);
holder2.tv2 = convertView.findViewById(R.id.tv2);
convertView.setTag(holder2);
}else{
holder2 = (ViewHolder2) convertView.getTag();
}
holder2.tv2.setText(list.get(position).getTime());
Glide.with(context).load(list.get(position).getImg()).into(holder2.image21);
Glide.with(context).load(list.get(position).getImg()).into(holder2.image22);
break;
case 2:
ViewHolder3 holder3 = null;
if(convertView==null){
holder3 = new ViewHolder3();
convertView=View.inflate(context,R.layout.item03,null);
holder3.image31 = convertView.findViewById(R.id.image31);
holder3.image32 = convertView.findViewById(R.id.image32);
holder3.image33 = convertView.findViewById(R.id.image33);
holder3.tv3 = convertView.findViewById(R.id.tv3);
convertView.setTag(holder3);
}else{
holder3 = (ViewHolder3) convertView.getTag();
}
holder3.tv3.setText(list.get(position).getTime());
Glide.with(context).load(list.get(position).getImg()).into(holder3.image31);
Glide.with(context).load(list.get(position).getImg()).into(holder3.image32);
Glide.with(context).load(list.get(position).getImg()).into(holder3.image33);
break;
case 3:
ViewHolder4 holder4 = null;
if(convertView==null){
holder4 = new ViewHolder4();
convertView=View.inflate(context,R.layout.item04,null);
holder4.image41 = convertView.findViewById(R.id.image41);
holder4.image42 = convertView.findViewById(R.id.image42);
holder4.image43 = convertView.findViewById(R.id.image43);
holder4.image44 = convertView.findViewById(R.id.image44);
holder4.tv4 = convertView.findViewById(R.id.tv4);
convertView.setTag(holder4);
}else{
holder4 = (ViewHolder4) convertView.getTag();
}
holder4.tv4.setText(list.get(position).getTime());
Glide.with(context).load(list.get(position).getImg()).into(holder4.image41);
Glide.with(context).load(list.get(position).getImg()).into(holder4.image42);
Glide.with(context).load(list.get(position).getImg()).into(holder4.image43);
Glide.with(context).load(list.get(position).getImg()).into(holder4.image44);
break;
}
return convertView;
}
class ViewHolder1{
TextView tv1;
ImageView image11;
}
class ViewHolder2{
TextView tv2;
ImageView image21,image22;
}
class ViewHolder3{
TextView tv3;
ImageView image31,image32,image33;
}
class ViewHolder4{
TextView tv4;
ImageView image41,image42,image43,image44;
}
@Override
public int getItemViewType(int position) {
return position % 4;
}
@Override
public int getViewTypeCount() {
return 4;
}
}
=================补充Fragment1文件============================
public class Frag01 extends BaseFragment {
private TabLayout tabLayout;
private ImageView imageView;
private ViewPager pager;
private ArrayList<Fragment> listP;
private ArrayList<String> listT;
private TabPagerAdapter adapter;
@Override
protected int bindLayout() {
return R.layout.frag01;///注意这里一定要有布局不然 会报错的!!!
}
@Override
protected void initView() {
tabLayout = bindView(R.id.tab);
pager = bindView(R.id.ppager);
imageView = bindView(R.id.image);
}
@Override
protected void initData() {
//先是Viewpager的数据
listP = new ArrayList<>();
listP.add(new Tab01());
listP.add(new Tab02());
//tab的数据
listT = new ArrayList<>();
for (int t = 0; t < listP.size(); t++) {
listT.add("孙悟空" + t);
}
//适配器
adapter = new TabPagerAdapter(getActivity().getSupportFragmentManager(), listP, listT);
pager.setAdapter(adapter);
//关联
tabLayout.setupWithViewPager(pager);
}
@Override
protected void bindEvent() {
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), TwoActivity.class);
intent.putStringArrayListExtra("key", listT);
startActivityForResult(intent, 100);
}
});
}
//接收回传值
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == 200) {
//取出集合
ArrayList<String> listTnew = data.getStringArrayListExtra("key");
///清除所有页面和数据
listT.clear();
listP.clear();
tabLayout.removeAllTabs(); //把tab数据清空
//合并集合
listT.addAll(listTnew);
//把数据设置给tab
for (int t = 0; t < listT.size(); t++) {
//创建一个tab
TabLayout.Tab tab = tabLayout.newTab();
//给tab设置标题
tab.setText(listT.get(t));
///把新的tab放进tabLayout中
tabLayout.addTab(tab);
//重新填装fragment
String ss = listT.get(t);
// if (ss.equals("孙悟空0")){
// listP.add(new Tab01());
// }else if(ss.equals("孙悟空1")){
// listP.add(new Tab02());
// }else if(ss.equals("孙悟空2")){
// listP.add(new Tab03());
// }.....
if (t == 0) {
//因为我们第一个页面有数据,所以要保证数据展示到第一个页面
listP.add(new Tab01());
} else {
//第二个页面没有数据,可以随意填充
listP.add(new Tab02());
}
}
//刷新适配器
adapter.notifyDataSetChanged();
tabLayout.setupWithViewPager(pager);
}
}
}
=================跳转到第二个页面 频道管理============================
public class TwoActivity extends BaseActivity implements View.OnClickListener {
private GridView g1, g2;
private ArrayList<String> list1, list2;
private GridAdapter adapter1;
private GridAdapter adapter2;
@Override
public int bindLayout() {
return R.layout.activity_two;
}
@Override
protected void initView() {
g1 = bindView(R.id.G1);
g2 = bindView(R.id.G2);
bindView(R.id.finish).setOnClickListener(this);
bindView(R.id.backs).setOnClickListener(this);
}
@Override
protected void initData() {
Intent intent = getIntent();
//g1的数据
list1 = intent.getStringArrayListExtra("key");
//g2的数据
list2 = new ArrayList<>();
for (int t = 0; t < 10; t++) {
list2.add("孙悟空" + t);
}
//由于t1中的数据t2肯定没有,所以我们要把t2中和T1一样的数据删掉
list2.removeAll(list1);
//适配器
adapter1 = new GridAdapter(list1, TwoActivity.this);
adapter2 = new GridAdapter(list2, TwoActivity.this);
//设置适配器
g1.setAdapter(adapter1);
g2.setAdapter(adapter2);
}
@Override
protected void bindEvent() {
//点击条目切换
g1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//得到我们点击的条目的内容
String str = list1.get(position);
///删除内容
list1.remove(position);
//把内容添加到list2中
list2.add(str);
//刷新两个适配器
adapter1.notifyDataSetChanged();
adapter2.notifyDataSetChanged();
}
});
//点击条目切换
g2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//得到我们点击的条目的内容
String str = list2.get(position);
///删除内容
list2.remove(position);
//把内容添加到list2中
list1.add(str);
//刷新两个适配器
adapter1.notifyDataSetChanged();
adapter2.notifyDataSetChanged();
}
});
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.finish:
case R.id.backs:
Intent intent = new Intent();
intent.putStringArrayListExtra("key", list1);
setResult( 200,intent);
finish();//注意一定要关闭页面,不然没效果
break;
}
}
}
================================第二个页面布局文件==========================================
<LinearLayout 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"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/backs"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:src="@drawable/fanhui" />
<View
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1" />
<TextView
android:id="@+id/finish"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:background="@drawable/sh"
android:gravity="center"
android:text="完成" />
</LinearLayout>
<TextView
android:background="#0f0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:padding="5dp"
android:text="切换的栏目"
android:textSize="20sp" />
<GridView
android:id="@+id/G1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:horizontalSpacing="5dp"
android:numColumns="4"
android:padding="5dp"
android:scrollbars="none"
android:verticalSpacing="5dp" />
<TextView
android:background="#0f0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:padding="5dp"
android:text="更多栏目"
android:textSize="20sp" />
<GridView
android:id="@+id/G2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:horizontalSpacing="5dp"
android:numColumns="4"
android:padding="5dp"
android:scrollbars="none"
android:verticalSpacing="5dp" />
</LinearLayout>
====================数据库====================================
public class SQLHelp extends SQLiteOpenHelper {
public SQLHelp( Context context) {
super(context, "bw.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE person(id INTEGER PRIMARY KEY AUTOINCREMENT,title TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
=====================Tab1类数据库补充==================================
public class Tab01 extends BaseFragment {
private PullToRefreshListView pull;
private ArrayList<One> ones;
private MyListAdapter adapter;
private SQLiteDatabase db;
@Override
protected int bindLayout() {
return R.layout.tab01;
}
@Override
protected void initView() {
pull = bindView(R.id.pull);
pull.setMode(PullToRefreshBase.Mode.BOTH);
pull.setPullToRefreshOverScrollEnabled(true);
//创建数据库对象
SQLHelp help = new SQLHelp(getActivity());
db = help.getReadableDatabase();
}
@Override
protected void initData() {
if (HttpUtils.isNetworkConnected(getActivity())) {
//有网的时候加载数据
HttpUtils.httpAsynTask("http://172.17.8.100/small/commodity/v1/commodityList",
new HttpUtils.CallBackString() {
@Override
public void getData(String s) {
ones = getArrayData(s);
//设置适配器
adapter = new MyListAdapter(getActivity(), ones);
pull.setAdapter(adapter);
//添加数据库
Cursor cursor = db.query("person", null, null, null, null, null, null);
if (!cursor.moveToFirst()) {
ContentValues values = new ContentValues();
values.put("title", s);
db.insert("person", null, values);
}
}
});
} else {
//没网络查询数据库
Cursor cursor = db.query("person", null, null, null, null, null, null);
if (cursor.moveToFirst()) {
String sdb = cursor.getString(cursor.getColumnIndex("title"));
ones = getArrayData(sdb);
//设置适配器
adapter = new MyListAdapter(getActivity(), ones);
pull.setAdapter(adapter);
}
}
}
@Override
protected void bindEvent() {
pull.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
if (HttpUtils.isNetworkConnected(getActivity())) {
HttpUtils.httpAsynTask("http://172.17.8.100/small/commodity/v1/commodityList",
new HttpUtils.CallBackString() {
@Override
public void getData(String s) {
ones = getArrayData(s);
//设置适配器
adapter = new MyListAdapter(getActivity(), ones);
pull.setAdapter(adapter);
pull.onRefreshComplete();
}
});
} else {
Toast.makeText(getActivity(), "没网", Toast.LENGTH_LONG).show();
}
pull.onRefreshComplete();
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
if (HttpUtils.isNetworkConnected(getActivity())) {
HttpUtils.httpAsynTask("http://172.17.8.100/small/commodity/v1/commodityList",
new HttpUtils.CallBackString() {
@Override
public void getData(String s) {
ArrayList<One> oneload = getArrayData(s);
//设置适配器
ones.addAll(oneload);
adapter.notifyDataSetChanged();
pull.onRefreshComplete();
}
});
} else {
Toast.makeText(getActivity(), "没网", Toast.LENGTH_LONG).show();
}
pull.onRefreshComplete();
}
});
}
public ArrayList<One> getArrayData(String s) {
Gson gson = new Gson();
Four four = gson.fromJson(s, Four.class);
Three result = four.getResult();
Two mlss = result.getMlss();
Two pzsh = result.getPzsh();
Two rxxp = result.getRxxp();
ArrayList<One> oneM = new ArrayList<>();
oneM.addAll(mlss.getCommodityList());
oneM.addAll(pzsh.getCommodityList());
oneM.addAll(rxxp.getCommodityList());
return oneM;
}
}