我已经创建了我想要的外观。它具有1个图像,一个输入框和一个按钮。单击该按钮时,我将要加载另一个 Activity 。我很困惑为什么会有 fragment 和 Activity 。我是Android世界的新手(来自iOS)。
我的理解是, Activity 类似于ViewControllers,但是我不确定我理解 fragment 是什么。
我应该在哪里处理事件?
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;
}
}
}
最佳答案
首先,我建议阅读此Fragments。特别注意创建的 fragment 部分,其中包括 fragment 生命周期图。第二次下载并编译此Sample App,有效的导航应用程序将帮助您了解不同 fragment 如何串联工作,甚至实现操作栏。
要或多或少地回答您的问题,可以将一个 fragment 视为一个单独的类。一旦调用了该特定 fragment ,就可以从该类中调用函数。
fragment 大小写切换
这是一些示例代码,向您展示我的意思。
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;
}
}
在这种情况下,对我来说,每个 fragment 都代表一个类,该类在单独的选项卡中实现,并且每个选项卡都具有单独的功能。 fragment 的主要优点之一是您可以运行单独的 Activity ,而无需先完成一个 Activity 。此外,每个 fragment 都是java.lang.Object库的扩展。因此,它具有所有这些功能以及其他功能。我也会读this。最后,最好为每个 fragment 有单独的xml文件,然后在调用 fragment 时可以分别显示该文件。
一些代码
每个 fragment 都会/可能有这个
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();
}
请记住,这些功能来自我之前提到的 fragment 生命周期。希望这能使您对 fragment 有所了解。还请记住要阅读this,因为许多功能都使用v7应用程序compat库。包括fragment manager。