TabLayout与频道拖拽联合+listview多条目+listview点击跳转

创建数据库

public class DBUtils {
    private final SQLiteDatabase db;
    public DBUtils(Context context) {
        SQLite sqLite = new SQLite(context);
        db = sqLite.getWritableDatabase();
    }
    //添加方法
    public void add(List<ChannelBean> channelBeanList) {
        if (channelBeanList == null || channelBeanList.size() < 0) {
            return;
        }
        for (ChannelBean channelBean : channelBeanList) {
            ContentValues values = new ContentValues();
            values.put("name", channelBean.getName());
            values.put("selected", channelBean.isSelect());
            db.insert("channels", null, values);
        }
    }
    //查询全部的方法
    public List<ChannelBean> chaxun(){
        Cursor cursor = db.query("channels", null, null, null, null, null, null);
        List<ChannelBean> channelBeanList = new ArrayList<>();
        ChannelBean channelBean;
        while (cursor.moveToNext()) {
            String name = cursor.getString(cursor.getColumnIndex("name"));
            int selected = cursor.getInt(cursor.getColumnIndex("selected"));
            channelBeanList.add(new ChannelBean(name, selected == 0 ? false : true));
        }
        cursor.close();
        return channelBeanList;
    }
 //条件查询
 public List<ChannelBean> tiaojian(){
        Cursor cursor = db.query("channels//表名", null, "selected=?", new String[]{"1"}, null, null, null);
        List<ChannelBean> channelBeanList = new ArrayList<>();
        ChannelBean channelBean;
        while (cursor.moveToNext()) {
            String name = cursor.getString(cursor.getColumnIndex("name"));
            int selected = cursor.getInt(cursor.getColumnIndex("selected"));
            channelBeanList.add(new ChannelBean(name, selected == 0 ? false : true));
        }
        cursor.close();
        return channelBeanList;
    }
    //清除方法
    public void clearChannels() {
        db.delete("channels", null, null);
    }
}
MyAdapter类:

public class MyAdapter extends FragmentPagerAdapter {
   
    List<ChannelBean> mChannelBeanList;
    public MyAdapter(FragmentManager fm,List<ChannelBean> channelBeanList) {
        super(fm);
        mChannelBeanList = channelBeanList;
    }

    @Override
    public Fragment getItem(int position) {
        MyFragment myFragment = new MyFragment();
        Bundle bundle = new Bundle();
        bundle.putString("text", mChannelBeanList.get(position).getName());
        myFragment.setArguments(bundle);
        return myFragment;

    }

    @Override
    public int getCount() {
        return mChannelBeanList != null?mChannelBeanList.size():0;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mChannelBeanList.get(position).getName();
    }
}
条目点击事件:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(getActivity(),Second.class);
        intent.putExtra("label_text",list.get(position).getLabel_text());
        startActivity(intent);
    }
});
主界面:

public class MainActivity extends AppCompatActivity {

    private TabLayout tabLayout;
    private ViewPager viewPager;
    private TextView pindao;
    private List<ChannelBean> qunabu;
    private List<ChannelBean> tiaojian;
    private DBUtils dbUtils;
    private MyAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tabLayout = (TabLayout) findViewById(R.id.tablayout);
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        tabLayout.setupWithViewPager(viewPager);
        viewPager.setOffscreenPageLimit(7);
        pindao = (TextView) findViewById(R.id.pindao);
        pindao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ChannelActivity.startChannelActivity(MainActivity.this, qunabu);
            }
        });
        initData();
    }

    private void initData() {
        qunabu = new ArrayList<>();
        tiaojian = new ArrayList<>();
        dbUtils = new DBUtils(this);
        List<ChannelBean> chaxun = dbUtils.chaxun();
        if(chaxun == null || chaxun.size() < 1){
            ChannelBean channelBean1 = new ChannelBean("周一", true);
            ChannelBean channelBean2 = new ChannelBean("周二", true);
            ChannelBean channelBean3 = new ChannelBean("周三", true);
            ChannelBean channelBean4 = new ChannelBean("周四", true);
            ChannelBean channelBean5 = new ChannelBean("周五", true);
            ChannelBean channelBean6 = new ChannelBean("今天", true);
            ChannelBean channelBean7 = new ChannelBean("昨天", false);
            tiaojian.add(channelBean1);
            tiaojian.add(channelBean2);
            tiaojian.add(channelBean3);
            qunabu.add(channelBean1);
            qunabu.add(channelBean2);
            qunabu.add(channelBean3);
            qunabu.add(channelBean4);
            qunabu.add(channelBean5);
            qunabu.add(channelBean6);
            qunabu.add(channelBean7);
            dbUtils.add(qunabu);
        }else {
            qunabu.addAll(chaxun);
            List<ChannelBean> tj = dbUtils.tiaojian();
            tiaojian.addAll(tj);
        }
        adapter = new MyAdapter(getSupportFragmentManager(),tiaojian);
        viewPager.setAdapter(adapter);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == ChannelActivity.REQUEST_CODE && resultCode == ChannelActivity.RESULT_CODE){
            String stringExtra = data.getStringExtra(ChannelActivity.RESULT_JSON_KEY);
            if (TextUtils.isEmpty(stringExtra)) {
                return;
            }
            List<ChannelBean> list = new Gson().fromJson(stringExtra, new TypeToken<List<ChannelBean>>() {
            }.getType());
            if (list == null || list.size() < 1) {
                return;
            }
            qunabu.clear();
            tiaojian.clear();
            qunabu.addAll(list);
            for (ChannelBean channelBean : list) {
                boolean select = channelBean.isSelect();
                if (select) {
                    tiaojian.add(channelBean);
                }
            }
            adapter.notifyDataSetChanged();
            dbUtils.clearChannels();
            dbUtils.add(qunabu);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值