简体   繁体   English

向后压入片段

[英]Handle back press into the Fragment

in my fragment i want to setVisibility for one element of my layout on back press. 在我的片段中,我想在背面按一下setVisibility布局的一个元素。

for example into the fragment i want this: 例如进入片段我想要这样:

 if(list.getVisibility() == View.GONE){

            list.setVisibility(View.VISIBLE);
            text.setVisibility(View.GONE);

  }

But how i can handle the back press event into the fragment? 但是我如何处理片段的反向新闻事件? and set this visibility? 并设置此可见性?

UPDATE: 更新:

In my FragmentActivity i have: 在我的FragmentActivity中,我有:

adapter =  new ViewPagerAdapterEventi(getSupportFragmentManager(),Titles,Numboftabs,mE1,mE2);

        // Assigning ViewPager View and setting the adapter
        pager = (ViewPager) findViewById(R.id.pager);
        pager.setAdapter(adapter);

        // Assiging the Sliding Tab Layout View
        tabs = (SlidingTabLayout) findViewById(R.id.tabs);
        tabs.setDistributeEvenly(true); // To make the Tabs Fixed set this true, This makes the tabs Space Evenly in Available width

        // Setting Custom Color for the Scroll bar indicator of the Tab View
        tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
            @Override
            public int getIndicatorColor(int position) {
                return getResources().getColor(R.color.tabsScrollColor);
            }
        });

        // Setting the ViewPager For the SlidingTabsLayout
        tabs.setViewPager(pager);

And into the Adaper: 并进入Adaper:

public class ViewPagerAdapter extends FragmentStatePagerAdapter {

....

 @Override
    public Fragment getItem(int position) {


        if (position == 0) // if the position is 0 we are returning the First tab
        {


            check = position;
            Fragment1 tab1 = new Fragment1();

            Bundle args = new Bundle();
            args.putString("json", mEv1.toString());
            tab1.setArguments(args);




            return tab1;
        } else {
            Fragment2 tab2 = new Fragment2();
            check = position;
            Bundle args = new Bundle();
            args.putString("json", mEv2.toString());
            tab2.setArguments(args);
            return tab2;
        }


    }

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

    // This method return the Number of tabs for the tabs Strip

    @Override
    public int getCount() {
        return NumbOfTabs;
    }

}

Please override method in FragmentActivity 请重写FragmentActivity中的方法

@Override
        public void onBackPressed() {
            // TODO Auto-generated method stub
            super.onBackPressed();
            Fragment fragment = getSupportFragmentManager().findFragmentByTag("FRAGMENT_TAG");
            fragment.setUserVisibility();
        }

You have to handle this in your Activity 您必须在活动中处理

@Override
public void onBackPressed() {
    super.onBackPressed();
    MyCustomFragment fragment = (MyCustomFragment) getFragmentManager().findFragmentByTag("TAG_I_USED_WHEN_COMMITTING_FRAGMENT");
    if(fragment != null) {
        fragment.onBackPressed();
    }
}

And create a public method in MyCustomFragment called onBackPressed() to handle everything that should happen when the back button is pressed. 并在MyCustomFragment中创建一个名为onBackPressed()的公共方法,以处理按下后退按钮时应该发生的所有事情。

note: Don't forget that your FragmentManager can also handle the onBackPressed event and remove your fragment from the back stack for example. 注意:不要忘记,例如, FragmentManager也可以处理onBackPressed事件并从后堆栈中删除片段。 This depends on how you've added the fragment 这取决于您如何添加片段

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM