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

时间:2018-07-15 09:34:29

标签: java android android-fragments

我有一个底部导航,负责切换片段。
因此,每当我单击按钮两次时,该片段每次都会在其上重新加载内容
如何阻止呢?我只想向上滚动来重新加载页面

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 个答案:

答案 0 :(得分:0)

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

使用它来解决问题:

// 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)

希望对您有帮助!

祝你好运