2swan

가로(세로) 슬라이드 본문

Programming/Android Example

가로(세로) 슬라이드

2swan 2023. 8. 9. 16:21

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/btnToggle"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="가로로 슬라이드"/>

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/viewPager2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="horizontal"/>

</LinearLayout>

 

item_viewpager.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/rl_layout">

    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="ITEM"
        android:textColor="@android:color/white"/>

</RelativeLayout>

main.activity

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ViewPager2 viewPager2 = findViewById(R.id.viewPager2);
        Button btnToggle = findViewById(R.id.btnToggle);

        ArrayList<DataPage> list = new ArrayList<>();
        list.add(new DataPage(Color.RED, "1 page"));
        list.add(new DataPage(Color.BLUE, "2 page"));
        list.add(new DataPage(Color.GREEN, "3 page"));
        list.add(new DataPage(Color.GRAY, "4 page"));
        list.add(new DataPage(Color.YELLOW, "5 page"));
        list.add(new DataPage(Color.DKGRAY, "6 page"));
        viewPager2.setAdapter(new ViewPagerAdapter(list));

        //토글
        btnToggle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(viewPager2.getOrientation() == ViewPager2.ORIENTATION_VERTICAL) {
                    btnToggle.setText("가로로 슬라이드");
                    viewPager2.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL);
                }else{
                    btnToggle.setText("세로로 슬라이드");
                    viewPager2.setOrientation(ViewPager2.ORIENTATION_VERTICAL);
                }

            }
        });



    }
}

 

DataPage.java

public class DataPage {
    private int color;
    private  String title;

    public DataPage(int color, String title) {
        this.color = color;
        this.title = title;
    }

    public int getColor() {
        return color;
    }

    public void setColor(int color) {
        this.color = color;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}

 

ViewPagerAdapter.java

public class ViewPagerAdapter extends RecyclerView.Adapter<ViewPagerAdapter.ViewHolderPage> {
    private ArrayList<DataPage> listDate;

    public ViewPagerAdapter(ArrayList<DataPage> listDate) {
        this.listDate = listDate;
    }

    @NonNull
    @Override
    public ViewPagerAdapter.ViewHolderPage onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        View view = LayoutInflater.from(context).inflate(R.layout.item_viewpager, parent, false);
        return new ViewHolderPage(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewPagerAdapter.ViewHolderPage holder, int position) {
        holder.tv_title.setText(listDate.get(position).getTitle());
        holder.rl_layout.setBackgroundColor(listDate.get(position).getColor());
    }

    @Override
    public int getItemCount() {
        return listDate.size();
    }

    public class ViewHolderPage extends  RecyclerView.ViewHolder {
        private TextView tv_title;
        private RelativeLayout rl_layout;

        public ViewHolderPage(@NonNull View itemView) {
            super(itemView);
            tv_title = itemView.findViewById(R.id.tv_title);
            rl_layout = itemView.findViewById(R.id.rl_layout);
        }
    }

}// class ViewPagerAdapter

결과 값

'Programming > Android Example' 카테고리의 다른 글

Tab 예제  (0) 2023.08.09
슬라이드 예제 2  (0) 2023.08.09
RecyclerView 예제2  (0) 2023.08.09
RecyclerView 추가, 전체보기  (0) 2023.08.09
RecyclerView 예제  (0) 2023.08.09