0

I want to add intent in sliding view pager. As I click different pages of the sliding view pager, a different activity must be called but I can't find the solution. I have added image text and description in the view pager and a button is there in the layout
I think this would involve the use of page selected but earlier when I used onpageselected by using the position it opens up the page even if we are not clicking but I want to use intent here.

Adapterclass

package com.android.msahakyan.expandablenavigationdrawer.adapter;

import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.support.v7.widget.CardView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.msahakyan.expandablenavigationdrawer.R;
import com.android.msahakyan.expandablenavigationdrawer.fragment.CardAdapter;
import com.android.msahakyan.expandablenavigationdrawer.fragment.CardItemString;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tanis on 21-06-2018.
 */

public class CardPagerAdapterS extends PagerAdapter implements CardAdapter {
    private List<CardView> mViews;
    private List<CardItemString> mData;
    private float mBaseElevation;

    public CardPagerAdapterS() {
        mData = new ArrayList<>();
        mViews = new ArrayList<>();
    }

    public void addCardItemS(CardItemString item) {
        mViews.add(null);
        mData.add(item);
    }



    public float getBaseElevation() {
        return mBaseElevation;
    }

    
    @Override
    public CardView getCardViewAt(int position) {
        return mViews.get(position);
    }

    @Override
    public int getCount() {
        return mData.size();
    }

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

    @Override
    public Object instantiateItem(ViewGroup container, int position) {


        View view = LayoutInflater.from(container.getContext())
                .inflate(R.layout.adapter, container, false);
        container.addView(view);
        bind(mData.get(position), view);
        CardView cardView = (CardView) view.findViewById(R.id.cardView);

        if (mBaseElevation == 0) {
            mBaseElevation = cardView.getCardElevation();
        }

        cardView.setMaxCardElevation(mBaseElevation * MAX_ELEVATION_FACTOR);
        mViews.set(position, cardView);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
        mViews.set(position, null);
    }

    private void bind(CardItemString item, View view) {
        TextView titleTextView = (TextView) view.findViewById(R.id.titleTextView);
        TextView contentTextView = (TextView) view.findViewById( R.id.contentTextView);
        ImageView imageView=(ImageView) view.findViewById( R.id.image12 ) ;
        titleTextView.setText(item.getTitle());
        contentTextView.setText(item.getText());
        imageView.setImageResource( item.getImages() );

    }

}

FragmentAction

package com.android.msahakyan.expandablenavigationdrawer.fragment;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.support.v4.content.res.ResourcesCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
//import android.support.v7.widget.LinearLayoutManager;
//import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.msahakyan.expandablenavigationdrawer.R;
import com.android.msahakyan.expandablenavigationdrawer.Registration;
import com.android.msahakyan.expandablenavigationdrawer.adapter.CardPagerAdapterS;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import in.goodiebag.carouselpicker.CarouselPicker;
import technolifestyle.com.imageslider.FlipperLayout;
import technolifestyle.com.imageslider.FlipperView;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link FragmentAction#newInstance} factory method to
 * create an instance of this fragment.
 */
public class FragmentAction extends Fragment {


    ViewPager mViewPager;
    CardPagerAdapterS mCardAdapter;
    ShadowTransformer mCardShadowTransformer;



    private Context context;
    ViewPager viewPager;


    String titlesText [] = {" Website Design", " Digital Marketing", " Domain Registration", "Graphics Design", " Mobile Apps", " Server Hosting",
            " Software Development", " Content Marketing", " Security (SSl)"};
    String  detailsArray [] = {
            "Your website is your digital home. We create, design, redesign, develop, improvise, and implement. We make beautiful websites",
            "We help your business reach potential customers on every possible digital device through all possible media channels    ",
            "To  launch  your  website  the  first  thing  you  need  is  the  domain  name.  You  can  choose  your  domain  name  with  us  here  ",
            "We  generate  creative  solutions  and  can  create  a  wide  range  of  graphic  for  your  clients  which  match  their  business         ",
            "We are mobile. And we make you mobile. We make responsive websites and mobile apps which compliment your business       ",
            "When  you  are  hosting  your     website  in  the  India  you   will  benefit  from  a      higher  ping  rate  and   lowest             latency              ",
            "Our team is competent at coding web apps with keen attention to detail & intuitive functionality that is high on design & creativity",
            "Content  is  the  heart  of  your  digital  presence. We  create  the right            content  with  the  right  focus   for   your  business",
            "Secure your site with the world's leading   provider of online security and get    these exclusive features at no added   cost",
    };
    int[] images = {R.drawable.website_design, R.drawable.digita,R.drawable.domain_registration,R.drawable.graphic,
          R.drawable.mob,R.drawable.server,R.drawable.software_development,R.drawable.ontent,R.drawable.ssl};

    private static final String KEY_MOVIE_TITLE = "key_title";

    public FragmentAction() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment.
     *
     * @return A new instance of fragment FragmentAction.
     */
    public static FragmentAction newInstance(String movieTitle) {
        FragmentAction fragmentAction = new FragmentAction();
        Bundle args = new Bundle();
        args.putString(KEY_MOVIE_TITLE, movieTitle);
        fragmentAction.setArguments(args);

        return fragmentAction;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);


    }

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


        View v = inflater.inflate(R.layout.fragment_action,container,false);
        TextView txt = (TextView)v.findViewById( R.id.textView12 );
        txt.setText("\u25BA Creative & Dedicated Team");
        TextView txt1 = (TextView)v.findViewById( R.id.textView13 );
        txt1.setText("\u25BA Affordable Cost");
        TextView txt2 = (TextView)v.findViewById( R.id.textView14 );
        txt2.setText("\u25BA Maintain Long Relationship");
        TextView txt3 = (TextView)v.findViewById( R.id.textView15 );
        txt3.setText("\u25BA Timely Deliverly ");

        context = this.getContext();


        mViewPager = (ViewPager)v.findViewById(R.id.viewpager1);
        mCardAdapter = new CardPagerAdapterS();




        for (int i=0; i<titlesText.length; i++){

            mCardAdapter.addCardItemS(new CardItemString( titlesText[i], detailsArray[i],images[i]));
        }

        mCardShadowTransformer = new ShadowTransformer(mViewPager, mCardAdapter);

        mViewPager.setAdapter(mCardAdapter);
        mViewPager.setPageTransformer(false, mCardShadowTransformer);
        mViewPager.setOffscreenPageLimit(3);



        viewPager = (ViewPager)v.findViewById( R.id.viewpager );
        ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter( this.getContext() );
        viewPager.setAdapter( viewPagerAdapter );

        Timer timer = new Timer(  );
        timer.scheduleAtFixedRate( new Mytime(),2000,4000 );



        FloatingActionButton floatingActionButton = (FloatingActionButton)v.findViewById( R.id.floatingActionButton );
        floatingActionButton.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity( new Intent( getActivity(),Registration.class ) );
            }
        } );




        return v;

    }



    public class Mytime extends TimerTask{
        @Override
        public void run() {

            getActivity().runOnUiThread( new Runnable() {
                @Override
                public void run() {

                    if(viewPager.getCurrentItem() == 0) {
                        viewPager.setCurrentItem( 1 );
                    }
                    else if (viewPager.getCurrentItem()== 1){
                        viewPager.setCurrentItem( 2 );

                    }
                    else if (viewPager.getCurrentItem()== 2){
                        viewPager.setCurrentItem( 3 );

                    }
                    else if (viewPager.getCurrentItem()== 3){
                        viewPager.setCurrentItem( 4 );

                    }
                    else {
                        viewPager.setCurrentItem(0);
                    }
                }
            } );
        }
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        Drawable movieIcon = ResourcesCompat.getDrawable(getResources(), R.drawable.webdesign, getContext().getTheme());

        String movieTitle = getArguments().getString(KEY_MOVIE_TITLE);
    }


}
Community
  • 1
  • 1
sayo
  • 25
  • 6

1 Answers1

0

first of all onPageSelected listener is not really good idea in this case, because it is invoked every time when viewpager' current position changes (it's good for setting toolbar title, for example).

You can add another collection for Activities which can be invoked in CardPagerAdapterS, for example:

....

private List<CardItemString> mData;

private List<Class<? extends Activity> mActivities;

and use them to create click listener in order to open activities:

@Override
public Object instantiateItem(ViewGroup container, int position) {


    View view = LayoutInflater.from(container.getContext())
            .inflate(R.layout.adapter, container, false);
    container.addView(view);
    bind(mData.get(position), view);
    CardView cardView = (CardView) view.findViewById(R.id.cardView);

    if (mBaseElevation == 0) {
        mBaseElevation = cardView.getCardElevation();
    }

    cardView.setMaxCardElevation(mBaseElevation * MAX_ELEVATION_FACTOR);
    cardView.setOnCLickListener(v-> {
        context.startActivity(new Intent(context, mActivities.get(position));
    });

    mViews.set(position, cardView);


    return view;
} 
Lukas
  • 1,216
  • 12
  • 25
  • If I want to jump on x.class on position 1 and y.class on position 2 how will i do that please help – sayo Jun 25 '18 at 06:09
  • you have to initialize `List mActivities;` but they are shown after you click on each page. maybe you want fragments inside each page? https://stackoverflow.com/questions/18413309/how-to-implement-a-viewpager-with-different-fragments-layouts – Lukas Jun 26 '18 at 06:15