gpt4 book ai didi

java - 如何在 fragment 中设置内容 View

转载 作者:行者123 更新时间:2023-12-01 16:55:09 27 4
gpt4 key购买 nike

我使用了 android studio 附带的标准 fragment ,因为我认为它会简化我的事情,因为我仍在学习 java。但里面的代码太多了,我已经迷路了,不知道事情是如何工作的了。 Activity 我似乎了解基础知识,但经过一些研究, Activity 和 fragment 之间存在很大差异

我正在尝试应用我在 fragment Activity 中学到的相同原则,但这似乎不可能。它说我应该用充气机覆盖 oncreateview 但我似乎不理解背后的概念,为什么我不能使用简单的 setcontentview 并从 imageview 和 textedit 中检索内容?有解决方法还是避免 fragment 对我来说更好?

    package com.venomdev.safestorage;

import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;


/**
* A simple {@link Fragment} subclass.
* Activities that contain this fragment must implement the
* {@link EncryptFiles.OnFragmentInteractionListener} interface
* to handle interaction events.
* Use the {@link EncryptFiles#newInstance} factory method to
* create an instance of this fragment.
*
*/
public class EncryptFiles extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
ImageView UploadFile;
Button bUpload;
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;

private OnFragmentInteractionListener mListener;

/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment EncryptFiles.
*/
// TODO: Rename and change types and number of parameters
public static EncryptFiles newInstance(String param1, String param2) {
EncryptFiles fragment = new EncryptFiles();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
public EncryptFiles() {
// Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.UploadFile);

UploadFile = (ImageView) findViewById(R.id.UploadFile);


if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_encrypt_files, container, false);
}

// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}

@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if (activity instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) activity;
} else {
throw new RuntimeException(activity.toString()
+ " must implement OnFragmentInteractionListener");
}
}

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

/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}

最佳答案

因为Fragment位于Activity内部,不能脱离Activity使用。您不能使用 Fragment 作为 Activity 的完全替代,但您可以使用 Fragment 作为 Activity 的一部分,也可以在另一个 Activity 中重用。

您需要调用onCreateView(),因为系统会在 fragment 第一次绘制其用户界面时调用此函数。要为 fragment 绘制 UI,您必须从此方法返回一个 View ,该 View 是 fragment 布局的根。如果 fragment 不提供 UI,则可以返回 null。

要进一步了解 Fragment,您应该阅读 official documentation

关于java - 如何在 fragment 中设置内容 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34079194/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com