ViewPager文本Onclick

时间:2017-11-07 09:00:12

标签: android android-viewpager textview

我目前正在做这个Android教程https://www.androidhive.info/2016/05/android-build-intro-slider-app/

我想在onClick()页面内实施viewpager事件,就像点击文字一样,它会将我引导到另一个页面。

有可能吗?如果可能的话,请帮帮我?感谢

这是我的viewPager代码。

public class welcomeActivity extends AppCompatActivity {

ViewPager viewPager;
private LinearLayout myLinear;
private TextView[] dots;
private int[] layouts;
private Button btnSkip, btnNext;
private MyViewPagerAdapter myViewPagerAdapter;
private PrefManager prefManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (Build.VERSION.SDK_INT >= 21) {
        getWindow().getDecorView().
                setSystemUiVisibility
                (View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);}

    setContentView(R.layout.activity_welcome);

    viewPager = (ViewPager) findViewById(R.id.view_pager);
    myLinear = (LinearLayout) findViewById(R.id.layoutDots);
    btnSkip = (Button) findViewById(R.id.btn_skip);
    btnNext = (Button) findViewById(R.id.btn_next);

    layouts = new int[]{
      R.layout.slide1, R.layout.slide2, R.layout.slide3, R.layout.slide4, R.layout.slide5
    };

    addBottomDots(0);
    changeStatusBarColor();

    myViewPagerAdapter = new MyViewPagerAdapter();
    viewPager.setAdapter(myViewPagerAdapter);
    viewPager.addOnPageChangeListener(viewPagerListener);

    btnSkip.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            launchHomeScreen();
        }
    });

    btnNext.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int current = getItem(+1);
            if (current < layouts.length){
                viewPager.setCurrentItem(current);
            }else launchHomeScreen();
        }
    });
}

ViewPager.OnPageChangeListener viewPagerListener = new ViewPager.OnPageChangeListener() {
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        addBottomDots(position);
        if (position == layouts.length -1){
           btnSkip.setVisibility(View.GONE);
            btnNext.setText("Get Started..");
        }
        else {
            btnNext.setText(getString(R.string.next));
            btnSkip.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
};

private void addBottomDots(int currentPage) {
    dots = new TextView[layouts.length];

    int[] colorsActive = getResources().getIntArray(R.array.array_dot_active);
    int[] colorsInactive = getResources().getIntArray(R.array.array_dot_inactive);

    myLinear.removeAllViews();
    for (int i = 0; i < dots.length; i++) {
        dots[i] = new TextView(this);
        dots[i].setText(Html.fromHtml("&#8226;"));
        dots[i].setTextSize(35);
        dots[i].setTextColor(colorsInactive[currentPage]);
        myLinear.addView(dots[i]);
    }

    if (dots.length > 0)
        dots[currentPage].setTextColor(colorsActive[currentPage]);
}

private int getItem(int i) {
    return viewPager.getCurrentItem() + i;
}

private void launchHomeScreen() {
    //prefManager.setFirstTimeLaunch(false);
    startActivity(new Intent(welcomeActivity.this, MainActivity.class));
    finish();
}

private void changeStatusBarColor() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(Color.TRANSPARENT);
    }
}

public class MyViewPagerAdapter extends PagerAdapter{

    private LayoutInflater inflater;

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
       inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflater.inflate(layouts[position],container,false);
        container.addView(v);
        return v;
    }

    @Override
    public int getCount() {
        return layouts.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {

        View view = (View) object;
        container.removeView(view);
    }
}

}

这是我的布局。 (activity_welcome.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:showIn="@layout/activity_welcome">


 <android.support.v4.view.ViewPager
    android:id="@+id/view_pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

 <LinearLayout
    android:id="@+id/layoutDots"
    android:layout_width="match_parent"
    android:layout_height="@dimen/dots_height"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="@dimen/dots_margin_bottom"
    android:gravity="center"
    android:orientation="horizontal"/>

 <View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:alpha=".5"
    android:layout_above="@id/layoutDots"
    android:background="@android:color/white" />

 <Button
    android:id="@+id/btn_next"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:background="@null"
    android:text="Next"
    android:textColor="@android:color/white" />

<Button
    android:id="@+id/btn_skip"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:background="@null"
    android:text="BACK"
    android:textColor="@android:color/white" />

 </RelativeLayout>

slide1.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/bg_screen1">

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:gravity="center_horizontal"
    android:orientation="vertical">

    <ImageView
        android:layout_width="@dimen/img_width_height"
        android:layout_height="@dimen/img_width_height"
        android:src="@drawable/ic_food" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/slide_1_title"
        android:textColor="@android:color/white"
        android:textSize="@dimen/slide_title"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/tvFood"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:paddingLeft="@dimen/desc_padding"
        android:paddingRight="@dimen/desc_padding"
        android:text="@string/slide_1_desc"
        android:textAlignment="center"
        android:textColor="@android:color/white"
        android:textSize="@dimen/slide_desc" />

</LinearLayout>
</RelativeLayout>

slide2.xml

 <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="@color/bg_screen2">

 <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:gravity="center_horizontal"
    android:orientation="vertical">

    <ImageView
        android:layout_width="@dimen/img_width_height"
        android:layout_height="@dimen/img_width_height"
        android:src="@drawable/ic_movie" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/slide_2_title"
        android:textColor="@android:color/white"
        android:textSize="@dimen/slide_title"
        android:textStyle="bold" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:paddingLeft="@dimen/desc_padding"
        android:paddingRight="@dimen/desc_padding"
        android:text="@string/slide_2_desc"
        android:textAlignment="center"
        android:textColor="@android:color/white"
        android:textSize="@dimen/slide_desc" />

 </LinearLayout>


 </RelativeLayout>

2 个答案:

答案 0 :(得分:0)

在适配器中输入以下行:

View v = inflater.inflate(layouts[position],container,false);

添加:

TextView tv =(TextView) v.findViewById(R.id.yourTextView);
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
   // Do your stuff here whatever you want to do upon click
   }});

答案 1 :(得分:0)

public class IntroFragment_1 extends Fragment {

    private String title;
    private int page;
    ImageView intro_images_1;
    Animation xmlAnimationSample;
    // newInstance constructor for creating fragment with arguments
    public static IntroFragment_1 newInstance(int page, String title) {
        IntroFragment_1 fragmentFirst = new IntroFragment_1();
        Bundle args = new Bundle();
        args.putInt("someInt", page);
        args.putString("someTitle", title);
        fragmentFirst.setArguments(args);
        return fragmentFirst;
    }
    // Store instance variables based on arguments passed
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        page = getArguments().getInt("someInt", 0);
        title = getArguments().getString("someTitle");
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.intro_view_1, container, false);
        intro_images_1 = view.findViewById(R.id.intro_images_1);
        xmlAnimationSample = AnimationUtils.loadAnimation(getContext(),R.anim.zoom_intro);
        intro_images_1.startAnimation(xmlAnimationSample);
        return view;
    }
}

MyViewPagerAdapter

public static class MyViewPagerAdapter extends FragmentPagerAdapter {
        private static int NUM_ITEMS = 3;

        public MyViewPagerAdapter(FragmentManager fragmentManager) {
            super(fragmentManager);
        }
        // Returns total number of pages
        @Override
        public int getCount() {
            return NUM_ITEMS;
        }

        // Returns the fragment to display for that page
        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0: // Fragment # 0 - This will show FirstFragment
                    return IntroFragment_1.newInstance(0, "Page # 1");
                case 1: // Fragment # 0 - This will show FirstFragment different title
                    return IntroFragment_1.newInstance(0, "Page # 1");
                case 2: // Fragment # 1 - This will show SecondFragment
                    return IntroFragment_1.newInstance(0, "Page # 1");
                default:
                    return null;
            }
        }

        // Returns the page title for the top indicator
        @Override
        public CharSequence getPageTitle(int position) {
            return "Page " + position;
        }

    }
 layouts = new int[]{0, 1, 2,};