일용직 코딩노동자

Image ViewPager 본문

Android

Image ViewPager

사용자 일용직 코딩노동자 2019. 9. 10. 16:11
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import java.util.ArrayList;

class ViewPagerAdapter extends PagerAdapter {
private Context mContext;
private ArrayList<Integer> imageList;

public ViewPagerAdapter(Context context, ArrayList<Integer> imageList)
{
this.mContext = context;
this.imageList = imageList;
}

@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.subpage, null);

ImageView imageView = view.findViewById(R.id.imageView);
imageView.setImageResource(imageList.get(position));

container.addView(view);

return view;
}

@Override
public int getCount() {
return imageList.size();
}

@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((View)object);
}

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

MainActivity.java

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import java.util.ArrayList;
import android.support.v4.app.Fragment;

public class MainActivity extends AppCompatActivity {

private ArrayList<Integer> imageList;
private static final int DP = 24; //수치가 높을수록 옆에 그림이 조금씩 더 많이보임

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

this.initializeData();

ViewPager viewPager = findViewById(R.id.viewPager);
viewPager.setClipToPadding(false);

float density = getResources().getDisplayMetrics().density;
int margin = (int) (DP * density);
viewPager.setPadding(margin, 0, margin, 0);
viewPager.setPageMargin(margin/2);

viewPager.setAdapter(new ViewPagerAdapter(this, imageList));
}

public void initializeData() //이미지를 저장함 배열에
{
imageList = new ArrayList();

imageList.add(R.drawable.movie1);
imageList.add(R.drawable.movie2);
imageList.add(R.drawable.movie3);
imageList.add(R.drawable.movie4);
imageList.add(R.drawable.movie5);
}
}

 

ViewpagerAdapter.java

 

 

activity_main.xml

<android.support.constraint.ConstraintLayout 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=".MainActivity">



<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="0dp"
android:layout_height="300dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

 

subpage.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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">

<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView"
android:src="@drawable/movie1"
/>
</android.support.constraint.ConstraintLayout>

 

--결과--

 

 

궁금하신 사항은 댓글 남겨주세요.

'Android' 카테고리의 다른 글

SharedPreferences (간단한 정보저장)  (0) 2019.09.10
비콘감지  (0) 2019.09.10
EditText/TextView/ImageView/Button/Radio/RadioGroup/URI/Intent  (0) 2019.09.10
Image ViewPager  (9) 2019.09.10
ViewPager  (0) 2019.09.10
ListView  (0) 2019.09.10
9 Comments
댓글쓰기 폼