Fragment 與ViewPager的聯合應用實現翻頁效果
阿新 • • 發佈:2022-03-15
MainActivity.java
package com.example.fragmentandviewpager;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager2.widget.ViewPager2;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager2 viewpager = findViewById(R.id.viewPager);
//定義adapter
ViewPagerAdapter viewPagerAdapter=new ViewPagerAdapter();
viewpager.setAdapter(viewPagerAdapter);
}
}
-----------------------------------------------------------------------------
ViewPagerAdapter.java
package com.example.fragmentandviewpager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.PagerAdapter;
public class ViewPagerAdapter extends RecyclerView.Adapter<ViewPagerAdapter.ViewPagerViewHolder> {
@NonNull
@Override
public ViewPagerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//解析 定義ViewPagerViewHolder後需要去適配
return new ViewPagerViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_page,parent,false));
}
@Override
public void onBindViewHolder(@NonNull ViewPagerViewHolder holder, int position) {
//展示不同的資料
}
@Override
public int getItemCount() {
return 5;
}
class ViewPagerViewHolder extends RecyclerView.ViewHolder {
TextView textView;
RecyclerView recyclerView;
public ViewPagerViewHolder(@NonNull View itemView) {
super(itemView);
recyclerView = itemView.findViewById(R.id.itempage);
textView=itemView.findViewById(R.id.tvTitle);
}
}
}
-----------------------------------------------------------------------------
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">
<androidx.viewpager2.widget.ViewPager2
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/viewPager"
android:background="#ff99"/>
</androidx.constraintlayout.widget.ConstraintLayout>
-----------------------------------------------------------------------------
item_page.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/itempage">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textColor="#ff99"
android:textSize="32sp"
android:text="=-="
android:id="@+id/tvTitle"/>
</RelativeLayout>