我想在android中使用Tabs(ViewPager Indicator)实现无限的ViewPager。
我找到了很多示例项目,所有这些项目都只包含ViewPager。我需要将infite ViewPager与Tabs集成。
我发现的一些样本:
1)https://github.com/antonyt/InfiniteViewPager
任何帮助或想法都将受到赞赏。
答案 0 :(得分:1)
我建议您使用LoopViewPager库 使用PagerSlidingTabsTrip的https://github.com/yanzm/LoopViewPager将解决您的问题
答案 1 :(得分:0)
**viewpager with indicator in android using fragment**
ViewPagerAdapter
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class ViewPagerAdapter extends FragmentPagerAdapter {
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return 4;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
FragmentAnimation1 fragmentAnimation1 = new FragmentAnimation1();
return fragmentAnimation1;
case 1:
FragmentAnimation2 fragmentAnimation2 = new FragmentAnimation2();
return fragmentAnimation2;
case 2:
FragmentAnimation3 fragmentAnimation3 = new FragmentAnimation3();
return fragmentAnimation3;
case 3:
FragmentAnimation4 fragmentAnimation4 = new FragmentAnimation4();
return fragmentAnimation4;
}
return null;
}
}
**Activity**
ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager()));
xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical">
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
答案 2 :(得分:0)
Utils.serverError(MainActivity.this);
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
alertBuilder.setCancelable(true);
alertBuilder.setTitle("");
alertBuilder.setMessage("Server Error");
alertBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
dialog.dismiss();
}
});
AlertDialog alert = alertBuilder.create();
Button pbutton = alert.getButton(DialogInterface.BUTTON_POSITIVE);
pbutton.setTextColor(context.getResources().getColor(R.color.black));
alert.show();
**WelcomemainActivity**
public class WelcomemainActivity extends AppCompatActivity {
private ViewPager viewPager;
private MyViewPagerAdapter myViewPagerAdapter;
private LinearLayout dotsLayout;
private TextView[] dots;
private int[] layouts;
private Button btnSkip, btnNext;
private PrefManager prefManager_main;
int currentPage = 0;
Timer timer;
final long DELAY_MS = 500;//delay in milliseconds before task is to be executed
final long PERIOD_MS = 20000; // time in milliseconds between successive task executions.
final int NUM_PAGES = 4;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Checking for first time launch - before calling setContentView()
/* prefManager_main = new PrefManager(this);
if (!prefManager_main.isFirstTimemainLaunch())
{
launchHomeScreen();
finish();
}*/
// Making notification bar transparent
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_welcomemain);
viewPager = (ViewPager) findViewById(R.id.view_pager);
dotsLayout = (LinearLayout) findViewById(R.id.layoutDots);
btnSkip = (Button) findViewById(R.id.btn_skip);
btnSkip.setVisibility(View.GONE);
btnNext = (Button) findViewById(R.id.btn_next);
new MaterialShowcaseView.Builder(WelcomemainActivity.this)
.setTarget(btnNext)
.setMaskColour(Color.argb(150, 0, 0, 0))
.setContentText("Next Screen") // optional but starting animations immediately in onCreate can make them choppy
.setDismissOnTouch(true)
.singleUse("next")
.show();
// layouts of all welcome sliders
// add few more layouts if you want
layouts = new int[]{
R.layout.welcome_slide1,
R.layout.welcome_slide2,
R.layout.welcome_slide3,
R.layout.welcome_slide4};
// adding bottom dots
addBottomDots(0);
// making notification bar transparent
changeStatusBarColor();
myViewPagerAdapter = new MyViewPagerAdapter();
viewPager.setAdapter(myViewPagerAdapter);
viewPager.addOnPageChangeListener(viewPagerPageChangeListener);
btnSkip.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
launchHomeScreen();
}
});
btnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// checking for last page
// if last page home screen will be launched
int current = getItem(+1);
if (current < layouts.length) {
// move to next screen
viewPager.setCurrentItem(current);
} else {
launchHomeScreen();
}
}
});
/*After setting the adapter use the timer */
// final Handler handler = new Handler();
// final Runnable Update = new Runnable() {
// public void run() {
// if (currentPage == NUM_PAGES - 1) {
// currentPage = 0;
// }
// viewPager.setCurrentItem(currentPage++, true);
// }
// };
//
// timer = new Timer(); // This will create a new Thread
// timer.schedule(new TimerTask() { // task to be scheduled
//
// @Override
// public void run() {
// handler.post(Update);
// }
// }, DELAY_MS, PERIOD_MS);
makeLanguageDialogChoise();
}
private void addBottomDots(int currentPage) {
dots = new TextView[layouts.length];
// Typeface font = Typeface.createFromAsset(getAssets(), "fonts/Lohit-Gujarati.otf");
// Typeface mFont = Typeface.createFromAsset(mContext.getAssets(),
// "fonts/montserrat_regular.otf");
int[] colorsActive = getResources().getIntArray(R.array.array_dot_active);
int[] colorsInactive = getResources().getIntArray(R.array.array_dot_inactive);
dotsLayout.removeAllViews();
for (int i = 0; i < dots.length; i++) {
dots[i] = new TextView(this);
dots[i].setText(Html.fromHtml("•"));
dots[i].setTextSize(35);
dots[i].setTextColor(colorsInactive[currentPage]);
// dots[i].setTypeface(font);
dotsLayout.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_main.setFirstTimeMainLaunch(false);
startActivity(new Intent(WelcomemainActivity.this, MainActivity.class));
finish();
}
// viewpager change listener
ViewPager.OnPageChangeListener viewPagerPageChangeListener = new ViewPager.OnPageChangeListener()
{
@Override
public void onPageSelected(int position) {
addBottomDots(position);
// changing the next button text 'NEXT' / 'GOT IT'
if (position == layouts.length - 1)
{
// last page. make button text to GOT IT
btnNext.setText(getString(R.string.start));
btnSkip.setVisibility(View.GONE);
}
else
{
// still pages are left
btnNext.setText(getString(R.string.next));
btnSkip.setVisibility(View.GONE);
}
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
};
/**
* Making notification bar transparent
*/
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);
}
}
/**
* View pager adapter
*/
public class MyViewPagerAdapter extends PagerAdapter {
private LayoutInflater layoutInflater;
public MyViewPagerAdapter() {
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(layouts[position], container, false);
// if (layouts[position] == 0) {
//
// TextView textView = (TextView) view.findViewById(R.id.txt1);
// textView.setText("txt1");
// } else if (layouts[position] == 1) {
// TextView textView = (TextView) view.findViewById(R.id.txt2);
// textView.setText("txt2");
// } else if (layouts[position] == 2) {
// TextView textView = (TextView) view.findViewById(R.id.txt3);
// textView.setText("txt3");
// } else {
//// TextView textView = (TextView) view.findViewById(R.id.txt4);
//// textView.setText("txt4");
// }
container.addView(view);
return view;
}
@Override
public int getCount() {
return layouts.length;
}
@Override
public boolean isViewFromObject(View view, Object obj) {
return view == obj;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
View view = (View) object;
container.removeView(view);
}
}
private void makeLanguageDialogChoise() {
// Build an AlertDialog
// AlertDialog.Builder builder = new AlertDialog.Builder(WelcomeActivity.this);
// String array for alert dialog multi choice items
String[] colors = new String[]{
"English",
"Hindi",
"Gujarati",
};
// Boolean array for initial selected items
final boolean[] checkedColors = new boolean[]{
false, // Red
true, // Green
false, // Blue
};
new AlertDialog.Builder(this)
.setSingleChoiceItems(colors, 0, null)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
int selectedPosition = ((AlertDialog) dialog).getListView().getCheckedItemPosition();
// Do something useful withe the position of the selected radio button
}
})
.setCancelable(false)
.show();
}
}
public class PrefManager {
SharedPreferences pref;
SharedPreferences.Editor editor;
Context _context;
// shared pref mode
int PRIVATE_MODE = 0;
// Shared preferences file name
private static final String PREF_NAME = "";
private static final String IS_FIRST_TIME_LAUNCH = "IsFirstTimeLaunch";
private static final String IS_FIRST_TIME_MAIN_LAUNCH = "IsFirstTimeMainLaunch";
public PrefManager(Context context)
{
this._context = context;
pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
editor = pref.edit();
}
public void setFirstTimeLaunch(boolean isFirstTime)
{
editor.putBoolean(IS_FIRST_TIME_LAUNCH, isFirstTime);
editor.commit();
}
public boolean isFirstTimeLaunch()
{
return pref.getBoolean(IS_FIRST_TIME_LAUNCH, true);
}
// // TODO: 27/1/18 sanjay
public void setFirstTimeMainLaunch(boolean isFirstTime) {
editor.putBoolean(IS_FIRST_TIME_MAIN_LAUNCH, isFirstTime);
editor.commit();
}
public boolean isFirstTimemainLaunch() {
return pref.getBoolean(IS_FIRST_TIME_MAIN_LAUNCH, true);
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<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"></LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_above="@id/layoutDots"
android:alpha=".5"
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:layout_margin="5dp"
android:background="@drawable/rectangle_rounded_corner_accent_filled"
android:text="@string/next"
android:textColor="@color/white"
android:textStyle="bold" />
<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="@string/skip"
android:textColor="@android:color/black" />
</RelativeLayout>