大家好,我有一个带按钮的片段。我想在单击按钮时打开fragment2。
最好的解决方案是什么?
您能为此举例吗?
在XML片段1中:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/background"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="116dp"
        android:background="#0000"
        android:drawableTop="@drawable/autobus"
        android:onClick="OPENFRAGMENT"
        android:text="ooo" />


在fragment1中:

public class Fragment1 extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

     View info = inflater.inflate(R.layout.info, container, false);

    return info;
}

public void OPENFRAGMENT(View view)
{

}


在fragment2中:

public class Fragment2 extends Fragment{

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

         View frag = inflater.inflate(R.layout.frag2, container, false);
         return frag;
 }
}


主要活动:

 public class MainActivity extends SherlockFragmentActivity  {


    ViewPager Tab;
    TabPagerAdapter TabAdapter;
    ActionBar actionBar;
    public boolean StatusConnection = false;
    public int thread = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        TabAdapter = new TabPagerAdapter(getSupportFragmentManager());
        Tab = (ViewPager)findViewById(R.id.pager);
        Tab.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {

                    @Override
                    public void onPageSelected(int position) {
                        actionBar.setSelectedNavigationItem(
                                position);
                        Tab.setCurrentItem(position);
                    }

                });
        Tab.setAdapter(TabAdapter);

        actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        ActionBar.TabListener tabListener = new ActionBar.TabListener(){

            @Override
            public void onTabReselected(ActionBar.Tab arg0,FragmentTransaction arg1) {
                // TODO Auto-generated method stub

            }
            @Override
            public void onTabSelected(ActionBar.Tab arg0,FragmentTransaction arg1) {
                // TODO Auto-generated method stub
                Tab.setCurrentItem(arg0.getPosition());
            }
            @Override
            public void onTabUnselected(ActionBar.Tab arg0,FragmentTransaction arg1) {
                // TODO Auto-generated method stub

            }
        };

        actionBar.addTab(actionBar.newTab().setText("A").setTabListener(tabListener));
        actionBar.addTab(actionBar.newTab().setText("B").setTabListener(tabListener));
        actionBar.addTab(actionBar.newTab().setText("C").setTabListener(tabListener));

 @Override
    public Fragment getItem(int i) {
        switch (i) {
            case 0:
                return new Info();
            case 1:
                return new B();
            case 2:
                return  new  C();


activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/pager"
  android:layout_width="match_parent"
  android:layout_height="match_parent" />


先感谢您!

最佳答案

首先,您必须使用以下代码从第一个片段上的按钮获取视图:

btnChFrag = info.findViewById(R.id.button1);


(信息是您的片段布局的视图)

那么您需要在该按钮上设置onclicklistener

btnChFrag.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        Fragment2 newFragment = new Fragment2();
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.replace(R.id.container, newFragment);
        ft.commit();
}


在MainActivity视图上使用容器更改R.id.container

所以您在fragment1上的代码将是:

public class Fragment1 extends Fragment {

private Button btnChFrag

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View info = inflater.inflate(R.layout.info, container, false);
    btnChFrag = info.findViewById(R.id.button1);
    //set button onclicklistener here

    return info;
}


- - -编辑 - - -

对不起,我完全忘了你想用按钮来做,我不好

这是fragment1的代码

public class Fragment1 extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View info = inflater.inflate(R.layout.info, container, false);


    Button btn1 = (Button)rootView.findViewById(R.id.button1);

    btn1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            ((MainActivity)Fragment1.this.getActivity()).Tab.setCurrentItem(2);
        }
    });
    return info;
}


尝试了一下,它有效:)

关于android - 将fragment1切换为fragment2,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23621279/

10-10 08:36