findFragmentByTag如何在父片段中工作以了解ViewPagerAdapter当前显示哪个子片段?

时间:2015-12-15 09:50:39

标签: android android-fragments fragmentpageradapter android-tablayout

我的MainFragment就像

public class MainTabFragment extends Fragment {

public static int position_child_tab = 0, three_childs_tab_position = 0;
static int count = -1;
int position_tab = 0;
Bundle args;
public static MyTabLayout myTabLayout;
private static MainTabFragment sMainTabFragment;
public static NonSiwpablePager pager;
private Fragment mFragment;
public MainTabFragment() {
    // Required empty public constructor
}

public static MainTabFragment getInstance() {
    return sMainTabFragment;
}

/**
 * Use this factory method to create a new instance of
 * this fragment using the provided parameters.
 *
 * @param param1 Parameter 1.
 * @param param2 Parameter 2.
 * @return A new instance of fragment MainTabFragment.
 */
// TODO: Rename and change types and number of parameters
public static MainTabFragment newInstance(String param1, String param2) {

    return new MainTabFragment();
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    args = getArguments();
    if (args != null && args.containsKey("pos_next"))
        position_tab = args.getInt("pos_next");
    if (args != null && args.containsKey("pos_end"))
        position_child_tab = args.getInt("pos_end");
    if (position_child_tab != 3) {
        three_childs_tab_position = position_child_tab;
    } else {
        three_childs_tab_position = 0;
    }
    args = new Bundle();
    args.putInt("pos_end", position_child_tab);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.main_tab_fragment, container, false);
    myTabLayout = (MyTabLayout) view.findViewById(R.id.mainTabLayout);
    pager = (NonSiwpablePager) view.findViewById(R.id.pager);

    ViewPagerAdapter mAdapter = getViewPagerAdapter();
    pager.setAdapter(mAdapter);
    pager.setOffscreenPageLimit(4);
    myTabLayout.setupWithViewPager(pager);
    for (int i = 0; i < mAdapter.getCount(); i++) {
        View customView = mAdapter.getCustomeView(getActivity(), i);
        myTabLayout.getTabAt(i).setCustomView(customView);
    }
    pager.setCurrentItem(position_tab);
    pager.setOffscreenPageLimit(3);
   // changeTab();
    myTabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            pager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });
    Fragment fragment = getFragmentManager().findFragmentByTag("TOP");
    if (fragment != null){
        pager.setCurrentItem(position_tab);
    }
    Log.e("Fragment", fragment + "" +pager.getCurrentItem());
    //
    return view;
}


public void changeTab(){
    pager.getCurrentItem();
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    myTabLayout.getTabAt(position_tab).getCustomView().setSelected(true);
}

public void setCurrentItem(int item) {
    pager.setCurrentItem(item);
}

private ViewPagerAdapter getViewPagerAdapter() {
    ViewPagerAdapter mAdapter = new ViewPagerAdapter(getChildFragmentManager());
    String title_arr[] = {"ADVISORY", "TOP ADVISORS", "EXPERT VIEW"};
    for (int i = 0; i < title_arr.length; i++) {
        Map<String, Object> map = new Hashtable<>();
        map.put(ViewPagerAdapter.KEY_TITLE, title_arr[i]);
        if (i == 0) {
            map.put(ViewPagerAdapter.KEY_FRAGMENT, AdvisoryPagerFragment.newInstance());
            AdvisoryPagerFragment.newInstance().setTargetFragment(this, getTargetRequestCode());
        } else if (i == 1) {
            map.put(ViewPagerAdapter.KEY_FRAGMENT, TopAdvisoryPagerFragment.newInstance());
            TopAdvisoryPagerFragment.newInstance().setTargetFragment(this, getTargetRequestCode());
        } else if (i == 2) {
            map.put(ViewPagerAdapter.KEY_FRAGMENT, ExperViewPagerFragment.newInstance());
            ExperViewPagerFragment.newInstance().setTargetFragment(this, getTargetRequestCode());
        }
        mAdapter.addFragmentAndTitle(map);

    }
    return mAdapter;
}

public static class ViewPagerAdapter extends FragmentStatePagerAdapter {
    private static final String KEY_TITLE = "fragment_title";
    private static final String KEY_FRAGMENT = "fragment";
    boolean abc = false;
    private int[] drawables = new int[]{R.drawable.advisory_selector, R.drawable.top_advisors_selector, R.drawable.expertview_selector};
    private List<Map<String, Object>> maps = new ArrayList<>();

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

    public View getCustomeView(Context context, int pos) {
        View mView = LayoutInflater.from(context).inflate(R.layout.custom_tab_view, null);
        TextView mTextView = (TextView) mView.findViewById(R.id.textView);
        mTextView.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/ufonts.com_cambria.ttf"));
        ImageView mImageView = (ImageView) mView.findViewById(R.id.imageView2);
        mImageView.setTag(pos);

        /*if(count >0)
        {
            Toast.makeText(context,"Count Is "+count,Toast.LENGTH_SHORT).show();
            mImageView =  (ImageView) mImageView.getTag(0);
            mImageView.setSelected(false);
        }
        */
        mImageView.setImageResource(drawables[pos]);
        mTextView.setText(getPageTitle(pos));
        return mView;
    }

    public void addFragmentAndTitle(Map<String, Object> map) {
        maps.add(map);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return (CharSequence) maps.get(position).get(KEY_TITLE);
    }

    @Override
    public Fragment getItem(int position) {
        Log.e("Fragmentss" ,(Fragment) maps.get(position).get(KEY_FRAGMENT) +"");
        return (Fragment) maps.get(position).get(KEY_FRAGMENT);
    }

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

}


}

在这里,我可以使用findFragmentByTag看到此片段可见。我想知道如何为使用Fragment添加的FragmentPagerAdapter的孩子使用相同的内容。

Fragment fragment = getFragmentManager().findFragmentByTag("TOP");
    if (fragment != null){
    Log.e("Fragment", fragment + "" +pager.getCurrentItem());        }

所以现在我想做同样的事情并从FragmentPagerAdapterAdvisoryPagerFragment TopAdvisoryPagerFragmentExperViewPagerFragmentDeviceInfo.java获得上面提到的当前所选片段的名称。 / p>

1 个答案:

答案 0 :(得分:0)

试试这个

Fragment fragment = getFragmentManager().findFragmentByTag("TOP");

if(fragment instanceOf Fragment1){
// Do something
}else if(fragment instanceOf Fragment2){
// Do something
}else{
// Do something
}