본문 바로가기
Android 기법/# Study

[Android]ViewPager with bottom dots

by 퍼즐잎 2017. 7. 31.




ViewPager와 하단에 . . . Indicator를 만들어 봤습니다.

쉽게 만드는 ViewPager Indicator 입니다.




[activity_main.xml]


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">
 
    <android.support.v4.view.ViewPager
        android:id="@+id/photos_viewpager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
    </android.support.v4.view.ViewPager>
 
    <android.support.design.widget.TabLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabBackground="@drawable/tab_selector"
        app:tabGravity="center"
        app:tabIndicatorHeight="0dp"
        android:id="@+id/tab_layout" />
 
</LinearLayout>
 
cs



tabIndicatorHeight는 Indicator 하단에 _ 줄이 나타나지 않게 하려고 높이를 0dp로 설정


[pager_adapter.xml]


1
2
3
4
5
6
7
8
9
10
11
12
13
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:adjustViewBounds="true"
        android:id="@+id/imageView"/>
</LinearLayout>
 
cs



. . . 모양으로 만들어 주기 위한 리소스



[default_dot.xml]


1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape
            android:innerRadius="0dp"
            android:shape="ring"
            android:thickness="8dp"
            android:useLevel="false">
            <solid android:color="@android:color/darker_gray"/>
        </shape>
    </item>
</layer-list>
cs


[selected_dot.xml]


1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape
            android:innerRadius="0dp"
            android:shape="ring"
            android:thickness="8dp"
            android:useLevel="false">
            <solid android:color="@color/colorAccent"/>
        </shape>
    </item>
</layer-list>
cs


[tab_selector.xml]


1
2
3
4
5
6
7
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
 
    <item android:drawable="@drawable/selected_dot"
        android:state_selected="true"/>
    <item android:drawable="@drawable/default_dot"/>
</selector>
cs



[MainActivity]


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class MainActivity extends AppCompatActivity {
 
    List<Drawable> temp;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        temp = new ArrayList<>();
        temp.add(ContextCompat.getDrawable(this,이미지 리소스1));
        temp.add(ContextCompat.getDrawable(this,이미지 리소스2));
        temp.add(ContextCompat.getDrawable(this,이미지 리소스3));
 
        Adapter a = new Adapter(temp,this);
 
        ViewPager pager = (ViewPager)findViewById(R.id.photos_viewpager);
        pager.setAdapter(a);
 
 
        TabLayout tabLayout = (TabLayout)findViewById(R.id.tab_layout);
        tabLayout.setupWithViewPager(pager, true);
 
    }
}
 
cs


ViewPager와 TabLayout을 setupWithViewPager를 통해 연결



[Adapter]


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Adapter extends PagerAdapter {
 
    Context context;
    List<Drawable> obj;
    Adapter(List<Drawable> res, Context context){
        obj = res;
        this.context = context;
    }
 
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
 
        View view = null;
        LayoutInflater inflater = LayoutInflater.from(context);
        view = inflater.inflate(R.layout.pager_adapter,container,false);
        ImageView imageView = (ImageView)view.findViewById(R.id.imageView);
        imageView.setImageDrawable(obj.get(position));
        container.addView(view);
 
        return view;
 
    }
 
  
    public int getCount() {
        return obj.size();
    }
}
 
cs



참고 : [링크]


댓글