片段和活动 - 我在哪里把我的应用程序逻辑? [英] Fragments and Activities -- where do I put my application logic?

查看:138
本文介绍了片段和活动 - 我在哪里把我的应用程序逻辑?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了我的观点我多么希望它看起来。它有1张图片,一个输入框和一个按钮。我将要加载另一个活动是单击按钮时。我很困惑,为什么有碎片和活动。我是新来的机器人世界(来自iOS的未来)。

我的理解是,活动类似于ViewControllers,但我不知道我理解什么是片段。

我在哪里把事件处理?

 包com.phppointofsale.phppointofsale;

进口android.support.v7.app.ActionBarActivity;
进口android.support.v7.app.ActionBar;
进口android.support.v4.app.Fragment;
进口android.os.Bundle;
进口android.view.LayoutInflater;
进口android.view.Menu;
进口android.view.MenuItem;
进口android.view.View;
进口android.view.ViewGroup;
进口android.os.Build;

公共类StoreUrlActivity扩展ActionBarActivity {

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_store_url);

        如果(savedInstanceState == NULL){
            getSupportFragmentManager()的BeginTransaction()
                    。新增(R.id.container,新StoreUrlFragement())提交()。
        }
    }

    @覆盖
    公共布尔onCreateOptionsMenu(功能菜单){

        //充气菜单;这增加了项目操作栏,如果它是present。
        。getMenuInflater()膨胀(R.menu.store_url,菜单);
        返回true;
    }

    @覆盖
    公共布尔onOptionsItemSelected(菜单项项){
        //处理动作栏项目点击这里。将操作栏
        //自动在主/向上按钮操作的点击,只要
        //你在AndroidManifest.xml中指定一个父活动。
        INT的id = item.getItemId();
        如果(ID == R.id.action_settings){
            返回true;
        }
        返回super.onOptionsItemSelected(项目);
    }

    / **
     *包含一个简单的视图的占位符片段。
     * /
    公共静态类StoreUrlFragement扩展片段{

        公共StoreUrlFragement(){
        }

        @覆盖
        公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
                捆绑savedInstanceState){
            查看rootView = inflater.inflate(R.layout.fragment_store_url,
                    集装箱,假);
            返回rootView;
        }
    }

}
 

解决方案

首先,我会推荐阅读的片段的。要特别注意所创建的片段部分,其中包括片段生命周期图。其次下载和编译此示例应用程序,有效的导航应用将帮助你理解碎片是如何不同的协同工作,甚至实现了一个操作栏。

要回答你的问题或多或少的片段可以被看作是一个单独的类。一旦你那个特定的片段呼吁你可以从这个类中调用的函数。

片段案例开关

这是一些示例code告诉你我是什么意思。

 公开片段的getItem(int i)以{
    开关(ⅰ){
        情况下0:
            //应用程序的第一部分是最有趣的 - 它提供
            //一个启动板成这个示例应用程序的其他示威。
            返回新LaunchpadSectionFragment();

        情况1:
            返回新BluetoothClass();

        默认:
            //应用程序的GPS部分。
            片段片段=新DummySectionFragment();
            捆绑的args =新包();
            args.putInt(DummySectionFragment.ARG_SECTION_NUMBER,I + 1);
            fragment.setArguments(参数);
            返回片段;
        }
}
 

在这种情况下,每个片段我重新presented一类,它是在一个单独的标签来实现,每个标签有一个单独的功能。其中一个片段的主要优点是可以运行独立的活动,没有首先让一个活动完成了。

此外每个片段是java.lang.Object中文库的延伸。因此,它拥有所有这些功能+附加的。我会读的为好。最后这将是有单独的XML文件中的每个片段则可以单独显示调用片段时,一个好主意。

一些较code

每个片断将/能有这种

 公共无效onActivityCreated(包savedInstanceState){
        super.onActivityCreated(savedInstanceState);
            //做的东西的创造。这通常是在其中添加你的大部分code。像clickListners

        查看rootview = inflater.inflate(R.layout.xml_the_fragment_uses容器,FALSE);
        rootview.findViewById(R.id.your_id).setOnClickListener(新View.OnClickListener(){
        @覆盖
        公共无效的onClick(视图v){
            //做一点事
        }
    });
}


    公共无效的OnStart(){
        super.onStart();
        Toast.makeText(getActivity(),碎片开始,Toast.LENGTH_SHORT).show();
    }

    公共无效onResume(){
        super.onStart();
        Toast.makeText(getActivity(),碎片续会,Toast.LENGTH_SHORT).show();

    }

    公共无效的onStop(){
        super.onStart();
        Toast.makeText(getActivity(),碎片罢,Toast.LENGTH_SHORT).show();
        disableBT();
    }
 

记住这些功能都是从片段生命周期我前面提到的。

希望这给你的碎片一些了解。还记得读作为很多功能使用V7应用程序兼容性库。包括片段经理

I have created my view how I want it to look. It has 1 images, an input box, and a button. I will want to load another activity when the button is clicked. I am confused why there are fragments and activities. I am new to the Android world (coming from iOS).

My understanding is that Activities are similar to ViewControllers, but I am not sure I understand what a fragment is.

Where do I put the event handling?

package com.phppointofsale.phppointofsale;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;

public class StoreUrlActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_store_url);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new StoreUrlFragement()).commit();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.store_url, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class StoreUrlFragement extends Fragment {

        public StoreUrlFragement() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_store_url,
                    container, false);
            return rootView;
        }
    }

}

解决方案

Firstly I would recommend reading this Fragments . Pay particular attention to the created fragment section, which includes the fragment life-cycle diagram. Second download and compile this Sample App,the effective navigation app will help you understand how different fragments work in tandem, and even implements a action bar.

To answer your question more or less a fragment can be thought of as a separate class. Once you call upon that particular fragment you can call functions from within that class.

Fragment Case-Switch

This is some sample code to show you what I mean.

 public Fragment getItem(int i){
    switch (i) {
        case 0:
            // The first section of the app is the most interesting -- it offers
            // a launchpad into the other demonstrations in this example application.
            return new LaunchpadSectionFragment();

        case 1:
            return new BluetoothClass();

        default:
            // The GPS section of the app .
            Fragment fragment = new DummySectionFragment();
            Bundle args = new Bundle();
            args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1);
            fragment.setArguments(args);
            return fragment;
        }
}

In this case each fragment for me represented a class, which was implemented in a separate tab and each tab had a separate functionality. One of the key advantages of fragments is you can run separate activities without first letting one activity complete.

Furthermore each fragment is an extension of the java.lang.Object library. So it has all those functions + additional ones. I would read this as well. Lastly it would be a good idea to have separate xml files for each fragment then you can display that separately when a fragment is invoked.

Some more code

Each fragment will/could have this

public void onActivityCreated(Bundle savedInstanceState){
        super.onActivityCreated(savedInstanceState);
            // Do stuff on creation. This is usually where you add the bulk of your code. Like clickListners

        View rootview = inflater.inflate(R.layout.xml_the_fragment_uses container,false);
        rootview.findViewById(R.id.your_id).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Do something 
        }
    });
}


    public void onStart(){
        super.onStart();
        Toast.makeText(getActivity(), "Fragment started",Toast.LENGTH_SHORT).show();
    }       

    public void onResume(){
        super.onStart();
        Toast.makeText(getActivity(), "Fragment Resumed",Toast.LENGTH_SHORT).show();

    }

    public void onStop(){
        super.onStart();
        Toast.makeText(getActivity(), "Fragment Stoped",Toast.LENGTH_SHORT).show();
        disableBT();
    }

Remember these functions are from the fragment life-cycle I mentioned earlier.

Hopefully that gave you some idea on fragments. Also remember to read this as a lot of functionality uses the v7 app compat library. Including the fragment manager.

这篇关于片段和活动 - 我在哪里把我的应用程序逻辑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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