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

8次阅读

共计 4075 个字符,预计需要花费 11 分钟才能阅读完成。

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

参考 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 上下文环境查阅 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