Android view pager with page indicator

后端 未结 7 1481
终归单人心
终归单人心 2020-12-07 19:23

I need to get page indicator in the view pager file with images. Here is my code.

public class IndicatorActivity extends Activity {


 /** Called when the ac         


        
相关标签:
7条回答
  • 2020-12-07 19:33

    you have to do following:

    1-Download the full project from here https://github.com/JakeWharton/ViewPagerIndicator ViewPager Indicator 2- Import into the Eclipse.

    After importing if you want to make following type of screen then follow below steps -

    Screen Shot

    change in

    Sample circles Default

      package com.viewpagerindicator.sample;
      import android.os.Bundle;  
      import android.support.v4.view.ViewPager;
      import com.viewpagerindicator.CirclePageIndicator;
    
      public class SampleCirclesDefault extends BaseSampleActivity {
       @Override
       protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.simple_circles);
    
        mAdapter = new TestFragmentAdapter(getSupportFragmentManager());
    
        mPager = (ViewPager)findViewById(R.id.pager);
      //  mPager.setAdapter(mAdapter);
    
        ImageAdapter adapter = new ImageAdapter(SampleCirclesDefault.this);
        mPager.setAdapter(adapter);
    
    
        mIndicator = (CirclePageIndicator)findViewById(R.id.indicator);
        mIndicator.setViewPager(mPager);
      }
    }
    

    ImageAdapter

     package com.viewpagerindicator.sample;
    
     import android.content.Context;
     import android.support.v4.view.PagerAdapter;
     import android.support.v4.view.ViewPager;
     import android.view.LayoutInflater;
     import android.view.View;
     import android.view.ViewGroup;
     import android.widget.ImageView;
     import android.widget.TextView;
    
     public class ImageAdapter extends PagerAdapter {
     private Context mContext;
    
     private Integer[] mImageIds = { R.drawable.about1, R.drawable.about2,
            R.drawable.about3, R.drawable.about4, R.drawable.about5,
            R.drawable.about6, R.drawable.about7
    
     };
    
     public ImageAdapter(Context context) {
        mContext = context;
     }
    
     public int getCount() {
        return mImageIds.length;
     }
    
     public Object getItem(int position) {
        return position;
     }
    
     public long getItemId(int position) {
        return position;
     }
    
     @Override
     public Object instantiateItem(ViewGroup container, final int position) {
    
        LayoutInflater inflater = (LayoutInflater) container.getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
        View convertView = inflater.inflate(R.layout.gallery_view, null);
    
        ImageView view_image = (ImageView) convertView
                .findViewById(R.id.view_image);
        TextView description = (TextView) convertView
                .findViewById(R.id.description);
    
        view_image.setImageResource(mImageIds[position]);
        view_image.setScaleType(ImageView.ScaleType.FIT_XY);
    
        description.setText("The natural habitat of the Niligiri tahr,Rajamala          Rajamala is 2695 Mts above sea level"
            + "The natural habitat of the Niligiri tahr,Rajamala Rajamala is 2695 Mts above sea level"
                        + "The natural habitat of the Niligiri tahr,Rajamala Rajamala is 2695 Mts above sea level");
    
        ((ViewPager) container).addView(convertView, 0);
    
        return convertView;
     }
    
     @Override
     public boolean isViewFromObject(View view, Object object) {
        return view == ((View) object);
     }
    
     @Override
     public void destroyItem(ViewGroup container, int position, Object object) {
        ((ViewPager) container).removeView((ViewGroup) object);
     }
    }
    

    gallery_view.xml

     <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/about_bg"
    android:orientation="vertical" >
    
    <LinearLayout
        android:id="@+id/about_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:weightSum="1" >
    
        <LinearLayout
            android:id="@+id/about_layout1"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight=".4"
            android:orientation="vertical" >
    
            <ImageView
                android:id="@+id/view_image"
                android:layout_width="match_parent"
                android:layout_height="match_parent" 
                android:background="@drawable/about1">
         </ImageView>
        </LinearLayout>
    
        <LinearLayout
            android:id="@+id/about_layout2"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight=".6"
            android:orientation="vertical" >
    
            <TextView
                android:id="@+id/textView1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="SIGNATURE LANDMARK OF MALAYSIA-SINGAPORE CAUSEWAY"
                android:textColor="#000000"
                android:gravity="center"
                android:padding="18dp"
                android:textStyle="bold"
                android:textAppearance="?android:attr/textAppearance" />
    
    
            <ScrollView
                android:layout_width="fill_parent"
                android:layout_height="match_parent"
                android:fillViewport="false"
                android:orientation="vertical"
                android:scrollbars="none"
                android:layout_marginBottom="10dp"
                android:padding="10dp" >
    
                <TextView
                    android:id="@+id/description"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:textColor="#000000"
                    android:text="TextView" />
    
                </ScrollView>
        </LinearLayout>
     </LinearLayout>
    

    0 讨论(0)
  • 2020-12-07 19:42

    Just an improvement to the nice answer given by @vuhung3990. I implemented the solution and works great but if I touch one radio button it will be selected and nothing happens.

    I suggest to also change page when a radio button is tapped. To do this, simply add a listener to the radioGroup:

    mPager = (ViewPager) findViewById(R.id.pager);
    final RadioGroup radioGroup = (RadioGroup)findViewById(R.id.radiogroup);    
    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    switch (checkedId) {
                        case R.id.radioButton :
                            mPager.setCurrentItem(0, true);
                            break;
                        case R.id.radioButton2 :
                            mPager.setCurrentItem(1, true);
                            break;
                        case R.id.radioButton3 :
                            mPager.setCurrentItem(2, true);
                            break;
                    }
                }
            });
    
    0 讨论(0)
  • 2020-12-07 19:53

    UPDATE: 22/03/2017

    main fragment layout:

           <FrameLayout 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">
    
                <android.support.v4.view.ViewPager
                    android:id="@+id/viewpager"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />
    
                <RadioGroup
                    android:id="@+id/page_group"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal|bottom"
                    android:layout_marginBottom="@dimen/margin_help_container"
                    android:orientation="horizontal">
    
                    <RadioButton
                        android:id="@+id/page1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:checked="true" />
    
                    <RadioButton
                        android:id="@+id/page2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />
    
                    <RadioButton
                        android:id="@+id/page3"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />
                </RadioGroup>
            </FrameLayout>
    

    set up view and event on your fragment like this:

            mViewPaper = (ViewPager) view.findViewById(R.id.viewpager);
            mViewPaper.setAdapter(adapder);
    
            mPageGroup = (RadioGroup) view.findViewById(R.id.page_group);
            mPageGroup.setOnCheckedChangeListener(this);
    
            mViewPaper.addOnPageChangeListener(this);
    
           *************************************************
           *************************************************
    
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        }
    
        @Override
        public void onPageSelected(int position) {
            // when current page change -> update radio button state
            int radioButtonId = mPageGroup.getChildAt(position).getId();
            mPageGroup.check(radioButtonId);
        }
    
        @Override
        public void onPageScrollStateChanged(int state) {
        }
    
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
            // when checked radio button -> update current page
            RadioButton checkedRadioButton = (RadioButton)radioGroup.findViewById(checkedId);
            // get index of checked radio button
            int index = radioGroup.indexOfChild(checkedRadioButton);
    
            // update current page
            mViewPaper.setCurrentItem(index), true);
        }
    

    custom checkbox state: Custom checkbox image android

    Viewpager tutorial: http://architects.dzone.com/articles/android-tutorial-using

    enter image description here

    0 讨论(0)
  • 2020-12-07 19:55

    Here are a few things you need to do:

    1-Download the library if you haven't already done that.

    2- Import into Eclipse.

    3- Set you project to use the library: Project-> Properties -> Android -> Scroll down to Library section, click Add... and select viewpagerindicator.

    4- Now you should be able to import com.viewpagerindicator.TitlePageIndicator.

    Now about implementing this without using fragments:

    In the sample that comes with viewpagerindicatior, you can see that the library is being used with a ViewPager which has a FragmentPagerAdapter.

    But in fact the library itself is Fragment independant. It just needs a ViewPager. So just use a PagerAdapter instead of a FragmentPagerAdapter and you're good to go.

    0 讨论(0)
  • 2020-12-07 19:55

    You Can create a Linear layout containing an array of TextView (mDots). To represent the textView as Dots provide this HTML source in your code . refer my code . I got this information from Youtube Channel TVAC Studio . here the code : `

        addDotsIndicator(0);
        viewPager.addOnPageChangeListener(viewListener);
    }
    
    public void addDotsIndicator(int position)
    {
            mDots = new TextView[5];
            mDotLayout.removeAllViews();
    
            for (int i = 0; i<mDots.length ; i++)
            {
                 mDots[i]=new TextView(this);
                 mDots[i].setText(Html.fromHtml("&#8226;"));            //HTML for dots
                 mDots[i].setTextSize(35);
                 mDots[i].setTextColor(getResources().getColor(R.color.colorAccent));
    
                 mDotLayout.addView(mDots[i]);
            }
    
            if(mDots.length>0)
            {
                mDots[position].setTextColor(getResources().getColor(R.color.orange));
            }
    }
    
    ViewPager.OnPageChangeListener viewListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int 
       positionOffsetPixels) {
    
        }
    
        @Override
        public void onPageSelected(int position) {
    
            addDotsIndicator(position);
        }
    
        @Override
        public void onPageScrollStateChanged(int state) {
    
        }
    };`
    
    0 讨论(0)
  • 2020-12-07 19:57

    I know this has already been answered, but for anybody looking for a simple, no-frills implementation of a ViewPager indicator, I've implemented one that I've open sourced. For anyone finding Jake Wharton's version a bit complex for their needs, have a look at https://github.com/jarrodrobins/SimpleViewPagerIndicator.

    0 讨论(0)
提交回复
热议问题