如何在片段中设置contentview [英] how to setcontentview in a fragment

查看:76
本文介绍了如何在片段中设置contentview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了android studio随附的标准片段,因为我认为这将简化我的工作,因为我仍在学习Java.但是里面的代码太多了,我迷路了,不知道事情如何运作了.我似乎很了解活动的基本知识,但是经过一些研究,活动和片段之间有很大的差异

I have used the standard fragment that comes with android studio as i thought it would simplify the things for me as i am still learning java. but with too much code inside already i got lost and don't know how things work anymore. Activities i seem to understand the basics but there is a big difference between activities and fragments following some research

我正在尝试将我学到的原理应用于片段活动,但似乎不可能. 它说我想用充气机覆盖oncreateview,但是我似乎不了解其背后的概念,为什么我不能使用简单的setcontentview并从imageview和textedit中检索内容? 有什么解决方法还是对我来说最好避免碎片?

I am trying to apply the same principles i learned for activities on fragments but it doesnt seem to be possible. it says that i'm suppose to override the oncreateview with an inflater but i don't seem to understand the concept behind, why can't i use simple setcontentview and retrieve the content from the imageview and textedit? is there a workaround or is it better for me to avoid fragments ?

    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中,因此不能与Fragment分开使用.您不能将Fragment用作Activity的完全替代品,但可以将Fragment用作Activity的一部分,也可以在另一个Activity中重用.

Because Fragment is located inside an Activity, and can not be used apart from one. You can't use Fragment as full replacement of Activity but you can use Fragment as a part of your Activity that can be reused in another activity too.

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

You need to call onCreateView() because the system calls this when it's time for the fragment to draw its user interface for the first time. To draw a UI for your fragment, you must return a View from this method that is the root of your fragment's layout. You can return null if the fragment does not provide a UI.

要进一步了解Fragment,您应该阅读官方文档

For further understanding of Fragment you should read the official documentation

这篇关于如何在片段中设置contentview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆