2015-04-28 9 views
22

Muszę zintegrować funkcję typu pudełko (używane/widoczne w jonowej aplikacji ramowej) w natywnej aplikacji dla systemu Android, dla której potrzebuję użyć ViewPager ze wskaźnikiem strony. Próbowałem suwaka dimajia, ale nie sądzę, żebym miał kontrolę nad jego funkcją automatycznego przesuwu. Potrzebuję, aby użytkownik losowo ślizgał się i zorientował się, że slajd porusza się do przodu wraz z urządzeniem wskazującym poniżej. Muszę zintegrować go na moim ekranie startowym, zanim się zaloguję. Z góry dziękuję.Użyj pudełka do slajdów (jak w jonowym szkielecie) w aplikacji natywnej Android z ViewPager

+0

Edytuj: Myślałem, że potrzebujesz pomocy jonowej. Nieważne. –

+0

Nie buduję aplikacji dla wielu platform. Muszę zintegrować się w JAVA, czy istnieje sposób lub biblioteka, aby to osiągnąć? –

+0

Jest ok Karan, nie problem. Dzięki za troskę. –

Odpowiedz

0

Użyj ViewPager z FragmentStatePagerAdapter. Następnie po prostu sprawdź, który fragment (int index) jesteś i użyj go, aby zaktualizować wskaźnik strony.

Aby dowiedzieć się, w którym fragment jesteś dodać następujące karty:

private HashMap<Integer, Fragment> _pageReferenceMap = new HashMap<Integer, Fragment>(); 

w GetItem() dodać

@Override 
public Fragment getItem(int i) { 
    ... 
    Fragment fragment = new myFragment(); 
    _pageReferenceMap.put(i, fragment); 
    return fragment; 
} 

dodać do destroyItem (..)

@Override 
public void destroyItem(ViewGroup container, int position, Object object) { 
    super.destroyItem(container, position, object); 
    _pageReferenceMap.remove(position); 
} 

oraz wreszcie dodać tej metody, aby wiedzieć, który fragment jest aktualnie w widoku

public myFragment getFragment(int key) { 
    return (myFragment)_pageReferenceMap.get(key); 
} 

Aby wywołać tę metodę, wszystko co musisz zrobić, to

int index = mViewpager.getCurrentItem(); 
myFragment fragment = profileAdapter.getFragment(index); 
4

Spróbuj kod:

activity_introduction.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:context="com.yourpackage.IntroductionActivity"> 

    <com.yourpackage.CustomViewPager 
     android:id="@+id/photos_viewpager" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_above="@+id/bottom_relative" /> 

    <RelativeLayout 
     android:id="@+id/bottom_relative" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:padding="@dimen/std_padding"> 

     <android.support.design.widget.TabLayout 
      android:id="@+id/tab_layout" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      app:tabBackground="@drawable/tab_selector" 
      app:tabGravity="center" 
      app:tabIndicatorHeight="0dp" /> 

     <Button 
      android:id="@+id/next_button" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentEnd="true" 
      android:text="@string/next" /> 

    </RelativeLayout> 

</RelativeLayout> 

IntroductionActivity.java

public class IntroductionActivity extends AppCompatActivity { 
    private CustomViewPager viewPager; 
    private Button nextButton; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_introduction); 
     //Initializing the tablayout 
     TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout); 
     //Adding the tabs using addTab() method 
     tabLayout.addTab(tabLayout.newTab()); 
     tabLayout.addTab(tabLayout.newTab()); 
     tabLayout.addTab(tabLayout.newTab()); 
     //Initializing viewPager 
     viewPager = (CustomViewPager) findViewById(R.id.photos_viewpager); 
     viewPager.setPagingEnabled(false); 
     //Creating our pager adapter 
     IntroductionPagerAdapter introductionPagerAdapter = new IntroductionPagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount()); 
     viewPager.setAdapter(introductionPagerAdapter); 
     tabLayout.setupWithViewPager(viewPager, true); 
     LinearLayout tabStrip = ((LinearLayout) tabLayout.getChildAt(0)); 
     for (int i = 0; i < tabStrip.getChildCount(); i++) { 
      tabStrip.getChildAt(i).setOnTouchListener(new View.OnTouchListener() { 
       @Override 
       public boolean onTouch(View v, MotionEvent event) { 
        return true; 
       } 
      }); 
     } 
     nextButton = (Button) findViewById(R.id.next_button); 
     final SharedPreferences sharedPreferences = getSharedPreferences("SHARED_PREFERENCES_PATH", Context.MODE_PRIVATE); 
     nextButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if (viewPager.getCurrentItem() == 0) { 
        viewPager.setCurrentItem(1); 
        nextButton.setText(getResources().getString(R.string.i_agree)); 
       } else if (viewPager.getCurrentItem() == 1) { 
        viewPager.setCurrentItem(2); 
        nextButton.setText(getResources().getString(R.string.skip)); 
       } else if (viewPager.getCurrentItem() == 2) { 
        SharedPreferences.Editor editor = sharedPreferences.edit(); 
        editor.putInt("KEY_INTRODUCTION_SKIPPED", 1); 
        editor.apply(); 
        Intent intent = new Intent(IntroductionActivity.this, MainActivity.class); 
        startActivity(intent); 
        finish(); 
       } 
      } 
     }); 
    } 

    @Override 
    public void onBackPressed() { 
     if (viewPager.getCurrentItem() == 0) { 
      finishAffinity(); 
     } else if (viewPager.getCurrentItem() == 1) { 
      viewPager.setCurrentItem(0); 
      nextButton.setText(getResources().getString(R.string.next)); 
     } else if (viewPager.getCurrentItem() == 2) { 
      viewPager.setCurrentItem(1); 
      nextButton.setText(getResources().getString(R.string.i_agree)); 
     } 
    } 
} 

fragment_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" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

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

     <ImageView 
      android:id="@+id/imageView4" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      app:srcCompat="@mipmap/ic_logo_round" /> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:padding="@dimen/fab_margin" 
      android:text="@string/welcome" 
      android:textAlignment="center" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:textColor="@color/colorWhite" 
      android:textSize="30sp" 
      android:textStyle="bold" /> 

    </LinearLayout> 

</RelativeLayout> 

WelcomeFragment.java

public class WelcomeFragment extends Fragment { 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     return inflater.inflate(R.layout.fragment_welcome, container, false); 
    } 
} 

Podobnie utworzyć dwa kolejne fragmenty.

IntroductionPagerAdaper.java

public class IntroductionPagerAdapter extends FragmentStatePagerAdapter { 
    //private variable 
    private int tabCount; 

    //Constructor to the class 
    public IntroductionPagerAdapter(FragmentManager fm, int tabCount) { 
     super(fm); 
     //Initializing tab count 
     this.tabCount = tabCount; 
    } 

    @Override 
    public Fragment getItem(int position) { 
     //Returning the current tabs 
     switch (position) { 
      case 0: 
       return new WelcomeFragment(); 
      case 1: 
       return new DisclaimerFragment(); 
      case 2: 
       return new RegistrationFragment(); 
      default: 
       return null; 
     } 
    } 

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

CustomViewPager.java

public class CustomViewPager extends ViewPager { 

    private boolean isPagingEnabled = true; 

    public CustomViewPager(Context context) { 
     super(context); 
    } 

    public CustomViewPager(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
     return this.isPagingEnabled && super.onTouchEvent(event); 
    } 

    @Override 
    public boolean onInterceptTouchEvent(MotionEvent event) { 
     return this.isPagingEnabled && super.onInterceptTouchEvent(event); 
    } 

    public void setPagingEnabled(boolean b) { 
     this.isPagingEnabled = b; 
    } 
} 

Uwaga: Można użyć domyślnego ViewPager chyba że chcesz zablokować jego bezstykowa.

+1

Doskonała odpowiedź! Pracuje dla mnie. Uratowałeś mojego codziennego mężczyznę. –

Powiązane problemy