如何在Android入门上制作首页和末页(教程)

时间:2019-02-04 16:18:24

标签: android android-viewpager slide

我在应用程序上安装了开机屏幕,但遇到了问题,因为我不知道如何使第一页没有图像,而最后一页也没有图像。 我希望第一页仅使用textview来解释该应用程序,最后一页也仅使用textview来表示教程已完成,然后单击完成以转到主页并开始使用该应用程序。

    public class SliderAdapter extends PagerAdapter {

    Context context;
    LayoutInflater layoutInflater;

    public SliderAdapter(Context context) {
        this.context = context;
    }


    //Arrays
    public int [] slide_images = {
            R.drawable.tipon,
            R.drawable.toaddnewphoto,
            R.drawable.choosingphoto,
            R.drawable.sharethephoto,
            R.drawable.postplusprofileicon,
            R.drawable.profilepicturee,
            R.drawable.rerf
    };

    public String [] slide_heading = {

            "The main Page",
            "Add new post.",
            "Add new post step 1/2",
            "Add new post step 2/2",
            "Main page after uploading a new post.",
            "Profile Page",
            "CODE"
    };
    public String [] slide_descp = {

            "You will see every time you will log in. \n here you also will your own posts" + " and the posts from users you are following on.",
            "Navigate to add a new post.",
            "Choose a new photo to upload from your gallery.",
            "Choose the company fashion logo and write your description and Hashtags",
            "After you will upload a new post you will be back to the main page and see the new post. You can also click on the profile icon to go to your profile.",
            "This is your profile. You can see all your images below and click on them to see your own post.",
            "Welcome and see you latersadasdad"
    };
    @Override
    public int getCount() {
        return slide_heading.length;
    }

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

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

        layoutInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
        View view = layoutInflater.inflate(R.layout.slide_layout,container,false);

        SquareImageView slideImageView = (SquareImageView) view.findViewById(R.id.slide_image);
        TextView slideHeading = (TextView) view.findViewById(R.id.slide_heading);
        TextView slideDescription = (TextView) view.findViewById(R.id.slide_descp);

        slideImageView.setImageResource(slide_images[position]);
        slideHeading.setText(slide_heading[position]);
        slideDescription.setText(slide_descp[position]);

        container.addView(view);
        return view;

    }

    //stop at the last page.
    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {

        container.removeView((RelativeLayout)object);
    }
}



 This is my slide_layout xml
    <RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <dim.aviv.fashionit.SquareImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/slide_image"
        android:scaleType="centerCrop"
        android:background="@drawable/black_border"
        />



    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="3dp"
        android:layout_below="@id/slide_image"
        android:background="@color/black"
        android:id="@+id/RealativeLayout2"
        >
    </RelativeLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:layout_below="@id/slide_image"
        android:text="HEADING"
        android:textSize="23sp"
        android:textStyle="bold"
        android:layout_centerHorizontal="true"
        android:id="@+id/slide_heading"
        />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:layout_marginTop="5dp"
        android:layout_marginLeft="25dp"
        android:fontFamily="sans-serif"
        android:padding="10dp"
        android:layout_below="@id/slide_heading"
        android:text="bla bla bla"
        android:layout_centerHorizontal="true"
        android:textAlignment="center"
        android:textColor="@color/black"
        android:textSize="18sp"
        android:id="@+id/slide_descp"

        />

</RelativeLayout>

      This is activity_onboarding 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="dim.aviv.fashionit.OnBoardActivity">


    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/slideViewPager"
        android:layout_alignParentStart="true"
        android:layout_above="@+id/dotsLayout">

    </android.support.v4.view.ViewPager>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        android:padding="20dp"
        android:id="@+id/dotsLayout"

        >

    </LinearLayout>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:text="Back"
        android:background="@null"
        android:textColor="@color/black"
        android:visibility="invisible"
        android:id="@+id/prevBtn"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:id="@+id/nextBtn"
        android:text="Next"
        android:background="@null"
        android:textColor="@color/black"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:id="@+id/finishBtn"
        android:text="Finish"
        android:background="@null"
        android:visibility="invisible"
        android:enabled="false"
        android:textColor="@color/black"/>

</RelativeLayout>




 This is my onboarding class:
    public class OnBoardActivity extends AppCompatActivity {

    private ViewPager mSlideViewPager;
    private LinearLayout mDotLayout;

    private TextView [] mDots;

    private SliderAdapter sliderAdapter;

    private Button mNextBtn;
    private Button mBackBtn;
    private Button mFinishBtn;

    private int mCurrentPage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_on_board);

           mSlideViewPager = (ViewPager) findViewById(R.id.slideViewPager);
           mDotLayout = (LinearLayout) findViewById(R.id.dotsLayout);


           mNextBtn = (Button) findViewById(R.id.nextBtn);
           mBackBtn = (Button) findViewById(R.id.prevBtn);
           mFinishBtn = (Button) findViewById(R.id.finishBtn);


           sliderAdapter = new SliderAdapter(this);
           mSlideViewPager.setAdapter(sliderAdapter);


           addDotsIndicator(0);

           mSlideViewPager.addOnPageChangeListener(viewListener);

           mNextBtn.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {

                   mSlideViewPager.setCurrentItem(mCurrentPage +1);
               }
           });

           mBackBtn.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {
                   mSlideViewPager.setCurrentItem(mCurrentPage -1);
               }
           });

           mFinishBtn.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {

                   Intent intent = new Intent(OnBoardActivity.this, MainActivity.class);
                   startActivity(intent);
                   finish();

               }
           });
    }

    public void addDotsIndicator(int position){

        mDots = new TextView[7];
        mDotLayout.removeAllViews();

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

            mDots[i] = new TextView(this);
            mDots[i].setText(Html.fromHtml("&#8226"));
            mDots[i].setTextSize(30);
            mDots[i].setTextColor(getResources().getColor(R.color.grey));

            mDotLayout.addView(mDots[i]);

        }
        if(mDots.length > 0){
            mDots[position].setTextColor(getResources().getColor(R.color.black));
        }
    }

    ViewPager.OnPageChangeListener viewListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int i) {

            addDotsIndicator(i);
            mCurrentPage = i;

            if(i == 0){
                mNextBtn.setEnabled(true);
                mBackBtn.setEnabled(false);
                mFinishBtn.setEnabled(false);
                mBackBtn.setVisibility(View.INVISIBLE);
                mFinishBtn.setVisibility(View.INVISIBLE);

                mNextBtn.setText("Next");
                mBackBtn.setText("");
                mFinishBtn.setText("");

            } else if (i == mDots.length -1 ){

                mNextBtn.setEnabled(false);
                mBackBtn.setEnabled(true);
                mFinishBtn.setEnabled(true);
                mBackBtn.setVisibility(View.VISIBLE);
                mNextBtn.setVisibility(View.INVISIBLE);
                mFinishBtn.setVisibility(View.VISIBLE);

                mNextBtn.setText("");
                mBackBtn.setText("Back");
                mFinishBtn.setText("Finish");
            } else {
                mNextBtn.setEnabled(true);
                mBackBtn.setEnabled(true);
                mFinishBtn.setEnabled(false);

                mBackBtn.setVisibility(View.VISIBLE);
                mFinishBtn.setVisibility(View.INVISIBLE);

                mNextBtn.setText("Next");
                mBackBtn.setText("Back");
                mFinishBtn.setText("");
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    };
}

0 个答案:

没有答案