最终

切换布局

<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="9"
        android:id="@+id/frame_layout"
        ></FrameLayout>
    <RadioGroup
        android:id="@+id/radio_group"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal"
        >
        <RadioButton
            android:id="@+id/rb1"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:button="@null"
            android:checked="true"
            android:text="我的"
            android:textSize="20dp"
            android:gravity="center"
            android:drawableTop="@drawable/sel"
            />
        <RadioButton
            android:id="@+id/rb2"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:button="@null"
            android:drawableTop="@drawable/sel"
            android:text="你的"
            android:textSize="20dp"
            android:gravity="center"
            />
        <RadioButton
            android:id="@+id/rb3"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:button="@null"
            android:drawableTop="@drawable/sel"
            android:text="他的"
            android:textSize="20dp"
            android:gravity="center"
            />
    </RadioGroup>
</LinearLayout>

代码

package com.example.dell.shujuku;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.RadioGroup;

import com.example.dell.shujuku.base.BaseActivity;
import com.example.dell.shujuku.fragment.fragment1;
import com.example.dell.shujuku.fragment.fragment2;
import com.example.dell.shujuku.fragment.fragment3;

public class MainActivity extends BaseActivity {


    private FrameLayout frameLayout;
    private RadioGroup radioGroup;
    private FragmentManager supportFragmentManager;
    private com.example.dell.shujuku.fragment.fragment1 fragment1;
    private com.example.dell.shujuku.fragment.fragment2 fragment2;
    private com.example.dell.shujuku.fragment.fragment3 fragment3;

    @Override
    protected void initView() {
        radioGroup = findViewById(R.id.radio_group);
    }

    @Override
    protected void initData() {
        fragment1 = new fragment1();
        fragment2 = new fragment2();
        fragment3 = new fragment3();
        //获取管理器
        supportFragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = supportFragmentManager.beginTransaction();
        transaction.add(R.id.frame_layout, fragment1)
                .add(R.id.frame_layout, fragment2)
                .add(R.id.frame_layout, fragment3)
                .show(fragment1)
                .hide(fragment2)
                .hide(fragment3)
                .commit();

    }


    @Override
    protected void Event() {
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                FragmentTransaction transaction = supportFragmentManager.beginTransaction();
                switch (checkedId){
                    case R.id.rb1:
                        transaction .show(fragment1)
                                .hide(fragment2)
                                .hide(fragment3).commit();
                        break;
                    case R.id.rb2:
                        transaction .show(fragment2)
                                .hide(fragment1)
                                .hide(fragment3).commit();
                        break;
                    case R.id.rb3:
                        transaction .show(fragment3)
                                .hide(fragment2)
                                .hide(fragment1).commit();
                        break;
                }
            }
        });
    }

    @Override
    public int bindlayout() {
        return R.layout.activity_main;
    }
}


上面可以切换

<?xml version="1.0" encoding="utf-8"?>



<android.support.design.widget.TabLayout
android:id="@+id/tab"
app:tabMode=“scrollable”
android:layout_width=“0dp”
android:layout_height=“match_parent”
android:layout_weight=“1”

        ></android.support.design.widget.TabLayout>
    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@drawable/see_true"
        android:id="@+id/img_pic"
        />
</LinearLayout>

<android.support.v4.view.ViewPager
android:layout_width=“match_parent”
android:layout_height=“0dp”
android:layout_weight=“1”
android:id="@+id/view_pager"
></android.support.v4.view.ViewPager>

```
代码

package com.example.dell.shujuku.fragment;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.widget.ImageView;

import com.example.dell.shujuku.R;
import com.example.dell.shujuku.adapter.FTAdpter;
import com.example.dell.shujuku.base.BaseFragment;
import com.example.dell.shujuku.tab.tab1;
import com.example.dell.shujuku.tab.tab2;

import java.util.ArrayList;

public class fragment1 extends BaseFragment {

private ImageView imageView;
private ViewPager pager;
private TabLayout tabLayout;
private ArrayList<Fragment> flist;
private ArrayList<String> tlist;

@Override
public int bindlayout() {
    return R.layout.frag1;
}

@Override
protected void initView() {
    imageView = getView().findViewById(R.id.img_pic);
    pager = getView().findViewById(R.id.view_pager);
    tabLayout = getView().findViewById(R.id.tab);
}

@Override
protected void initData() {
    //先是Viewpager的数据
    flist = new ArrayList<>();
    flist.add(new tab1());
    flist.add(new tab2());

    //然后是Tab的数据
    tlist = new ArrayList<>();
    for(int i = 0; i< flist.size(); i++){
        tlist.add("哲家大旗"+i);
    }
    //设置适配器
    pager.setAdapter(new FTAdpter(getActivity().getSupportFragmentManager(),flist,tlist));
    //绑定数据
    tabLayout.setupWithViewPager(pager);
}

@Override
protected void Event() {

}

}

Pager的 代码

package com.example.dell.shujuku.adapter;

import android.content.Context;
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.widget.BaseAdapter;

import java.util.ArrayList;

public class FTAdpter extends FragmentPagerAdapter {
private ArrayList flist;
private ArrayList tlist;

public FTAdpter(FragmentManager fm, ArrayList<Fragment> flist, ArrayList<String> tlist) {
    super(fm);
    this.flist = flist;
    this.tlist = tlist;
}

@Override
public Fragment getItem(int i) {
    return flist.get(i);
}

@Override
public int getCount() {
    return flist.size();
}

@Nullable
@Override
public CharSequence getPageTitle(int position) {
    return tlist.get(position);
}

}
//点击跳转

protected void Event() {
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //点击图片跳转
                Intent intent = new Intent(getActivity(), TwoActivity.class);
                //存入Tab数据
                intent.putStringArrayListExtra("key", tlist);
                startActivityForResult(intent, 100);

            }
        });
    }

//跳转的页面

package com.example.dell.shujuku;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.dell.shujuku.adapter.GAdapter;
import com.example.dell.shujuku.base.BaseActivity;

import java.util.ArrayList;

public class TwoActivity extends BaseActivity implements View.OnClickListener{


    private GridView g1;
    private GridView g2;
    private ArrayList<String> list1;
    private ArrayList<String> list2;
    private GAdapter gAdapter1;
    private GAdapter gAdapter2;
    private ImageView backs;
    private TextView textView;

    @Override
    protected void initView() {
        g1 = findViewById(R.id.G1);
        g2 = findViewById(R.id.G2);

        backs = findViewById(R.id.backs);
        textView = findViewById(R.id.finish);

        backs.setOnClickListener(this);
        textView.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        //得到数据
        Intent intent = getIntent();
        //取出数据
        list1 = intent.getStringArrayListExtra("key");

        list2 = new ArrayList<>();
        for(int t=0;t<10;t++){
            list2.add("白骨精"+t);
        }
        
        //t2中删除t1的数据
        list2.remove(list1);
        
        //创建适配器
        gAdapter1 = new GAdapter(TwoActivity.this, list1);
        gAdapter2 = new GAdapter(TwoActivity.this,list2);

        //设置
        g1.setAdapter(gAdapter1);
        g2.setAdapter(gAdapter2);

    }

    @Override
    protected void Event() {
        //点击的时候
        g1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //点击的时候
                String ss = list1.get(position);
                list1.remove(ss);
                list2.add(ss);

                gAdapter1.notifyDataSetChanged();
                gAdapter2.notifyDataSetChanged();
            }
        });
        g2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //点击的时候
                String sss = list2.get(position);
                list2.remove(sss);
                list1.add(sss);

                gAdapter1.notifyDataSetChanged();
                gAdapter2.notifyDataSetChanged();
            }
        });
    }

    @Override
    public int bindlayout() {
        return R.layout.activity_two;
    }
    //点击的时候回传
    @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
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //判断
        //取出集合
        //清楚所有集合的数据的数据
        //Tab目前的数据清空
        //把新的数据放入tab中
        //循环数据给Tab、
        //创建一个新的tab属性
        //取出数据 get(变量)赋值给新的Tab
        //然后在赋值给滑动的Tab
        //展示到第一个页面的数据
        //重要-------------------刷新适配器//绑定tablayout
        if(resultCode==200){
            ArrayList<String> newdate = data.getStringArrayListExtra("key");
            tlist.clear();
            flist.clear();
            tabLayout.removeAllTabs();
            tlist.addAll(newdate);
            for (int i=0;i<tlist.size();i++){
                TabLayout.Tab tab = tabLayout.newTab();
                tab.setText(tlist.get(i));
                tabLayout.addTab(tab);
                if (i==0){
                    flist.add(new tab1());
                }else{
                    flist.add(new tab2());
                }
            }
            ftAdpter.notifyDataSetChanged();
            tabLayout.setupWithViewPager(pager);
        }

    }

//数据库代码

package com.qy.month.tab;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.webkit.WebView;
import android.widget.ListView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.qy.month.R;
import com.qy.month.adapter.MyListAdapter;
import com.qy.month.base.BaseFragment;
import com.qy.month.beanlist.Four;
import com.qy.month.beanlist.One;
import com.qy.month.beanlist.Three;
import com.qy.month.beanlist.Two;
import com.qy.month.http.HttpUtils;
import com.qy.month.sql.SQLHelp;

import java.util.ArrayList;

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) {

                            }
                        });
            } 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;
}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值