【GT-安卓应用开发之ViewPager+Fragment实现TAB切换】

前言:话不多说,先看效果

【GT-安卓应用开发之ViewPager+Fragment实现TAB切换】_第1张图片

步骤:

         Step1,界面布局




    

        

        
    

    

        

            

            
        
        
    

        Step2,定义碎片列表,初始化ViewPager

Coursefragment sFragment = new Coursefragment();
fragmentList = new ArrayList();
fragmentList.add(sFragment);
DynamicFragment fFragment = new DynamicFragment();
fragmentList.add(fFragment);
adapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), fragmentList);
viewPager.setAdapter(adapter);
viewPager.setCurrentItem(0);
viewPager.setOnPageChangeListener((ViewPager.OnPageChangeListener) new MyOnPageChangeListener());

        Step3,定义MyOnPageChangeListener

private class MyOnPageChangeListener implements ViewPager.OnPageChangeListener{

    @Override
    public void onPageScrollStateChanged(int arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onPageSelected(int arg0) {
        // TODO Auto-generated method stub
        switch (arg0) {
            case 0:
                btnCourse.setTextColor(Color.parseColor("#000000"));
                btnDynamic.setTextColor(Color.parseColor("#919191"));
                break;
            case 1:
                btnCourse.setTextColor(Color.parseColor("#919191"));
                btnDynamic.setTextColor(Color.parseColor("#000000"));
                break;
        }
    }
}

        Step4,GroupButton与ViewPager关联

group.setOnCheckedChangeListener((RadioGroup.OnCheckedChangeListener) new MyCheckChangeListener());
private class MyCheckChangeListener implements RadioGroup.OnCheckedChangeListener{

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        // TODO Auto-generated method stub
        switch (checkedId) {
            case R.id.btnCourse:
                viewPager.setCurrentItem(0,false);
                break;
            case R.id.btnDynamic:
                viewPager.setCurrentItem(1,false);
                break;
        }
    }
}

        Step5,碎片实现(两个碎片功能相近,这里只贴出课程收藏碎片的代码)

public class Coursefragment extends BaseFragment {
    @BindView(R.id.ywjl_lv)
    LRecyclerView ywjlLv;
    private CourseAdapter mCollectAdapter;
    private LRecyclerViewAdapter mLRecyclerViewAdapter;
    private SharedPreferencesHelper shared;
    private String token, userId;
    private CommitParam commitParam;
    private Call data;
    private Map map;
    /**
     * 服务器端一共多少条数据
     */
    private static final int TOTAL_COUNTER = 1000;//如果服务器没有返回总数据或者总页数,这里设置为最大值比如10000,什么时候没有数据了根据接口返回判断

    /**
     * 每一页展示多少条数据
     */
    private static final int REQUEST_COUNT = 10;

    /**
     * 已经获取到多少条数据了
     */
    private static int mCurrentCounter = 0;
    private boolean isLoadMore = false;
    int page = 0;
    private ArrayList mList;
    private WeakHandler mHandler = new WeakHandler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:

                case 1:
                    if (!isLoadMore) {
                        page = 0;
                    }
                    collectList(String.valueOf(page));
                    break;

                case -3:
                    ywjlLv.refreshComplete(REQUEST_COUNT);
                    ywjlLv.setOnNetWorkErrorListener(new OnNetWorkErrorListener() {
                        @Override
                        public void reload() {
                        }
                    });

                    break;
                default:
                    break;
            }
        }
    };
    @Override
    protected boolean isLazyLoad() {
        return false;
    }

    @Override
    protected int getContentLayout() {
        return R.layout.fragment_collect;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {
        shared = new SharedPreferencesHelper(getActivity(), "user");
        token = shared.getSharedPreference("token", "").toString();
        userId = shared.getSharedPreference("userid", 0).toString();
        mList = new ArrayList();
        MyGridLayoutManager manager = new MyGridLayoutManager(MyApplication.getContextObject(), 2, LinearLayoutManager.VERTICAL, false);
        ywjlLv.setLayoutManager(manager);
        mCollectAdapter = new CourseAdapter(MyApplication.getContextObject(), R.layout.new_course_item, "sckc");
        mLRecyclerViewAdapter = new LRecyclerViewAdapter(mCollectAdapter);
        ywjlLv.setAdapter(mLRecyclerViewAdapter);
        mHandler.sendEmptyMessage(1);

    }

    @Override
    protected void setListener() {
        ywjlLv.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                isLoadMore = false;
                mHandler.sendEmptyMessage(1);
            }
        });
        ywjlLv.setLoadMoreEnabled(false);
        mLRecyclerViewAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(MyApplication.getContextObject(), CoursePlayActivity.class);
                intent.putExtra("csid", mList.get(position).getId());
                intent.putExtra("title", mList.get(position).getCsName());
                startActivity(intent);
            }
        });
    }
    private void collectList(String page) {
        commitParam = new CommitParam();
        commitParam.setToken(token);
        map = commitParam.getData();
        Call data = RetrofitBean.getApi2().getDataCourse("get_course_collects", map);
        data.enqueue(new SimpleCallBack("课程收藏列表") {
            @Override
            protected void showData(int i, String json) {
                super.showData(i, json);
                Gson gson = new Gson();
                Course yg = gson.fromJson(json, Course.class);
                if (!isLoadMore) {
                    mList.clear();
                    mCollectAdapter.clear();
                    mCurrentCounter = 0;
                }
                mList.addAll(yg.getResult());
                mCollectAdapter.addAll(yg.getResult());
                ywjlLv.refreshComplete(REQUEST_COUNT);
            }

            @Override
            protected void showData(String msg) {
                if (!isLoadMore) {
                    mList.clear();
                    mCollectAdapter.setDataList(mList);
                    //禁用自动加载更多功能
                    mCurrentCounter = 0;
                } else {
                    ywjlLv.setNoMore(true);
                    Toast.makeText(MyApplication.getContextObject(), "暂无更多数据", Toast.LENGTH_SHORT).show();
                }
                ywjlLv.refreshComplete(REQUEST_COUNT);
            }
        });
    }
}

 

 

 

 

 

你可能感兴趣的:(【GT-安卓应用开发之ViewPager+Fragment实现TAB切换】)