机器人如何与片段标签交互 [英] Android how to interact with Fragment tabs

查看:117
本文介绍了机器人如何与片段标签交互的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我有困难改变片段活动的可视化视图。我创建的所有选项卡,但的onClick我不知道如何将活动传递到该空白。

Hello I am having difficulty changing the visual view of the fragment activity. I created all the tabs but onClick I do not know how to pass the activity into that white space.

所以这种结构被命名为这只是设置的内容视图到一个简单的活动 maintabholder.xml 持有的片段来看,连接到 MainNav ,其内容视图包含所有选项卡底部指定一个类的文档。

so the structure of this is a simple activity named Main which just sets the content view to a maintabholder.xml document that holds the fragment view which connects to a class named MainNav whose content view contains all the tabs at the bottom.

maintabholder.xml

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>

<include layout="@layout/header" android:layout_alignParentTop="true"           android:id="@+id/headerArea"></include>
<fragment
    class="my.package.name.MainNav"
    android:id="@+id/fragment_tabs"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />
</LinearLayout>

MainNav.java code段

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) 
{
    mRoot = inflater.inflate(R.layout.fragment_tabs, null);
    mTabHost = (TabHost) mRoot.findViewById(android.R.id.tabhost);
    setupTabs();
    return mRoot;
}

和R.layout.fragment_tabs:

and R.layout.fragment_tabs:

<?xml version="1.0" encoding="utf-8"?>
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#EFEFEF">

<RelativeLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TabWidget
        android:id="@android:id/tabs"
        android:layout_alignParentBottom="true"
        android:layout_width="fill_parent"
        android:background="@drawable/tab_background"
        android:gravity="center_horizontal"
        android:layout_height="wrap_content" >
    </TabWidget>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"

            android:layout_height="wrap_content">

            <FrameLayout
                android:id="@+id/tab_1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <FrameLayout
                android:id="@+id/tab_2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
             <FrameLayout
                android:id="@+id/tab_3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
              <FrameLayout
                android:id="@+id/tab_4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
               <FrameLayout
                android:id="@+id/tab_5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

        </FrameLayout>
</RelativeLayout>

和最后一个活动的,我要加载到的片段(在pressing的TAB1按钮)的内容

and Finally the contents of an Activity that I want to load into the Fragment (upon pressing the tab1 button)

 public class HomeFragment extends Activity {


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home);

    }
 }

这是帮助你得到的东西的要点,但发生在这里MainNav.java问题:

That is to help you get the gist of things, but the problem occurs here in MainNav.java:

  @Override
   public void onTabChanged(String tabId) {
        Log.d(TAG, "onTabChanged(): tabId=" + tabId);
        if (TAB_HOME.equals(tabId)) {
            updateTab(tabId, R.id.tab_1);
            mCurrentTab = 0;
            return;
        }
   }

一个选项卡时,它被注册在 onTabChanged 的活动,而 updateTab 函数被调用:

when a tab is clicked, it is registered in the onTabChanged activity, and the updateTab function is called:

   private void updateTab(String tabId, int placeholder) {
        FragmentManager fm = getFragmentManager();
        if (fm.findFragmentByTag(tabId) == null) {
            //fm.beginTransaction()
             //      .replace(placeholder, /*need fragment object here*/ , tabId)
             //       .commit();
            //
        }
}

注释掉替换方法查找(INT,分片,字符串),我想这应该然后改变空格我。

the commented out replace method is looking for an (int, fragment, string) and I guess it should then change the white space for me.

但我不明白的地方我从片段的对象!这种幻象对象需要以某种方式包含我的 HomeFragment 的XML布局,让我与其他一切我已经codeD的 HomeFragment互动的活动。

but I don't understand where I get the fragment object from! This phantom object needs to somehow contain my HomeFragment's xml layout and allow me to interact with everything else I've coded for the HomeFragment activity.

我的第一直觉是,我的 HomeFragment 活动需要延长FragmentActivity 而不是 只是活动。但是,这是我打了一堵墙。

My first hunch is that my HomeFragment activity needs to extend FragmentActivity instead just activity. But this is where I am hitting a wall.

对于所用兼容包的方法非常少的文件,但TabActivity的文件说,不使用TabActivity了,所以我在这里。开发的Andr​​oid 2.2 +

There is very little documentation on the compatibility pack methods, but the documentation for TabActivity says not to use TabActivity anymore, so here I am. Developing for Android 2.2+

洞察AP preciated。

Insight appreciated.

推荐答案

您不能把活动成片段,它的突出部分的简单。你必须把你的活动,服务于制表内容成了碎片。它不应该是一个问题,因为你没有在这些活动中使用的碎片。

You can't put Activities into Fragments, it's jut that simple. You have to convert your Activities that serve the tab-contents into Fragments. It shouldn't be a problem, given that you don't use fragments in those Activities.

因此​​,这里有2规律可循:

So here are 2 rules to follow:

  1. 您不能把活动分为片段。
  2. 您不能把碎片成片段。

我建议您保留上布局底部周围的片段只是为了处理制表开关的选项卡,小部件。如果标签交换发生,则报告给父活动,这将加载相应片段插入是制表片段的上方。对于这个工作,你必须修改你布局,使它具有制表碎片中的头的上方和下方的ViewGroup。你会动态地加载与制表内容的碎片进入了一个ViewGroup。

I recommend that you keep that bottom fragment on your layout around just to handle the tab-switches in the tab-widget. If a tab switch happens, you report this to the parent Activity, which will load the corresponding Fragment into the are above the tab-fragment. For this to work, you'd have to modify you layout so it has a ViewGroup above the tab-fragment and below the header. You'd dynamically load the fragments with the tab-contents into that ViewGroup.

下面的布局(maintabholder.xml):

Here's the layout (maintabholder.xml):

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>

<include layout="@layout/header" android:layout_alignParentTop="true"           android:id="@+id/headerArea"></include>

<FrameLayout android:id="@+id/TabContent"
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="1"/>

<fragment
    class="my.package.name.MainNav"
    android:id="@+id/fragment_tabs"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
</LinearLayout>

在制表开关的底部片段发生了,你把这事告诉了父母活动:

When a tab-switch happens in your bottom fragment, you tell this to the parent Activity:

...
(YourActivity)getActivity().switchToTab(tabId);
...

而在YourActivity:

And in the YourActivity:

...
public void switchToTab(int tabId){
    switch(tabId){
    ...
    case 2:
        getSupportFragmentManager().beginTransaction().replace(R.id.TabContent, TabContentFragment2.newInstance(), "someTag").commit().
    break;
    ...
    }
}

类似的东西。

Something like that.

这篇关于机器人如何与片段标签交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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