1.fragment优点
之前这篇博客ViewPager实现引导页中,是用PagerAdapter的子类实现的引导页。
因为要切换不同的View,所以在初始化Adapter的时候,就把所有View设置好了。
当View数量比较少的时候不成问题,但当引导页要切换上百次时,就会造成内存开销过大的问题。
所以我们可以用fragment来实现引导页,这样内存里一次最多保存两三个View,内存开销小。
2.代码实现
实现FragmentPagerAdapter的子类:
package com.example.chapter08;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import com.example.chapter08.fragment.LaunchFragment;
public class ImprovedLaunchAdapter extends FragmentPagerAdapter {
public int[] list;
public ImprovedLaunchAdapter(@NonNull FragmentManager fm, int[] images) {
super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
this.list = images;
}
@NonNull
@Override
public Fragment getItem(int position) {
return LaunchFragment.newInstance(list.length, position,list[position]);
}
@Override
public int getCount() {
return list.length;
}
}
fragment类的实现:
package com.example.chapter08.fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import com.example.chapter08.PagerTabActivity;
import com.example.chapter08.R;
/**
* A simple {@link Fragment} subclass.
* Use the {@link LaunchFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class LaunchFragment extends Fragment {
public static LaunchFragment newInstance(int count, int position, int image_id) {
LaunchFragment fragment = new LaunchFragment();
Bundle args = new Bundle();
args.putInt("count", count);
args.putInt("position", position);
args.putInt("image_id", image_id);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_launch, container, false);
Bundle bundle = getArguments();
int image_id = bundle.getInt("image_id");
int count = bundle.getInt("count");
int position = bundle.getInt("position");
Context context = getContext();
ImageView iv = view.findViewById(R.id.iv);
iv.setImageResource(image_id);
RadioGroup rg = view.findViewById(R.id.rg);
for(int j=0;j<count;j++){
RadioButton rb = new RadioButton(context);
rb.setLayoutParams(
new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
)
);
rg.addView(rb);
}
((RadioButton)rg.getChildAt(position)).setChecked(true);
if(position==count-1){
Button btn = view.findViewById(R.id.btn);
btn.setVisibility(View.VISIBLE);
btn.setOnClickListener(v->{
Toast.makeText(context, "Enter the APP", Toast.LENGTH_SHORT).show();
context.startActivity(new Intent(context, PagerTabActivity.class));
});
}
return view;
}
}