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

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

问题描述

我已经创建了我想要的视图.它有 1 个图像、一个输入框和一个按钮.单击按钮时,我想加载另一个活动.我很困惑为什么会有碎片和活动.我是 Android 世界的新手(来自 iOS).

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

我的理解是活动类似于 ViewController,但我不确定我是否理解片段是什么.

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.

这是一些示例代码,用于向您展示我的意思.

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.

此外,每个片段都是 java.lang.Object 库的扩展.所以它具有所有这些功能+附加功能.我也会阅读this.最后,为每个片段设置单独的 xml 文件是个好主意,这样您就可以在调用片段时分别显示该文件.

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.

每个片段都会/可能有这个

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.

希望这能让您对片段有所了解.还记得多读this功能使用 v7 应用程序兼容库.包括片段管理器.

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天全站免登陆