FindFragment.java 4.65 KB
package com.cnlive.goldenline.ui.fragment;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.cnlive.goldenline.R;
import com.cnlive.goldenline.ui.activity.SearchResultActivity;
import com.cnlive.goldenline.util.LogUtils;

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

import butterknife.BindView;

/**
 * FileName:com.cnlive.goldenline.ui.fragment.FindFragment.java
 * Created on 2017/2/17
 * Version V1.0
 */

public class FindFragment extends BaseFindFragment implements RadioGroup.OnCheckedChangeListener {

    private static final String TAG = "FindFragment";

    @BindView(R.id.fake_status_bar)
    protected View mFakeStatusBar;

    @BindView(R.id.fr_find_img_search)
    protected ImageView img_search;

    @BindView(R.id.fr_find_rg)
    protected RadioGroup rg;

    @BindView(R.id.fr_find_rb_my_subscription)
    protected RadioButton rb_my_subscription;

    @BindView(R.id.fr_find_rb_find)
    protected RadioButton rb_find;

    @BindView(R.id.controlBar)
    protected View controlBar;

//    @BindView(R.id.fr_find_rb_rankinglist)
//    protected RadioButton rb_rankinglist;

    @BindView(R.id.fr_find_content)
    protected FrameLayout layout_content;
    /**
     * 发现
     */
    private FindNestedFindFragment findFragment;
    /**
     * 订阅
     */
    private FindNestedSubscriptionFragment subscriptionFragment;
    /**
     * 排行榜
     */
    private FindNestedRankingFragment rankingFragment;

    private List<Fragment> fragmentList;
    private FragmentManager fManager;
    private FragmentTransaction ft;

    @Override
    protected void setupView() {
        super.setupView();
        LogUtils.LOGE(TAG + " setupView ");
        rg.setOnCheckedChangeListener(this);
        img_search.setOnClickListener(this);
        initFragment();

    }

    /**
     * 默认展示低2个fm
     */
    private void showView() {
        LogUtils.LOGE(TAG + " showView ");
        RadioButton rb = (RadioButton) rg.getChildAt(1);
        rb.setChecked(true);
    }

    //初始化fm
    private void initFragment() {
        findFragment = new FindNestedFindFragment();
        rankingFragment = new FindNestedRankingFragment();
        subscriptionFragment = new FindNestedSubscriptionFragment();
        fragmentList = new ArrayList<>();
        fragmentList.add(subscriptionFragment);
        fragmentList.add(findFragment);
//        fragmentList.add(rankingFragment);
        fManager = getChildFragmentManager();
        showView();
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.fragment_find;
    }

    @Override
    protected View[] getViews() {
        return new View[]{controlBar};
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.fr_find_img_search: //搜索
                SearchResultActivity.start(getActivity());
                break;
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        onPageChange();
        LogUtils.LOGE("选择的id====onCheckedChanged" + checkedId + "    " + group.getChildCount());
        for (int i = 0; i < rg.getChildCount(); i++) {
            RadioButton rb = (RadioButton) rg.getChildAt(i);
            LogUtils.LOGE("选择的id====" + checkedId + "    " + rb.getId());
            if (rb.getId() == checkedId) {
                rb.setEnabled(false);
                Fragment fragment = fragmentList.get(i);
                FragmentTransaction oft = getObtainFragmentTransaction(i);
                if (!fragment.isAdded()) {
                    oft.add(R.id.fr_find_content, fragment);
                    oft.commit();
                    LogUtils.LOGE("onCheckedChanged====" + i);
                }
                showTab(i);
            } else {
                rb.setEnabled(true);
            }
        }


    }

    private void showTab(int idex) {
        for (int i = 0; i < fragmentList.size(); i++) {
            FragmentTransaction transaction = getObtainFragmentTransaction(idex);
            Fragment fragment = fragmentList.get(i);
            if (i == idex) {
                transaction.show(fragment);
            } else {
                transaction.hide(fragment);
            }
            transaction.commit();
            LogUtils.LOGE("显示第几个fm===" + idex);
        }

    }

    private FragmentTransaction getObtainFragmentTransaction(int idex) {
        ft = fManager.beginTransaction();
        return ft;

    }

}