Android 用户个人中心开发记录(一)

最近开始开发毕业论文所需的app的前台界面
写到用户个人界面这里,记录一些坑,由于我边学边写的,可能有些地方会出现错误
首先我用TabLayout + ViewPager + Fragment搭建好了整个应用的主界面框架。每个界面就是一个Fragment,在查阅google和baidu之后,据说这种搭配会造成数据保存上的麻烦,暂且搁置,后面搭建后台时再进行修改
clipboard.png


参考CDSN上面的一篇博客 https://blog.csdn.net/asfang/...
然后,在写用户界面的时候决定照着这篇文章去做,用下面两种开源库去加载图片

    implementation 'com.github.bumptech.glide:glide:3.7.0'
    implementation 'jp.wasabeef:glide-transformations:2.0.1'

用户界面的XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <!--个人信息-->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/h_back"
            android:layout_width="match_parent"
            android:layout_height="200dp" />

        <ImageView
            android:id="@+id/h_front"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_centerInParent="true" />

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@id/h_back"
            android:layout_marginBottom="20dp">

            <ImageView
                android:id="@+id/user_line"
                android:layout_width="1dp"
                android:layout_height="25dp"
                android:layout_centerHorizontal="true"
                android:layout_marginStart="15dp"
                android:background="@color/colorWhite" />

            <TextView
                android:id="@+id/user_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toStartOf="@id/user_line"
                android:text="Profile Fragment"
                android:textColor="@color/colorWhite"
                android:textSize="17sp" />

            <TextView
                android:id="@+id/user_val"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="15dp"
                android:layout_toEndOf="@id/user_line"
                android:text="phone_num"
                android:textColor="@color/colorWhite"
                android:textSize="17sp" />
        </RelativeLayout>
    </RelativeLayout>
</LinearLayout>

思路:

  • 最外层是一个LinearLayout,orientation为vertical,包含所有的组件
  • 一个RelativeLayout包含图片和用户
  • 因为最后的效果是后面是虚化的图片,前面是一个圆形的头像,所以在这里先放两个ImageView
  • 中间再加入一个RelativeLayout紧贴着圆形头像下方

/**
 * 个人信息和设置页卡Fragment
 */
public class ProfileFragment extends Fragment {
    private static final String ARG_FROM = "From";
    private String mFrom;
    private TextView mTextView;
    private ImageView blurImageView;
    private ImageView avatarImageView;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.profile_fragment, container, false);
        blurImageView = (ImageView) view.findViewById(R.id.h_back);
        avatarImageView = (ImageView) view.findViewById(R.id.h_front);
        Glide.with(getActivity()).load(R.drawable.person_pic).bitmapTransform(new BlurTransformation(getActivity(), 25), new CenterCrop(getActivity())).into(blurImageView);
        Glide.with(getActivity()).load(R.drawable.person_pic).bitmapTransform(new CropCircleTransformation(getActivity())).into(avatarImageView);
        return view;
    }

    public static ProfileFragment newInstance(String from) {

        Bundle args = new Bundle();
        args.putString(ARG_FROM, from);
        ProfileFragment fragment = new ProfileFragment();
        fragment.setArguments(args);
        return fragment;
    }
}

这是我第一次写好的初始化界面代码。
因为我是在Fragment中加载的,之前的文章是直接在Activity中写的,所以获取上下文对象原文可以直接用this关键字,我这里用了getActivity()代替,发现在Glide加载图片的第一行中出现了空指针异常,然后用getContext()getActivity().getApplicationContext()getContext().getApplicationContext()都不行,因为这四句都一样的效果,使得Fragment能获得绑定的Activity上下文环境
出现这种情况的原因是当activity实例在后台时, 系统在回收资源时很可能回收掉Activity,  并在onSaveInstanceState函数里保存fragment的状态, 再次打开该Activity时, 在onCreate方法里取出bundle里的fragment状态, 但这时fragment对应的Activity早就不在了, 所以getActivity为空。
查阅Google,发现重写onCreate()onDetach()之后才能保证不会崩溃

public class ProfileFragment extends Fragment {

    public Context mContext;
    
    ...
    
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getContext();
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mContext = null;
    }
}

还有另外一种方法能解决getActivity()null的情况,参考自https://blog.csdn.net/wdd1324...
恢复Fragment之前把保存Bundle里面的数据给清除。赶在Activity恢复其之前所绑定的Fragment之前清除所有存储在savedInstanceState中的信息。方法如下:

    if (savedInstanceState != null) {  
            savedInstanceState.putParcelable("android:support:fragments", null); 
    //或者
    //String FRAGMENTS_TAG = "Android:support:fragments";
    // remove掉保存的Fragment
    // savedInstanceState.remove(FRAGMENTS_TAG);
        }  
    super.onCreate(savedInstanceState); 

activity中
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        //super.onSaveInstanceState(outState);
    }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值