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">
二、写继承类
/**
@Auther: Mr.Zhao
@Date: 2019/1/7 14:28:39
@Description: */ //这里一点记得加abstract
public abstract class BaseActivity extends FragmentActivity {
@Override
protected void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//绑定布局的方法
setContentView(bindLayout());
initView();//找控件
initData();//给控件设置数据(位置不要写反了!!!)
bindEvent();
}
//设置监听的方法
protected abstract void bindEvent();
//操作数据的方法(请求网络)
protected abstract void initData();
//绑定组件的方法
protected abstract void initView();
public abstract int bindLayout();
protected T bindView(int resid){
return findViewById(resid);
}
}
三、主页面代码
//继承上 public class MainActivity extends BaseActivity {
private RadioGroup group;
private Frag01 frag01;
private Frag02 frag02;
private Frag03 frag03;
private FragmentManager manager;
private FrameLayout pager;
@Override
protected void bindEvent() {
group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
//得到管理类
FragmentTransaction transaction = manager.beginTransaction();
switch (checkedId){
case R.id.mBut1:
transaction.show(frag01)
.hide(frag02)
.hide(frag03);
break;
case R.id.mBut2:
transaction.show(frag02)
.hide(frag01)
.hide(frag03);
break;
case R.id.mBut3:
transaction.show(frag03)
.hide(frag01)
.hide(frag02);
break;
}
transaction.commit();
}
});
}
@Override
protected void initData() {
//创建三个Frag类 继承BaseFragment
frag01 = new Frag01();
frag02 = new Frag02();
frag03 = new Frag03();
manager = getSupportFragmentManager();
manager.beginTransaction()
.add(R.id.pager,frag01)
.add(R.id.pager,frag02)
.add(R.id.pager,frag03)
.show(frag01)
.hide(frag02)
.hide(frag03).commit();
}
//找控件
@Override
protected void initView() {
group = findViewById(R.id.radioGroup);
pager = findViewById(R.id.pager);
}
//找布局
@Override
public int bindLayout() {
return R.layout.activity_main;
}
}
Frag继承的BaseFrament
//记得加静态 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 bindEvent();
//操作数据
protected abstract void initData();
//绑定组件
protected abstract void initView();
protected T bindView(int resId){
return (T) getView().findViewById(resId);
}
}
**
以上完成了Fragment下导航栏切换 Mainactivity继承BaseActivity Frag继承BaseFragment
**
四、下面看Frag01里功能,先看布局
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/colorPrimary"
android:orientation="vertical"
android:layout_height="match_parent">
五、在Frag01总 完成上导航栏显示结合Viwepager, 跳转到频道管理
//继承类
public class Frag01 extends BaseFragment {
private TabLayout tabLayout;
private ViewPager pager;
private ImageView imageView;
private ArrayList listP;
private ArrayList ListT;
private TabPagerAdapter adapter;
@Override
protected int bindLayout() {
return R.layout.frag01;
}
@Override
protected void bindEvent() {
//点击跳转到频道管理
imageView.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), TwoActivty.class);
intent.putStringArrayListExtra("key",ListT);
startActivityForResult(intent, 100);
}
});
}
@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 listThew = data.getStringArrayListExtra("key");
///清除所有页面和数据
ListT.clear();
listP.clear();
tabLayout.removeAllTabs();
//合并集合
ListT.addAll(listThew);
//把数据设置给tab
for (int i=0;i
}
六、TabPagerAdapter适配器
public class TabPagerAdapter extends FragmentPagerAdapter {
private ArrayList listP;
private ArrayList listT;
public TabPagerAdapter(FragmentManager fm, ArrayList listP, ArrayList listT) {
super(fm);
this.listP = listP;
this.listT = listT;
}
@Override
public Fragment getItem(int i) {
return listP.get(i);
}
@Override
public int getCount() {
return listP.size();
}
//设置了表头这里不能忘!!!
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return listT.get(position);
}
}
七、从Frag01中跳转到的TwoActivity 在这里实现
//继承BaseActivity 接口点击 public class TwoActivty extends BaseActivity implements View.OnClickListener {
private GridView G1;
private GridView G2;
private ArrayList list1,list2;
private GridAdapter adapter1,adapter2;
@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;
}
}
@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
protected void initData() {
Intent intent = getIntent();
list1 = intent.getStringArrayListExtra("key");
//g2的数据
list2 = new ArrayList<>();
for (int i =0;i<10;i++){
list2.add("阿辰是个猪"+i);
}
//由于t1中的数据t2肯定没有,所以我们要把t2中和T1一样的数据删掉
adapter1 = new GridAdapter(list1, TwoActivty.this);
adapter2 = new GridAdapter(list2, TwoActivty.this);
//设置适配器
G1.setAdapter(adapter1);
G2.setAdapter(adapter2);
}
@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
public int bindLayout() {
return R.layout.activity_two;
}
}
TwoActivity 的布局
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">
TwoActivity 的适配器
public class GridAdapter extends BaseAdapter {
private ArrayList list;
private Context context;
public GridAdapter(ArrayList list, Context context) {
this.list = list;
this.context = context;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = View.inflate(context, R.layout.item,null);
TextView textView = convertView.findViewById(R.id.text1);
textView.setText(list.get(position));
return convertView;
}
}
在Tab01页面展示接口解析数据,添加数据库 有网展示数据 没网展示数据库
Tab01
public class Tab01 extends BaseFragment {
private MyListAdapter adapter;
private PullToRefreshListView pull;
private SQLiteDatabase db;
private ArrayList ones;
@Override
protected int bindLayout() {
return R.layout.tab01;
}
@Override
protected void bindEvent() {
pull.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2(){
@Override
public void onPullDownToRefresh(PullToRefreshBase refreshView) {
if (HttpUtils.isNetworkConnected(getActivity())){
HttpUtils.httpAsTask("http://172.17.8.100/small/commodity/v1/commodityList",
new HttpUtils.CallBackA() {
@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 refreshView) {
if (HttpUtils.isNetworkConnected(getActivity())){
HttpUtils.httpAsTask("http://172.17.8.100/small/commodity/v1/commodityList",
new HttpUtils.CallBackA() {
@Override
public void getData(String s) {
ArrayList data = getArrayData(s);
ones.addAll(data);
adapter.notifyDataSetChanged();
pull.onRefreshComplete();
}
});
}else{
Toast.makeText(getActivity(), "没网", Toast.LENGTH_LONG).show();
}
pull.onRefreshComplete();
}
});
}
@Override
protected void initData() {
if (HttpUtils.isNetworkConnected(getActivity())){
//有网的时候加载数据
HttpUtils.httpAsTask("http://172.17.8.100/small/commodity/v1/commodityList",
new HttpUtils.CallBackA() {
@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 title = cursor.getString(cursor.getColumnIndex("title"));
ones = getArrayData(title);
//设置适配器
adapter = new MyListAdapter(getActivity(),ones);
pull.setAdapter(adapter);
}
}
}
@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();
}
public ArrayList 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 oneM = new ArrayList<>();
oneM.addAll(mlss.getCommodityList());
oneM.addAll(pzsh.getCommodityList());
oneM.addAll(rxxp.getCommodityList());
return oneM;
}
}
数据库类
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) {
}
}
数据展示适配器类
public class MyListAdapter extends BaseAdapter { private Context context; private ArrayList list;
public MyListAdapter(Context context, ArrayList list) {
this.context = context;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView==null){
convertView = View.inflate(context, R.layout.list_item,null);
holder = new ViewHolder();
holder.imageView = convertView.findViewById(R.id.imageview);
holder.textView = convertView.findViewById(R.id.text_name);
holder.textView1 = convertView.findViewById(R.id.text_pric);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
One one = list.get(position);
holder.textView.setText(one.getCommodityName());
holder.textView1.setText(one.getPrice()+" ");
Glide.with(context).load(one.getMasterPic()).into(holder.imageView);
return convertView;
}
class ViewHolder {
private ImageView imageView;
private TextView textView;
private TextView textView1;
}
}
效果