Android :tablayout的各种嵌套

Android :tablayout的各种嵌套_第1张图片
Android :tablayout的各种嵌套_第2张图片
Android :tablayout的各种嵌套_第3张图片
需要用到的权限


需要用到的jar包

com.android.support:design:28.0.0
com.google.code.gson:gson:2.8.5
com.nostra13.universalimageloader:universal-image-loader:1.9.5
com.github.bumptech.glide:glide:4.8.0

implementation project(':pulltolibrary')

MainActivity

package com.example.administrator.yuekaomoni;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.example.administrator.yuekaomoni.fragment.ShowFragment;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView ShowMovie;
    private DrawerLayout MyDra;
    private FragmentManager manager;
    private ShowFragment showFragment;
    private FrameLayout FragmentLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
    }
    private void initData() {
        showFragment = new ShowFragment();
    }
    private void initView() {
        ShowMovie = (TextView) findViewById(R.id.ShowMovie);
        ShowMovie.setOnClickListener(this);
        MyDra = (DrawerLayout) findViewById(R.id.MyDra);
        manager = getSupportFragmentManager();
    }

    @Override
    public void onClick(View v) {
        manager.beginTransaction().replace(R.id.FragmentLayout, showFragment).commit();
        MyDra.closeDrawers();
    }
}

Activity的xml 侧滑菜单




    

        
    

    

        

        
    


解析工具

package com.example.administrator.yuekaomoni.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpUtil {

    public static String getStr(String mUrl){
        try {
            URL url = new URL(mUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            InputStream stream = connection.getInputStream();
            String jsons = getStream(stream);
            return jsons;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    private static String getStream(InputStream stream) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
        StringBuffer sb = new StringBuffer();
        String con;
        while((con = reader.readLine())!=null){
            sb.append(con);
        }
        reader.close();
        return sb.toString();
    }
}

bean,因为是两个接口所以写了两个bean包

package com.example.administrator.yuekaomoni.bean;

import java.util.List;

public class MyData {

    private String message;
    private String status;
    private List result;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public List getResult() {
        return result;
    }

    public void setResult(List result) {
        this.result = result;
    }

    public static class ResultBean {


        private boolean followMovie;
        private int id;
        private String imageUrl;
        private String name;
        private int rank;
        private String summary;

        public boolean isFollowMovie() {
            return followMovie;
        }

        public void setFollowMovie(boolean followMovie) {
            this.followMovie = followMovie;
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getImageUrl() {
            return imageUrl;
        }

        public void setImageUrl(String imageUrl) {
            this.imageUrl = imageUrl;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getRank() {
            return rank;
        }

        public void setRank(int rank) {
            this.rank = rank;
        }

        public String getSummary() {
            return summary;
        }

        public void setSummary(String summary) {
            this.summary = summary;
        }
    }
}

第二个bean

package com.example.administrator.yuekaomoni.bean;

import java.util.List;

public class WeiShangYing {

    private String message;
    private String status;
    private List result;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public List getResult() {
        return result;
    }

    public void setResult(List result) {
        this.result = result;
    }

    public static class ResultBean {


        private boolean followMovie;
        private int id;
        private String imageUrl;
        private String name;
        private int rank;
        private long releaseTime;
        private String releaseTimeShow;
        private String summary;

        public boolean isFollowMovie() {
            return followMovie;
        }

        public void setFollowMovie(boolean followMovie) {
            this.followMovie = followMovie;
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getImageUrl() {
            return imageUrl;
        }

        public void setImageUrl(String imageUrl) {
            this.imageUrl = imageUrl;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getRank() {
            return rank;
        }

        public void setRank(int rank) {
            this.rank = rank;
        }

        public long getReleaseTime() {
            return releaseTime;
        }

        public void setReleaseTime(long releaseTime) {
            this.releaseTime = releaseTime;
        }

        public String getReleaseTimeShow() {
            return releaseTimeShow;
        }

        public void setReleaseTimeShow(String releaseTimeShow) {
            this.releaseTimeShow = releaseTimeShow;
        }

        public String getSummary() {
            return summary;
        }

        public void setSummary(String summary) {
            this.summary = summary;
        }
    }
}

适配器

package com.example.administrator.yuekaomoni.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.administrator.yuekaomoni.R;
import com.example.administrator.yuekaomoni.bean.MyData;

import java.util.ArrayList;

public class MyAdapter extends BaseAdapter {
    private Context context;
    private ArrayList datas;

    public MyAdapter(Context context, ArrayList datas) {
        this.context = context;
        this.datas = datas;
    }

    public ArrayList getDatas() {
        return datas;
    }

    public void setDatas(ArrayList datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

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

    @Override
    public Object 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 holder = null;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.mylayout, null);
            holder = new ViewHolder();
            holder.iv_img = convertView.findViewById(R.id.Img_Show);
            holder.tv_title = convertView.findViewById(R.id.My_Title);
            holder.tv_content = convertView.findViewById(R.id.My_content);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Glide.with(context).load(datas.get(position).getImageUrl()).into(holder.iv_img);
        holder.tv_title.setText(datas.get(position).getName());
        holder.tv_content.setText(datas.get(position).getSummary());
        return convertView;
    }

    class ViewHolder {
        ImageView iv_img;
        TextView tv_title;
        TextView tv_content;
    }
}

xml的包




    

        

        

            

            
        
    

fragment的适配器

package com.example.administrator.yuekaomoni.adapter;

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

import java.util.ArrayList;

public class MyFragmentAdapter extends FragmentPagerAdapter {

    private ArrayList datas;

    public MyFragmentAdapter(FragmentManager fm, ArrayList datas) {
        super(fm);
        this.datas = datas;
    }

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

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

未上映里面的适配器

package com.example.administrator.yuekaomoni.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.administrator.yuekaomoni.R;
import com.example.administrator.yuekaomoni.bean.WeiShangYing;

import java.util.ArrayList;

public class MyWeiShang extends BaseAdapter {
    private Context context;
    private ArrayList datas;

    public MyWeiShang(Context context, ArrayList datas) {
        this.context = context;
        this.datas = datas;
    }

    public ArrayList getDatas() {
        return datas;
    }

    public void setDatas(ArrayList datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

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

    @Override
    public Object 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 holder = null;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.mylayout, null);
            holder = new ViewHolder();
            holder.iv_img = convertView.findViewById(R.id.Img_Show);
            holder.tv_title = convertView.findViewById(R.id.My_Title);
            holder.tv_content = convertView.findViewById(R.id.My_content);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Glide.with(context).load(datas.get(position).getImageUrl()).into(holder.iv_img);
        holder.tv_title.setText(datas.get(position).getName());
        holder.tv_content.setText(datas.get(position).getSummary());
        return convertView;
    }

    class ViewHolder {
        ImageView iv_img;
        TextView tv_title;
        TextView tv_content;
    }
}

//未上映的Fragment的适配器

package com.example.administrator.yuekaomoni.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 java.util.ArrayList;

public class MyTabFragmentAdapter extends FragmentPagerAdapter {
    private ArrayList datas;
    private ArrayList titles;

    public MyTabFragmentAdapter(FragmentManager fm, ArrayList datas, ArrayList titles) {
        super(fm);
        this.datas = datas;
        this.titles = titles;
    }

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

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

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

展示页面的fragment

package com.example.administrator.yuekaomoni.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;

import com.example.administrator.yuekaomoni.R;
import com.example.administrator.yuekaomoni.adapter.MyFragmentAdapter;

import java.util.ArrayList;

/**
 * A simple {@link Fragment} subclass.
 */
public class ShowFragment extends Fragment {


    private RadioGroup My_Rg;
    private ViewPager Show_vp;
    private MyFragmentAdapter adapter;
    private ArrayList datas = new ArrayList();
    private MovieFragment movieFragment;
    private CinemaFragment cinemaFragment;
    private VipFragment vipFragment;
    private SetFragment setFragment;
    private View v;
    private MyFragmentAdapter myFragmentAdapter;
    private ArrayList lists = new ArrayList<>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        v = inflater.inflate(R.layout.fragment_show, null);
        initView();
        initData();
        myFragmentAdapter = new MyFragmentAdapter(getActivity().getSupportFragmentManager(), datas);
        Show_vp.setAdapter(myFragmentAdapter);

        My_Rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                //进行判断
                if (checkedId == R.id.rb1) {
                    Show_vp.setCurrentItem(0);
                } else if (checkedId == R.id.rb2) {
                    Show_vp.setCurrentItem(1);
                } else if (checkedId == R.id.rb3) {
                    Show_vp.setCurrentItem(2);
                } else {
                    Show_vp.setCurrentItem(3);
                }
            }
        });
        Show_vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                //进行判断
                if (i == 0) {
                    My_Rg.check(R.id.rb1);
                } else if (i == 1) {
                    My_Rg.check(R.id.rb2);
                } else if (i == 2) {
                    My_Rg.check(R.id.rb3);
                } else {
                    My_Rg.check(R.id.rb4);
                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
        return v;
    }

    //初始化fragment页面
    private void initData() {
        movieFragment = new MovieFragment();
        cinemaFragment = new CinemaFragment();
        vipFragment = new VipFragment();
        setFragment = new SetFragment();
        datas.add(movieFragment);
        datas.add(cinemaFragment);
        datas.add(vipFragment);
        datas.add(setFragment);
    }

    private void initView() {
        My_Rg = (RadioGroup) v.findViewById(R.id.My_Rg);
        Show_vp = (ViewPager) v.findViewById(R.id.Show_vp);

    }

}

展示fragment的xml




    

        

        

        

        
    

    

电影的fragment

package com.example.administrator.yuekaomoni.fragment;


import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.administrator.yuekaomoni.R;
import com.example.administrator.yuekaomoni.adapter.MyTabFragmentAdapter;

import java.util.ArrayList;

/**
 * A simple {@link Fragment} subclass.
 */
public class MovieFragment extends Fragment {

    private TabLayout My_Tab;
    private ViewPager My_Vp;
    private ArrayList datas = new ArrayList<>();
    private ArrayList lists = new ArrayList<>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // 引入视图
        View v = inflater.inflate(R.layout.fragment_movie, null);
        //判断为空才初始化数据
        if (datas.size() <= 0) {
            initData();
        }
        ;
        initView(v);

        return v;
    }

    private void initData() {

        datas.add(new HotVideoFragment());
        datas.add(new NowShowingFragment());
        datas.add(new RaoulRuizFragment());
        lists.add("热门电影");
        lists.add("正在上映");
        lists.add("即将上映");

    }

    private void initView(View v) {
        My_Tab = (TabLayout) v.findViewById(R.id.My_Tab);
        My_Vp = (ViewPager) v.findViewById(R.id.My_Vp);
        My_Vp.setAdapter(new MyTabFragmentAdapter(getChildFragmentManager(), datas, lists));
        My_Tab.setupWithViewPager(My_Vp);
    }
}

电影的xml




    

    



热门电影的fragment

package com.example.administrator.yuekaomoni.fragment;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

import com.example.administrator.yuekaomoni.R;
import com.example.administrator.yuekaomoni.adapter.MyAdapter;
import com.example.administrator.yuekaomoni.bean.MyData;
import com.example.administrator.yuekaomoni.util.HttpUtil;
import com.example.pulltolibrary.PullToRefreshBase;
import com.example.pulltolibrary.PullToRefreshListView;
import com.google.gson.Gson;

import java.util.ArrayList;

/**
 * A simple {@link Fragment} subclass.
 */
public class HotVideoFragment extends Fragment {
    private ArrayList datas = new ArrayList<>();
    private String mUrl = "http://172.17.8.100/movieApi/movie/v1/findHotMovieList?userId=18&sessionId=15320748258726&page=1&count=10";
    private PullToRefreshListView Pull_To;
    private MyAdapter adapter;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0) {
                datas.addAll((ArrayList) msg.obj);
                //刷新适配器
                adapter.notifyDataSetChanged();
                //停止刷新动画
                Pull_To.onRefreshComplete();
            }
        }
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // 引入视图
        View v = inflater.inflate(R.layout.fragment_hot_video, null);
        initView(v);
        adapter = new MyAdapter(getActivity(), datas);
        Pull_To.setAdapter(adapter);
        initData();
        //刷新监听事件
        setListener();
        return v;
    }

    private void setListener() {
        Pull_To.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase refreshView) {
                datas.clear();
                initData();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase refreshView) {
                Toast.makeText(getActivity(), "没有更多数据了", Toast.LENGTH_SHORT).show();

            }
        });
    }

    private void initData() {
        //创建子线程刷新数据
        new Thread(new Runnable() {
            @Override
            public void run() {
                String jsonstr = HttpUtil.getStr(mUrl);
                Gson gson = new Gson();
                MyData myData = gson.fromJson(jsonstr, MyData.class);
                handler.sendMessage(handler.obtainMessage(0, myData.getResult()));
            }
        }).start();

    }

    private void initView(View v) {
        Pull_To = (PullToRefreshListView) v.findViewById(R.id.Pull_To);
        //设置上下拉滑动
        Pull_To.setMode(PullToRefreshBase.Mode.BOTH);
    }
}

热门电影的xml




    


正在上映的fragment

package com.example.administrator.yuekaomoni.fragment;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.example.administrator.yuekaomoni.R;
import com.example.administrator.yuekaomoni.adapter.MyWeiShang;
import com.example.administrator.yuekaomoni.bean.WeiShangYing;
import com.example.administrator.yuekaomoni.util.HttpUtil;
import com.example.pulltolibrary.PullToRefreshBase;
import com.example.pulltolibrary.PullToRefreshListView;
import com.google.gson.Gson;

import java.util.ArrayList;

/**
 * A simple {@link Fragment} subclass.
 */
public class NowShowingFragment extends Fragment {
    private ArrayList datas = new ArrayList<>();
    private String mUrl = "http://172.17.8.100/movieApi/movie/v1/findReleaseMovieList?userId=18&sessionId=15320748258726&count=10&page=";
    private MyWeiShang adapter;
    private int index = 1;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0) {
                datas.addAll((ArrayList) msg.obj);
                //刷新适配器
                adapter.notifyDataSetChanged();
                //停止刷新动画
                PPPP.onRefreshComplete();
            }
        }
    };
    private PullToRefreshListView PPPP;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // 引入视图
        View v = inflater.inflate(R.layout.fragment_now_showing, null);
        initView(v);
        adapter = new MyWeiShang(getActivity(), datas);
        PPPP.setAdapter(adapter);
        initData(index);
        //刷新监听事件
        setListener();
        return v;
    }

    private void setListener() {
        PPPP.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase refreshView) {
                index = 1;
                datas.clear();
                initData(index);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase refreshView) {
                index++;
                initData(index);
            }
        });
    }

    private void initData(final int index) {
        //创建子线程刷新数据
        new Thread(new Runnable() {
            @Override
            public void run() {
                String jsonstr = HttpUtil.getStr(mUrl + index);
                Gson gson = new Gson();
                WeiShangYing ying = gson.fromJson(jsonstr, WeiShangYing.class);
                handler.sendMessage(handler.obtainMessage(0, ying.getResult()));
            }
        }).start();
    }

    private void initView(View v) {
        PPPP = (PullToRefreshListView) v.findViewById(R.id.PPPP);
        //设置上下拉滑动
        PPPP.setMode(PullToRefreshBase.Mode.BOTH);
    }
}

正在上映的exml




    


剩下的即将上映也就不用写了和其他的都一样,主要就是布局嵌套的问题
以上

你可能感兴趣的:(Android :tablayout的各种嵌套)