登录页面

目录

目录

登录页面

登录布局

 显示页面show

布局-侧拉 -tablayout

apapter-FilmPageAdapter

adapter-ShowPageAdapter

数据库-DBsql

DBdao

frament-base

fragment布局



登录页面

package com.example.week_04_01;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private ImageView imageView;
    private TextView textView;
    private int time=3;
    private SharedPreferences preferences;

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
           if (time>0){
               time--;
               textView.setText(time+"s");
               handler.sendEmptyMessageDelayed(0,1000);
           }else{
               Intent intent = new Intent(MainActivity.this,ShowActivity.class);
               startActivity(intent);
               finish();
               edit.putBoolean("ischeck",true);
               edit.commit();
           }
        }
    };
    private SharedPreferences.Editor edit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取资源id
        imageView=findViewById(R.id.image_jump);
        textView=findViewById(R.id.text_jump);
        handler.sendEmptyMessageDelayed(0,1000);
        preferences=getSharedPreferences("url",MODE_PRIVATE);
        edit = preferences.edit();
        boolean ischeck = preferences.getBoolean("ischeck", false);
        if (ischeck){
            handler.removeCallbacksAndMessages(null);
            Intent intent = new Intent(MainActivity.this,ShowActivity.class);
            startActivity(intent);
            finish();
        }
    }
}

登录布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context=".MainActivity"
    >
    <TextView
        android:layout_marginTop="20dp"
        android:id="@+id/text_jump"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:text="3s"
        app:layout_constraintTop_toTopOf="parent"
        android:textSize="18sp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginRight="20dp"
        android:gravity="center"
        android:background="@drawable/shape"/>

    <ImageView
        android:id="@+id/image_jump"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@mipmap/ic_launcher"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />


</android.support.constraint.ConstraintLayout>

 显示页面show

package com.example.week_04_01;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;


import com.example.adapter.ShowPageAdapter;
import com.example.fragment.LeftFragment;

public class ShowActivity extends AppCompatActivity {
    private ViewPager viewPager;
    private TabLayout tabLayout;
    private DrawerLayout drawerLayout;
    private ActionBarDrawerToggle toggle;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.show_item);
        //初始化
        initView(savedInstanceState);
        //加载数据
        initData();
    }

    private void initData() {

    }

    private void initView(Bundle savedInstanceState) {
        //获取资源id
        viewPager=findViewById(R.id.show_viewpager);
        tabLayout=findViewById(R.id.show_tablayout);
        drawerLayout=findViewById(R.id.drawerlayout);
        //创建适配器
        viewPager.setAdapter(new ShowPageAdapter(getSupportFragmentManager()));
        tabLayout.setupWithViewPager(viewPager);
        //允许侧拉左上角按钮
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.opan, R.string.close);
        toggle.syncState();
        drawerLayout.addDrawerListener(toggle);
        //创建侧滑界面
        if (savedInstanceState==null){
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.framelayout,new LeftFragment())
                    .commit();
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (toggle.onOptionsItemSelected(item)){
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

布局-侧拉 -tablayout

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/drawerlayout"

    xmlns:app="http://schemas.android.com/apk/res-auto">
   <!--主界面-->
    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
        <android.support.v4.view.ViewPager
            android:id="@+id/show_viewpager"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toTopOf="@id/show_tablayout"/>
        <android.support.design.widget.TabLayout
            android:id="@+id/show_tablayout"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@id/show_viewpager"/>
    </android.support.constraint.ConstraintLayout>
<!--侧拉界面-->
    <FrameLayout
        android:id="@+id/framelayout"
        android:layout_width="320dp"
        android:layout_gravity="start"
        android:layout_height="match_parent"
        android:background="#fff"/>
</android.support.v4.widget.DrawerLayout>

apapter-FilmPageAdapter

package com.example.adapter;

import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;


import com.andy.library.ChannelBean;
import com.example.fragment.HotFragment;
import com.example.fragment.NowFragment;
import com.example.fragment.SoonFragment;

import java.util.ArrayList;
import java.util.List;


public class FilmPageAdapter extends FragmentPagerAdapter {
   private List<ChannelBean> list;

    public FilmPageAdapter(FragmentManager fm) {
        super(fm);
        list=new ArrayList<>();
    }

    public void setList(List<ChannelBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public Fragment getItem(int i) {
        switch (i){
            case 0:
                return new HotFragment();
            case 1:
                return new NowFragment();
            case 2:
                return new SoonFragment();
                default:
                    return new Fragment();
        }

    }

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

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

adapter-ShowPageAdapter

package com.example.adapter;

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.support.v4.view.PagerAdapter;

import com.example.fragment.FilmFragment;
import com.example.fragment.MMFragment;

public class ShowPageAdapter extends FragmentPagerAdapter {
    private String[] meuns = new String[]{"影片","影院","会员","设置"};

    public ShowPageAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int i) {
        switch (i){
            case 0:
                return new FilmFragment();
                default:
                    return new MMFragment();
        }

    }

    @Override
    public int getCount() {
        return meuns.length;
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return meuns[position];
    }
}

数据库-DBsql

package com.example.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class MySqlite extends SQLiteOpenHelper {
    public MySqlite( Context context) {
        super(context, "user.db", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table users(id integer primary key autoincrement," +
                "name text," +
                "ischeck boolean)");
        ContentValues values =new ContentValues();
        values.put("name","热门影片");
        values.put("ischeck",true);
        db.insert("users",null,values);
        values.put("name","正在上映");
        values.put("ischeck",true);
        db.insert("users",null,values);
        values.put("name","即将上映");
        values.put("ischeck",true);
        db.insert("users",null,values);


        values.put("name","推荐");
        values.put("ischeck",false);
        db.insert("users",null,values);
        values.put("name","关注");
        values.put("ischeck",false);
        db.insert("users",null,values);
        values.put("name","头条");
        values.put("ischeck",false);
        db.insert("users",null,values);
        values.put("name","微视");
        values.put("ischeck",false);
        db.insert("users",null,values);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

DBdao

package com.example.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.andy.library.ChannelBean;
import com.example.database.MySqlite;

import java.util.ArrayList;
import java.util.List;

public class UserDao {
    private static UserDao insanner;
    private final SQLiteDatabase sb;

    public UserDao(Context context) {
        sb = new MySqlite(context).getWritableDatabase();

    }
    public static UserDao getInsanner(Context context) {
        if (insanner==null){
            insanner=new UserDao(context);
        }
        return insanner;
    }
    //查询
    public List<ChannelBean> select() {
        List<ChannelBean> list = new ArrayList<>();
        Cursor query = sb.query("users", null, null, null, null, null, null);
        while (query.moveToNext()){
            String name = query.getString(query.getColumnIndex("name"));
            Boolean ischeck = query.getInt(query.getColumnIndex("ischeck"))==1?true:false;
            list.add(new ChannelBean(name,ischeck));
        }
        return list;
    }

    public void addAll(List<ChannelBean> datas) {
        try{
            sb.beginTransaction();
            for (ChannelBean bean: datas
                 ) {
                add(bean);
            }
            sb.setTransactionSuccessful();
        }finally {
            sb.endTransaction();
        }
    }
    //添加数据到数据库
    private void add(ChannelBean bean) {
        ContentValues values=new ContentValues();
        values.put("name",bean.getName());
        values.put("ischeck",bean.isSelect());
        sb.insert("users",null,values);
    }
}

frament-base

package com.example.fragment;

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;

public abstract class BaseFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
       if (getLayoutReseId()!=0) {
           return inflater.inflate(getLayoutReseId(), container, false);
       }
       View view=getLayoutView();
       if (view!=null){
           return view;
       }
       return null;
    }

    protected  View getLayoutView(){
        return null;
    }

    protected  int getLayoutReseId(){
        return 0;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //初始化
        initView(view);
    }

    protected abstract void initView(View view);

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //获取数据
        initData();
        
    }

    protected abstract void initData();
}
package com.example.fragment;

import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageButton;


import com.andy.library.ChannelActivity;
import com.andy.library.ChannelBean;
import com.example.adapter.FilmPageAdapter;
import com.example.dao.UserDao;
import com.example.week_04_01.R;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

import static com.andy.library.ChannelActivity.REQUEST_CODE;
import static com.andy.library.ChannelActivity.RESULT_JSON_KEY;

public class FilmFragment extends BaseFragment {
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private List<ChannelBean> select;
    private FilmPageAdapter filmPageAdapter;
    private ImageButton imageButton;

    @Override
    protected void initView(View view) {
    //获取资源id
        tabLayout=view.findViewById(R.id.film_tablayout);
        viewPager=view.findViewById(R.id.film_viewpager);
        imageButton=view.findViewById(R.id.imagebutton);
        //创建适配器
        filmPageAdapter = new FilmPageAdapter(getChildFragmentManager());
        viewPager.setAdapter(filmPageAdapter);
        tabLayout.setupWithViewPager(viewPager);
    }
    @Override
    protected void initData() {
        List<ChannelBean> datas = new ArrayList<>();
        datas.add(new ChannelBean("体育",false));
        datas.add(new ChannelBean("新闻",false));
        datas.add(new ChannelBean("腾讯",false));
        datas.add(new ChannelBean("乐视",false));
        datas.add(new ChannelBean("快手",false));
        //把集合datas调用添加数据库的方法添加到数据库中
        UserDao.getInsanner(getActivity()).addAll(datas);
        //从数据库查询数据
        select = UserDao.getInsanner(getActivity()).select();
        //调用适配器的方法把选中为true的传值到适配器中
        filmPageAdapter.setList(getSelectResult(select));
        //点击按钮
        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Gson gson = new Gson();
                //将查询出来的数据转化为json串
                String toJson = gson.toJson(select);
                //通过跳转把json串传递到ChannelActivity
                Intent intent = new Intent(getActivity(),ChannelActivity.class);
                intent.putExtra(RESULT_JSON_KEY,toJson);
                startActivityForResult(intent,REQUEST_CODE);

            }
        });

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode==REQUEST_CODE){
            String json = data.getStringExtra("json");
            select=new Gson().fromJson(json,new TypeToken<ArrayList<ChannelBean>>(){}.getType());
            //调用适配器的方法把选中为true的传值到适配器中
            filmPageAdapter.setList(getSelectResult(select));
            return;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
    //选择选中状态为true的存入新集合中
    private List<ChannelBean> getSelectResult(List<ChannelBean> datas) {
        //创建新集合存放true的数据
        List<ChannelBean> result = new ArrayList<>();
        //遍历原来的集合
        for (ChannelBean bean:datas
             ) {
            //状态为true的存入新集合
            if (bean.isSelect()){
                result.add(bean);
            }

        }
        return result;
    }

    @Override
    protected int getLayoutReseId() {
        return R.layout.film_item;
    }
}

fragment布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <android.support.design.widget.TabLayout
        android:id="@+id/film_tablayout"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:tabMode="scrollable"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@id/imagebutton"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@id/film_viewpager"/>
    <ImageButton
        android:id="@+id/imagebutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_action_name"
        app:layout_constraintLeft_toRightOf="@id/film_tablayout"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="@id/film_tablayout"
        app:layout_constraintRight_toRightOf="parent"/>
    <android.support.v4.view.ViewPager
        android:id="@+id/film_viewpager"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/film_tablayout"/>
</android.support.constraint.ConstraintLayout>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值