二十八、Fragment + ViewPager使用
1. activity_main.xml
<?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" tools:context=".MainActivity"> <androidx.viewpager2.widget.ViewPager2 android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:id="@+id/id_viewpager" android:background="@color/purple_200"> </androidx.viewpager2.widget.ViewPager2> </LinearLayout>
2. fragment_blank.xml
<?xml version="1.0" encoding="utf-8"?> <FrameLayout 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" tools:context=".BlankFragment"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:textSize="36sp" android:id="@+id/text" android:gravity="center" android:text="测试" /> </FrameLayout>
3. BlankFragment
package com.example.pagerrun; import android.os.Bundle; import androidx.fragment.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class BlankFragment extends Fragment { private static final String ARG_TEXT = "param1"; private String mTextString; View rootview; public BlankFragment() { } public static BlankFragment newInstance(String param1) { BlankFragment fragment = new BlankFragment(); Bundle args = new Bundle(); args.putString(ARG_TEXT, param1); fragment.setArguments(args); return fragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { mTextString = getArguments().getString(ARG_TEXT); } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (rootview == null){ rootview = inflater.inflate(R.layout.fragment_blank, container, false); } initView(); return rootview; } private void initView() { TextView textView = rootview.findViewById(R.id.text); String sa=mTextString; textView.setText(mTextString); } }
4. MyFragmentPagerAdapter
package com.example.pagerrun; import androidx.annotation.NonNull; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; import androidx.lifecycle.Lifecycle; import androidx.viewpager2.adapter.FragmentStateAdapter; import java.util.ArrayList; import java.util.List; public class MyFragmentPagerAdapter extends FragmentStateAdapter { List<Fragment> fragmentList = new ArrayList<>(); public MyFragmentPagerAdapter(@NonNull FragmentManager fragmentManager, @NonNull Lifecycle lifecycle,List<Fragment> fragments) { super(fragmentManager, lifecycle); fragmentList = fragments; } @NonNull @Override public Fragment createFragment(int position) { return fragmentList.get(position); } @Override public int getItemCount() { return fragmentList.size(); } }
5. MainActivity
package com.example.pagerrun; import androidx.appcompat.app.AppCompatActivity; import androidx.fragment.app.Fragment; import androidx.viewpager2.widget.ViewPager2; import android.os.Bundle; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { ViewPager2 viewPager2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initPager(); } private void initPager() { viewPager2 = findViewById(R.id.id_viewpager); ArrayList<Fragment> fragments = new ArrayList<>(); fragments.add(BlankFragment.newInstance("微信聊天")); fragments.add(BlankFragment.newInstance("通讯录")); fragments.add(BlankFragment.newInstance("发现")); fragments.add(BlankFragment.newInstance("我的")); MyFragmentPagerAdapter pagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), getLifecycle(),fragments);
viewPager2.setAdapter(pagerAdapter); } }