Java – click and slide to the next segment with a button

Look at the code below

FirstView. 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

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,Bundle savedInstanceState)
    {


        View view = inflater.inflate(R.layout.fragment_view2,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

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 the button in firstview, I need to move to secondview I tried to make a plan, but my guess was wrong At present, this is because of the sliding function of viewpager. When I click the button, I need to move to secondview, which has the same sliding function

What shall I do? Please help.

What shall I do?

Solution

Just add a new way to move fragments to your activity:

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

And call it from the button listener:

((MainActivity)getActivity()).setCurrentItem (1,true);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>