为了账号安全,请及时绑定邮箱和手机立即绑定

每次单击如何停止片段重新加载?

每次单击如何停止片段重新加载?

米脂 2021-05-05 13:16:23
我有一个底部导航,它负责切换片段。因此,每当我单击按钮两次时,片段每次都会在其上重新加载内容。如何停止该片段 ?我只想向上滚动即可重新加载页面public View onCreateView(LayoutInflater inflater, ViewGroup container,                         Bundle savedInstanceState) {    // Inflate the layout for this fragment    View Layout = inflater.inflate(R.layout.fragment_profile, container, false);    ConstraintLayout profile = (ConstraintLayout) Layout.findViewById(R.id.ll);    ImageView imageView = (ImageView) Layout.findViewById(R.id.imageView3);    imageView.setClipToOutline(true);    SharedPreferences sharedPref = getActivity().getSharedPreferences("INFORMATION", 0);    String USERNAME = sharedPref.getString("user","");    TextView username = (TextView) Layout.findViewById(R.id.username);    Typeface custom_font = Typeface.createFromAsset(getActivity().getAssets(),  "fonts/Roboto Thin.ttf");    TextView desc = (TextView) Layout.findViewById(R.id.desc);    desc.setTypeface(custom_font);    username.setTypeface(custom_font);    username.setText(USERNAME);    new PostClass(getContext()).execute();    new PostClass_2().execute();    new PostClass_3().execute();    return Layout;} 每次更改片段时,都会执行以上代码。我想通过像片段中的chrome一样向上滚动来重新加载它。任何帮助表示赞赏
查看完整描述

1 回答

?
偶然的你

TA贡献1841条经验 获得超3个赞

我很久以前就遇到过这个问题:/但我找到了解决问题的简单方法:)


用这个来解决问题:


// Necessary :)

    private Context mContext;

    private View MyView;



    // Nullable

    @Override

    public View onCreateView(LayoutInflater inflater,

            @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    mContext=getActivity();

    if(MyView==null){


    // Layout View

    MyView=LayoutInflater.from(mContext).inflate(R.layout.fragment_profile, null);


    // Your views & Codes

    ConstraintLayout profile = (ConstraintLayout) MyView.findViewById(R.id.ll);

    ImageView imageView = (ImageView) MyView.findViewById(R.id.imageView3);

    imageView.setClipToOutline(true);

    SharedPreferences sharedPref = getActivity().getSharedPreferences("INFORMATION", 0);

    String USERNAME = sharedPref.getString("user","");

    TextView username = (TextView) MyView.findViewById(R.id.username);

    Typeface custom_font = Typeface.createFromAsset(getActivity().getAssets(),  "fonts/Roboto Thin.ttf");

    TextView desc = (TextView) MyView.findViewById(R.id.desc);

    desc.setTypeface(custom_font);

    username.setTypeface(custom_font);

    username.setText(USERNAME);

    new PostClass(getContext()).execute();

    new PostClass_2().execute();

    new PostClass_3().execute();




    // Stop fragment reload [ When changing ]

      } ViewGroup parent = (ViewGroup) MyView.getParent();  

      if (parent != null) {  

           parent.removeView(MyView);  

           }  

        return MyView;

    }


}

如果您要设置新的ID,请使用此选项: MyView.findViewById(R.id.SetId)


希望对您有所帮助!


查看完整回答
反对 回复 2021-05-26
  • 1 回答
  • 0 关注
  • 167 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信