数据库+多条目+侧拉+tablayout+viewpager-fragment的切换+PullToRefreshListView的上拉下拉的刷新+banner的多条目

屏幕适配

values的dimens

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="w">100dp</dimen>
</resources>

activity_main viewpager+fragment+侧滑的切换布局

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

<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer"
    android:layout_height="match_parent"
    tools:context=".MainActivity">



<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >

    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="9"
        ></android.support.v4.view.ViewPager>

    <RadioGroup
        android:id="@+id/group"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal"
        >
        <RadioButton
            android:id="@+id/but1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:button="@null"
            android:gravity="center"
            android:text="影片"
            />
        <RadioButton
            android:id="@+id/but2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:button="@null"
            android:gravity="center"
            android:text="影院"
            />
        <RadioButton
            android:id="@+id/but3"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:button="@null"
            android:gravity="center"
            android:text="会员"
            />
        <RadioButton
            android:id="@+id/but4"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:button="@null"
            android:gravity="center"
            android:text="设置"
            />
    </RadioGroup>
</LinearLayout>

<fragment
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    class="com.bwei.yuekaolianxi.LeftFragment"
    />

</android.support.v4.widget.DrawerLayout>

MainActivity
viewpager+Fragment切换的功能代码

public class MainActivity extends AppCompatActivity {

private ViewPager viewPager;
private RadioGroup radioGroup;
private List<Fragment> list;
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle toggle;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    drawerLayout=findViewById(R.id.drawer);
    viewPager=findViewById(R.id.viewPager);
    radioGroup=findViewById(R.id.group);

    list=new ArrayList<>();
    list.add(new FragmentOne());
    list.add(new FragmentTwo());
    list.add(new FragmentThree());
    list.add(new FragmentFour());

    toggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.open, R.string.close);
    drawerLayout.addDrawerListener(toggle);
    toggle.syncState();


    viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
        @Override
        public Fragment getItem(int i) {
            return list.get(i);
        }

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


    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (checkedId){
                case R.id.but1:
                    viewPager.setCurrentItem(0);
                    break;
                case R.id.but2:
                    viewPager.setCurrentItem(1);
                    break;
                case R.id.but3:
                    viewPager.setCurrentItem(2);
                    break;
                case R.id.but4:
                    viewPager.setCurrentItem(3);
                    break;
            }
        }
    });

}

public void show(int positioin){
    viewPager.setCurrentItem(positioin);
    drawerLayout.closeDrawer(Gravity.START);
}


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

侧滑的fragment的点击事件

public class LeftFragment extends Fragment {
    private ListView listView;
    private MainAdapter adapter;


@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.activity_left,container,false);
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    listView=view.findViewById(R.id.listView);

    adapter = new MainAdapter(getActivity());

    listView.setAdapter(adapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if(position==0){
                return;
            }
            ((MainActivity)getActivity()).show(position-1);
        }
    });

}

侧滑的布局

<ListView
        android:id="@+id/listView"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        ></ListView>

侧滑的多条目加载 头像和listView

public class MainAdapter extends BaseAdapter {

private String[] data=new String[]{
        "影片","影院","会员","设置"
};
private Context context;

public MainAdapter(Context context) {
    this.context = context;
}

@Override
public int getCount() {
    return data.length+1;
}

@Override
public int getViewTypeCount() {
    return 2;
}

@Override
public int getItemViewType(int position) {
    return position==0?1:0;
}

@Override
public String getItem(int position) {
    if(position==0){
        return null;
    }
    return data[position-1];
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if(getItemViewType(position)==1){
        ImagerViewHolder ivh;
        if(convertView==null){
            convertView=LayoutInflater.from(context).inflate(R.layout.image,parent,false);
            ivh=new ImagerViewHolder();
            ivh.imageView=convertView.findViewById(R.id.image);
            convertView.setTag(ivh);
        }else{
            ivh= (ImagerViewHolder) convertView.getTag();
        }
    }else{
        ListViewHolder lvh;
        if(convertView==null){
            convertView=LayoutInflater.from(context).inflate(R.layout.item,parent,false);
            lvh=new ListViewHolder();
            lvh.textView=convertView.findViewById(R.id.text);
            convertView.setTag(lvh);
        }else{
            lvh= (ListViewHolder) convertView.getTag();
        }
        lvh.textView.setText(getItem(position));
    }
    return convertView;
}

class ImagerViewHolder{
    ImageView imageView;
}
class ListViewHolder{
    TextView textView;
}

第三个fragment的XML

<com.handmark.pulltorefresh.library.PullToRefreshListView
        android:id="@+id/pullRefresh"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent"
></com.handmark.pulltorefresh.library.PullToRefreshListView>

PullToRefreshListView的多条目加载
banner和news

private PullToRefreshListView pull;
    private int mapge;
    private MyFragment fragment;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fargmentthree,container,false);
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    pull=view.findViewById(R.id.pullRefresh);

    mapge=1;

    fragment = new MyFragment(getActivity());
    pull.setAdapter(fragment);
    pull.setMode(PullToRefreshBase.Mode.BOTH);

    pull.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
        @Override
        public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
            mapge=1;
            loadData();
        }

        @Override
        public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
        loadData();
        }
    });
	       /*pull.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<GridView>() {
	           @Override
	           public void onPullDownToRefresh(PullToRefreshBase<GridView> refreshView) {
	               mapge=1;
	               loadData();
	           }
	
	           @Override
	           public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) {
	               loadData();
	           }
	       });*/
    loadData();

	        /*GridView refreshableView = pull.getRefreshableView();
	        refreshableView.setNumColumns(2);*/
}

String dataUrl="http://172.17.8.100/movieApi/movie/v1/findReleaseMovieList?count=10&page=";

private void loadData() {
    Util.getInstance().getRequest(dataUrl + mapge, UserBean.class, new Util.Callback<UserBean>() {
            @Override
            public void onSuccess(UserBean o) {
                if(o == null || !o.isIntent()) {
                    Toast.makeText(getActivity(),"请求错误",Toast.LENGTH_SHORT).show();
                    pull.onRefreshComplete();
                } else {
                   if(mapge==1){
                        fragment.setDatas(o.getResult());
                   }else{
                        fragment.addDatas(o.getResult());
                   }
                   mapge++;
                }
         }
    });
}

fragmentOne的布局加载
TabLayout和viewpager

<android.support.design.widget.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        ></android.support.design.widget.TabLayout>


<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintTop_toBottomOf="@id/tabLayout"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    ></android.support.v4.view.ViewPager>

fragment的加载

private TabLayout tabLayout;
    private ViewPager viewPager;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fargmentone,container,false);
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    tabLayout = view.findViewById(R.id.tabLayout);
    viewPager = view.findViewById(R.id.pager);


    viewPager.setAdapter(new ReAdapter(getChildFragmentManager()));
    tabLayout.setupWithViewPager(viewPager);

tablayout的加载适配器

private String[] datas=new String[]{
            "热门影片","正在上映","即将上映"
    };


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

@Override
public Fragment getItem(int i) {
    switch (i){
        case 0:
            return new ReFragment();
        case 1:
            return new DoingFragment();
        case 2:
            return new JiFragment();
    }
    return null;
}

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

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

数据库的创建

public SQLite( 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," +
            "imageUrl text," +
            "summary text)");

    db.execSQL("create table persons(id integer primary key autoincrement," +
            "name text," +
            "imageUrl text," +
            "summary text)");
    db.execSQL("create table goods(id integer primary key autoincrement," +
            "name text," +
            "ischecked byte)");
}

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

}

两个dao层

UserDao

private SQLite sqLite;
    private SQLiteDatabase database;

public UserDao(Context context){
    sqLite=new SQLite(context);
    database=sqLite.getReadableDatabase();
}

public void add(UserBean.ResultBean data){
    ContentValues values = new ContentValues();
    values.put("name",data.getName());
    values.put("imageUrl",data.getImageUrl());
    values.put("summary",data.getSummary());
    database.insert("users",null,values);
}

public void addAll(List<UserBean.ResultBean> datas){
    try {
        database.beginTransaction();
        for (UserBean.ResultBean bean:datas){
            add(bean);
        }
        database.setTransactionSuccessful();
    }finally {
        database.endTransaction();
    }
}

public void delAll(){
    database.delete("users",null,null);
}

public List<UserBean.ResultBean> select(){
    ArrayList<UserBean.ResultBean> beans = new ArrayList<>();
    Cursor cursor = database.query("users", null, null, null, null, null, null);
    while(cursor.moveToNext()){
        String name = cursor.getString(cursor.getColumnIndex("name"));
        String imageUrl = cursor.getString(cursor.getColumnIndex("imageUrl"));
        String summary = cursor.getString(cursor.getColumnIndex("summary"));
        UserBean.ResultBean bean=new UserBean.ResultBean();
        bean.setName(name);
        bean.setImageUrl(imageUrl);
        bean.setSummary(summary);
        beans.add(bean);
    }
    return beans;
}

personsDao

private SQLite sqLite;
    private SQLiteDatabase database;

public PersonDao(Context context){
    sqLite=new SQLite(context);
    database=sqLite.getReadableDatabase();
}

public void add(UserBean.ResultBean data){
    ContentValues values = new ContentValues();
    values.put("name",data.getName());
    values.put("imageUrl",data.getImageUrl());
    values.put("summary",data.getSummary());
    database.insert("persons",null,values);
}

public void addAll(List<UserBean.ResultBean> datas){
    try {
        database.beginTransaction();
        for (UserBean.ResultBean bean:datas){
            add(bean);
        }
        database.setTransactionSuccessful();
    }finally {
        database.endTransaction();
    }
}

public void delAll(){
    database.delete("persons",null,null);
}

public List<UserBean.ResultBean> select(){
    ArrayList<UserBean.ResultBean> beans = new ArrayList<>();
    Cursor cursor = database.query("persons", null, null, null, null, null, null);
    while(cursor.moveToNext()){
        String name = cursor.getString(cursor.getColumnIndex("name"));
        String imageUrl = cursor.getString(cursor.getColumnIndex("imageUrl"));
        String summary = cursor.getString(cursor.getColumnIndex("summary"));
        UserBean.ResultBean bean=new UserBean.ResultBean();
        bean.setName(name);
        bean.setImageUrl(imageUrl);
        bean.setSummary(summary);
        beans.add(bean);
    }
    return beans;
}

tablayout的第一个页面

热播和xml

<com.handmark.pulltorefresh.library.PullToRefreshListView
        android:id="@+id/pullrefreash"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        ></com.handmark.pulltorefresh.library.PullToRefreshListView>

private PullToRefreshListView pull;
    private int mpage;
    private HomeAdapter adapter;
    private UserDao userDao;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.refragment,container,false);
    }

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    pull=view.findViewById(R.id.pullrefreash);
    mpage=1;

    /*adapter = new HomeAdapter(getActivity());
    pull.setAdapter(adapter);*/


    userDao=new UserDao(getContext());
    pull.setMode(PullToRefreshBase.Mode.BOTH);


    pull.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
        @Override
        public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
            mpage=1;
            loadData();
        }

        @Override
        public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
            loadData();
        }
    });
    loadData();
}

String dataUrl="http://172.17.8.100/movieApi/movie/v1/findHotMovieList?page=%d&count=10";

private void loadData() {
    Util.getInstance().getRequest(String.format(dataUrl,mpage), UserBean.class, new Util.Callback<UserBean>() {
        @Override
        public void onSuccess(UserBean o) {
            if(o==null||!o.isIntent()) {
                Toast.makeText(getActivity(),"错误请求",Toast.LENGTH_SHORT).show();
                pull.onRefreshComplete();
            }
            if(mpage==1){
                userDao.delAll();
            }
            userDao.addAll(o.getResult());
            List<UserBean.ResultBean> select = userDao.select();

            adapter = new HomeAdapter(getActivity(),select);
            pull.setAdapter(adapter);

            if(mpage==1){
                adapter.setDatas(o.getResult());
            }else{
                adapter.addDatas(o.getResult());
            }
            mpage++;

        }
    });
}

热播和正在运用同一个Adapter

private Context context;
    private List<UserBean.ResultBean> datas;

public HomeAdapter(Context context, List<UserBean.ResultBean> datas) {
    this.context = context;
    this.datas = datas;
}

public void setDatas(List<UserBean.ResultBean> mdatas) {
    //this.datas = datas;
    datas.clear();
    if(mdatas!=null){
        datas.addAll(mdatas);
    }
    notifyDataSetChanged();
}
public void addDatas(List<UserBean.ResultBean> mdatas) {
    //this.datas = datas;
    if(mdatas!=null){
        datas.addAll(mdatas);
    }
    notifyDataSetChanged();
}

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

@Override
public UserBean.ResultBean getItem(int position) {
    return datas.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder vh;
    if(convertView==null){
        convertView=LayoutInflater.from(context).inflate(R.layout.auto_item,parent,false);
        vh=new ViewHolder();
        vh.imageview=convertView.findViewById(R.id.image1);
        vh.textView1=convertView.findViewById(R.id.textView1);
        vh.textView2=convertView.findViewById(R.id.textView2);
        convertView.setTag(vh);
    }else{
        vh= (ViewHolder) convertView.getTag();
    }
    vh.textView1.setText(getItem(position).getName());
    vh.textView2.setText(getItem(position).getSummary());
    ImageLoader.getInstance().displayImage(getItem(position).getImageUrl(),vh.imageview);

    return convertView;
}
class ViewHolder{
    ImageView imageview;
    TextView textView1,textView2;
}

正在热播和xml

<com.handmark.pulltorefresh.library.PullToRefreshListView
        android:id="@+id/pullrefresh"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        ></com.handmark.pulltorefresh.library.PullToRefreshListView>

private PullToRefreshListView pullToRefresh;
    private int mpage;
    private HomeAdapter adapter;
    private PersonDao personDao;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.doingfragment,container,false);
    }

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    pullToRefresh = view.findViewById(R.id.pullrefresh);

    mpage=1;


    personDao=new PersonDao(getContext());
    pullToRefresh.setMode(PullToRefreshBase.Mode.BOTH);


    pullToRefresh.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
        @Override
        public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
            mpage=1;
            loadData();
        }

        @Override
        public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
            loadData();
        }
    });
    loadData();
}

String Url="http://172.17.8.100/movieApi/movie/v1/findReleaseMovieList?page=%d&count=10";

private void loadData() {
    Util.getInstance().getRequest(String.format(Url,mpage), UserBean.class, new Util.Callback<UserBean>() {
        @Override
        public void onSuccess(UserBean o) {
            if(o==null||!o.isIntent()) {
                Toast.makeText(getActivity(),"错误请求",Toast.LENGTH_SHORT).show();
                pullToRefresh.onRefreshComplete();
            }
            if(mpage==1){
                personDao.delAll();
            }
            personDao.addAll(o.getResult());
            List<UserBean.ResultBean> select = personDao.select();

            adapter = new HomeAdapter(getActivity(),select);
            pullToRefresh.setAdapter(adapter);

            if(mpage==1){
                adapter.setDatas(o.getResult());
            }else{
                adapter.addDatas(o.getResult());
            }
            mpage++;

        }
    });
}

图片的不同页面的加载和xml

<com.youth.banner.Banner
        android:id="@+id/banner"
        android:layout_width="0dp"
        android:layout_height="400dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        ></com.youth.banner.Banner>

private Banner banner;
    private int mpage=1;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.jifragment,container,false);
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);


    banner=view.findViewById(R.id.banner);

    banner.setBannerStyle(BannerConfig.NOT_INDICATOR);

    banner.setImageLoader(new ImageLoaderInterface<ImageView>() {


        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            UserBean.ResultBean datas= (UserBean.ResultBean) path;
            com.nostra13.universalimageloader.core.ImageLoader.getInstance()
                    .displayImage(datas.getImageUrl(),imageView);
        }

        @Override
        public ImageView createImageView(Context context) {
            ImageView imageView = new ImageView(context);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            return imageView;
        }
    });

    banner.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int i, float v, int i1) {

        }

        @Override
        public void onPageSelected(int i) {
            if(i == currentPageCount - 1) {
                mpage++;
                loadData();
            }
        }

        @Override
        public void onPageScrollStateChanged(int i) {

        }
    });

    loadData();
}

String Url="http://172.17.8.100/movieApi/movie/v1/findReleaseMovieList?count=10&page=";
private int currentPageCount;

private void loadData() {

    Util.getInstance().getRequest(Url + mpage, UserBean.class, new Util.Callback<UserBean>() {
        @Override
        public void onSuccess(UserBean o) {
            if(o == null || o.getResult() == null || o.getResult().size() == 0) {
                mpage = 1;
                loadData();
            } else {
                currentPageCount = o.getResult().size();
                banner.setImages(o.getResult());

                banner.start();
            }
        }
    });

}

第三个页面的Adapter banner+news的多条目

private Context context;
    private List<UserBean.ResultBean>  datas;

public MyFragment(Context context) {
    this.context = context;
    datas=new ArrayList<>();
}

public void setDatas(List<UserBean.ResultBean> mdatas) {
    //this.datas = datas;
    datas.clear();
    if(mdatas!=null){
        datas.addAll(mdatas);
    }
    notifyDataSetChanged();
}
public void addDatas(List<UserBean.ResultBean> mdatas) {
    //this.datas = datas;
    if(mdatas!=null){
        datas.addAll(mdatas);
    }
    notifyDataSetChanged();
}

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

@Override
public int getViewTypeCount() {
    return 2;
}

@Override
public int getItemViewType(int position) {
    return position==0?0:1;
}

@Override
public UserBean.ResultBean getItem(int position) {
    return datas.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if(getItemViewType(position)==0){
        BViewHolder bvh;
        if(convertView==null){
            convertView=LayoutInflater.from(context).inflate(R.layout.banner_item,parent,false);
            bvh=new BViewHolder(convertView);
        }else{
            bvh= (BViewHolder) convertView.getTag();
        }
        bvh.bindData(context,datas);

    }else{
        NViewHolder nvh;
        if(convertView==null){
            convertView=LayoutInflater.from(context).inflate(R.layout.new_item,parent,false);
            nvh=new NViewHolder(convertView);
        }else{
            nvh= (NViewHolder) convertView.getTag();
        }
        nvh.bindData(getItem(position));
        com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(getItem(position).getImageUrl(),nvh.imageView);
    }
    return convertView;
}
class NViewHolder{

    ImageView imageView;
    TextView textView1,textView2;

    public NViewHolder(View convertView){
        imageView=convertView.findViewById(R.id.image_banner);
        textView1=convertView.findViewById(R.id.text_name);
        textView2=convertView.findViewById(R.id.text_item);
        convertView.setTag(this);
    }

    public void bindData(UserBean.ResultBean item) {
        textView2.setText(item.getSummary());
        textView1.setText(item.getName());
    }
}
class BViewHolder{
    Banner banner;

    public BViewHolder(View convertView){
        banner=convertView.findViewById(R.id.banner1);
        convertView.setTag(this);
    }


    public void bindData(Context context, List<UserBean.ResultBean> datas) {

        banner.setBannerStyle(BannerConfig.NOT_INDICATOR);
        banner.setImageLoader(new ImageLoaderInterface<ImageView>() {
            @Override
            public void displayImage(Context context, Object path, ImageView imageView) {
                UserBean.ResultBean bean= (UserBean.ResultBean) path;
                com.nostra13.universalimageloader.core.ImageLoader.getInstance()
                        .displayImage(bean.getImageUrl(),imageView);
            }

            @Override
            public ImageView createImageView(Context context) {
                ImageView imageView = new ImageView(context);
                imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                return imageView;
            }
        });
        loadData();

   }
    String dataUrl="http://172.17.8.100/movieApi/movie/v1/findReleaseMovieList?count=10&page=1";

    private void loadData() {
        Util.getInstance().getRequest(dataUrl, UserBean.class, new Util.Callback<UserBean>() {
            @Override
            public void onSuccess(UserBean o) {
                banner.setImages(o.getResult());
                banner.start();
            }
        });
    }
}

fragmentTwo的频道管理

private TabLayout tabLayout;
    private TextView textView;
    private ViewPager viewPager;
    private List<ChannelBean> channelBeans=new ArrayList<>();
    private List<Fragment> fragments=new ArrayList<>();
    private MyBase myBase;
    private GoodsDao dao;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fargmenttwo,container,false);
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    tabLayout=view.findViewById(R.id.tab);
    textView=view.findViewById(R.id.text_jia);
    viewPager=view.findViewById(R.id.pager3);

    dao=new GoodsDao(getContext());
    textView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Gson gson = new Gson();
            String jsonArray = gson.toJson(channelBeans);
            Intent intent = new Intent(getActivity(), ChannelActivity.class);
            intent.putExtra(RESULT_JSON_KEY, jsonArray);
            startActivityForResult(intent, REQUEST_CODE);
        }
    });
    channelBeans.add(new ChannelBean("热点",true));
    channelBeans.add(new ChannelBean("关注",true));
    channelBeans.add(new ChannelBean("北京",true));
    channelBeans.add(new ChannelBean("房价",true));
    channelBeans.add(new ChannelBean("旅游",false));
    channelBeans.add(new ChannelBean("条目一",false));
    channelBeans.add(new ChannelBean("条目二",false));

    fragments.add(new One());
    fragments.add(new Two());
    fragments.add(new Three());
    fragments.add(new One());
    fragments.add(new Two());
    fragments.add(new Three());
    fragments.add(new One());

    for (int i=0;i<channelBeans.size();i++){
        if(channelBeans.get(i).isSelect()){

            tabLayout.addTab(tabLayout.newTab().setText(channelBeans.get(i).getName()));
        }
    }
    myBase = new MyBase(getChildFragmentManager());
    viewPager.setAdapter(myBase);
    tabLayout.setupWithViewPager(viewPager);

}

@Override
public 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);
        Gson gson = new Gson();
        Type type = new TypeToken<ArrayList<ChannelBean>>() {
        }.getType();
        channelBeans = gson.fromJson(stringExtra, type);


        for (int i = 0; i < channelBeans.size(); i++) {

            if (channelBeans.get(i).isSelect()) {
                try {
                    tabLayout.addTab(tabLayout.newTab().setText(channelBeans.get(i).getName()));
                    dao.add(channelBeans.get(i).getName(),channelBeans.get(i).isSelect());

                }catch (Exception e){

                }
            }
        }
        myBase.notifyDataSetChanged();
    }
}
private class MyBase extends FragmentPagerAdapter {

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

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

    @Override
    public int getCount() {
        int count=0;
        for (int i=0;i<channelBeans.size();i++){
            if(channelBeans.get(i).isSelect()){
                count++;
            }
        }
        return count;
    }

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

屏幕管理的数据库

private SQLite sqlite;
private SQLiteDatabase database;

public GoodsDao(Context context){
    sqlite=new SQLite(context);
    database=sqlite.getReadableDatabase();
}

public void add(String name, boolean ischecked){
    ContentValues values = new ContentValues();
    values.put("name",name);
    values.put("ischecked",ischecked);
    database.insert("goods",null,values);
}

屏幕管理的布局

<LinearLayout
        android:id="@+id/linear"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:orientation="horizontal"
        >
        <android.support.design.widget.TabLayout
            android:id="@+id/tab"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            ></android.support.design.widget.TabLayout>
        <TextView
            android:id="@+id/text_jia"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="+"
            android:textSize="40dp"
            />
    </LinearLayout>
<android.support.v4.view.ViewPager
    android:id="@+id/pager3"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toBottomOf="@id/linear"
    ></android.support.v4.view.ViewPager>

App的全局配置

try {
            ImageLoader.getInstance().init(
                    new ImageLoaderConfiguration.Builder(this)

                        .diskCache(new LruDiskCache(new File(Environment.getExternalStorageDirectory(),"image"),new Md5FileNameGenerator(),10*1024*1024))
                .memoryCacheSize(13)
                .defaultDisplayImageOptions(
                    new DisplayImageOptions.Builder()

                            .cacheOnDisk(true)
                            .cacheInMemory(true)
                            .showImageOnLoading(R.mipmap.ic_launcher)
                            .showImageOnFail(R.mipmap.ic_launcher)
                            .showImageForEmptyUri(R.mipmap.ic_launcher)
                            .bitmapConfig(Bitmap.Config.RGB_565)
                            .displayer(new RoundedBitmapDisplayer(4))
                            .build()
                ).build()
        );
    } catch (IOException e) {
        e.printStackTrace();
    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值