简体   繁体   English

将我的应用程序转换为片段:如何处理活动?

[英]Convert my application to Fragments: How to handle activities?

I'm planning to convert an existing android application to fragments layout. 我打算将现有的android应用程序转换为片段布局。

The idea is to have the classical two panel layout (item list on left, and details on right). 这个想法是采用经典的两个面板布局(项目列表在左侧,细节在右侧)。

Actually the application is composed by 4 activites: 实际上,该应用程序由4个活动组成:

  1. A ChoiceListActivity with all the available options 具有所有可用选项的ChoiceListActivity
  2. 3 different activities, one for each operation available on the tool. 3种不同的活动,工具上的每个操作一项。

Now i started to work on the conversion and i created a FragmentActivity classs, that is the main class: 现在,我开始进行转换,并创建了FragmentActivity类,这是主要的类:

    public class MainFragment extends FragmentActivity {

    private static final String TAG = "MainFragment";

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_main);
        if(findViewById(R.id.fragment_container)!=null){
            Log.i(TAG, "No Tablet");
            Intent i = new Intent(MainFragment.this, main.ChoiceActivity.class);
            startActivity(i);
        } else {
            Log.i(TAG, "Tablet");
        }
    }
}

And i created a ChoiceListFragment: ` 我创建了ChoiceListFragment:

public class ChoiceListFragment extends ListFragment {

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        Toast.makeText(getActivity(), getListView().getItemAtPosition(position).toString(), Toast.LENGTH_LONG).show();
        super.onListItemClick(l, v, position, id);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);     
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        String[] options = getResources().getStringArray(R.array.listitems);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(inflater.getContext(), R.layout.list_item, options);
        setListAdapter(adapter);
        return super.onCreateView(inflater, container, savedInstanceState);
    }

}

That fragment will be the left side of the panel. 该片段将在面板的左侧。

My problem is for the right side. 我的问题是在右边。 The idea is that for every element of the list the corresponding activity (or fragment?) will be shown. 想法是,将为列表中的每个元素显示相应的活动(或片段?)。

So what is the correct way? 那么正确的方法是什么?

  1. Is a good idea to start an activity in the right fragment when the user select an item? 当用户选择项目时,以正确的片段开始活动是个好主意吗?

  2. Or i must switch between fragments programmatically? 还是我必须以编程方式在片段之间切换? And how to do that (i found many tutorials, but they use always the same activity for the right panel changing some data inside it)? 以及如何做到这一点(我找到了很多教程,但是他们在右侧面板中始终使用相同的活动来更改其中的一些数据)?

I have created the following class for the right fragment (but i'm not sure that i'm doing it correctly): 我已经为正确的片段创建了以下类(但是我​​不确定自己是否正确执行了该类):

public class RightFragment extends Fragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

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


}

I noticed that i can eventually change the layout using the LayoutInflater object during onCreate method, but this simply siwtch the layout on the screen, The objects declared in the layout aren't initialized (nor eventListener added, etc). 我注意到我最终可以在onCreate方法期间使用LayoutInflater对象更改布局,但这只是在屏幕上显示布局,而在布局中声明的对象未初始化(也未添加eventListener,等等)。 So how to do that? 那么该怎么做呢?

Maybe i should Create an Intent and use startActivity to launch the existing activities, or this is a bad idea into a fragment? 也许我应该创建一个Intent并使用startActivity来启动现有活动,或者这是一个坏主意吗?

Actually the xml layout is: 实际上,xml布局是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <fragment
        android:id="@+id/choicelist_fragment"
        android:name="main.fragments.ChoiceListFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <fragment
        android:id="@+id/right_fragment"
        android:name="main.fragments.RightFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2" />

</LinearLayout> 

Ok i found myself the solution, it was not clear on a first moment, but reading some documentation, looking at many tutorials maybe i understand how it works. 好的,我找到了解决方案,一开始不清楚,但是阅读一些文档,看了许多教程,也许我知道它是如何工作的。

First of all i removed the second fragment *right_fragment* from the layout (check the question), i replaced it with an empty FrameLayout called *activity_container* that will be the container of my fragments. 首先,我从布局中删除了第二个片段* right_fragment *(检查问题),我用一个名为* activity_container *的空FrameLayout替换了它,它将成为我的片段的容器。

The idea behind is simply use the FragmentManager to replace the fragment inside the container. 背后的想法只是使用FragmentManager替换容器内的片段。

So i updated the onListItemClick method into the ChoiceListFragment, and depending on what is the list item tapped, it creates a new Fragment and replace it into the *activity_container*. 因此,我将onListItemClick方法更新为ChoiceListFragment,然后根据所点击的列表项,创建一个新的Fragment并将其替换为* activity_container *。 The updated method is similar to the following: 更新的方法类似于以下内容:

public void onListItemClick(ListView l, View v, int position, long id) {            
        String itemName = getListView().getItemAtPosition(position).toString();
        switch(position){
        case OPTION_ONE:                getFragmentManager().beginTransaction().replace(R.id.activity_container, new OptionOneFragment()).commit();
            break;
        case RESISTOR_VALUE:
            getFragmentManager().beginTransaction().replace(R.id.activity_container, new OptionTwoFragment()).commit();
            break;
        default:
            Toast.makeText(getActivity(), getListView().getItemAtPosition(position).toString(), Toast.LENGTH_LONG).show();
            break;
        }       
        super.onListItemClick(l, v, position, id);
    }

In that way every component of the application has its own fragment, handled by a different class. 这样,应用程序的每个组件都有自己的片段,由不同的类处理。

You're on the right track. 您走在正确的轨道上。 On small screens, clicking a list item starts a DetailActivity, which is a simple wrapper around a DetailFragment. 在小屏幕上,单击列表项将启动一个DetailActivity,这是一个对DetailFragment的简单包装。 On a larger screen, clicking the list item would replace the right hand side with a new instance of DetailFragment. 在更大的屏幕上,单击列表项将用新的DetailFragment实例替换右侧。

If you are using eclipse and ADT, I would suggest taking a look at the MasterDetailFlow template, which can be accessed by creating a new Android project or a new Android Activity. 如果您使用的是eclipse和ADT,建议您看一下MasterDetailFlow模板,可以通过创建新的Android项目或新的Android Activity来访问它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM