Andriod开发 用fragment实现引导页

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;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值