Java 单击按钮滑动到下一个 Fragment

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20264586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 00:41:29  来源:igfitidea点击:

Swiping to the next Fragment with a button click

javaandroiduser-interfaceandroid-fragmentsandroid-viewpager

提问by Lemon Juice

Here is my code:

这是我的代码:

FirstView.java

第一视图.java

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class FirstView extends Fragment
{
    private TextView firstText;
    private Button btn;

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


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


        firstText = (TextView)view.findViewById(R.id.viewOneText);
        btn = (Button)view.findViewById(R.id.viewOneBtn);

        btn.setOnClickListener(new ButtonEvent());
        return view;

    }

    private class ButtonEvent implements OnClickListener
    {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            new SecondView();

        }

    }
}

SecondView.java

第二视图.java

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class SecondView extends Fragment
{
    private TextView secondText;
    private Button secondViewBtn;

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


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


        secondText = (TextView)view.findViewById(R.id.secondViewText);
        secondViewBtn = (Button)view.findViewById(R.id.secondViewBtn);

        secondViewBtn.setOnClickListener(new ButtonEvent());
        return view;

    }

    private class ButtonEvent implements OnClickListener
    {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            secondText.setText("Second View Text changed");

        }

    }
}

MainActivity.java

主活动.java

import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;

public class MainActivity extends  FragmentActivity  {

    private ViewPager viewPager;
    private MyAdapter pageAdapter;
    private static final int ITEMS = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        viewPager = (ViewPager)findViewById(R.id.pager);
        pageAdapter = new MyAdapter(getSupportFragmentManager());
        viewPager.setAdapter(pageAdapter);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public static class MyAdapter extends FragmentPagerAdapter {


        public MyAdapter(FragmentManager fragmentManager) {
            super(fragmentManager);
        }

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

        @Override
        public Fragment getItem(int position) {
            if(position==0)
            {
                return new FirstView();
            }
            else
            {
                return new SecondView();
            }
        }
    }

}

In this code, when I click on the Buttonin FirstView, I need to move to the SecondView. I tried with Intentsbut guess it is wrong. Currently this is having the Swipe function because of the ViewPager, I need to this to move to the SecondView when the button is clicked, with the same swipe functionality.

在这段代码中,当我Button在 FirstView 中单击时,我需要移动到 SecondView。我试过,Intents但猜是错的。当前,由于具有相同的滑动功能,因此具有滑动功能ViewPager,我需要在单击按钮时移动到 SecondView。

采纳答案by Mikalai Daronin

Just add a new method that moves fragments to your activity:

只需添加一个将片段移动到您的活动的新方法:

public void setCurrentItem (int item, boolean smoothScroll) {
    viewPager.setCurrentItem(item, smoothScroll);
}

and call it from a button listener:

并从按钮侦听器调用它:

((MainActivity)getActivity()).setCurrentItem (1, true);

回答by Hasan A Yousef

you can define in your activity the below functions:

您可以在您的活动中定义以下功能:

public void next_fragment(View view) {
    viewPager.setCurrentItem(viewPager.getCurrentItem()+1);
}

public void previous_fragment(View view) {
    viewPager.setCurrentItem(viewPager.getCurrentItem()-1);
}

then call them from your layout as:

然后从您的布局中调用它们:

android:onClick="next_fragment" for the "NEXT" button

and

android:onClick="previous_fragment" for the "PREVIOUS" button