如何在FragmentTabHost的片段之间传递数据?

时间:2016-02-05 13:59:19

标签: android android-fragments

我正在尝试学习android并且遇到了这个项目的问题。我已经设置了一个包含四个片段的片段标签主机。我想要做的是让前三个片段与用户交互,当他们切换到第四个选项卡时,它将显示他们放在前三个中的所有信息。我想我可以将onPause()上的信息覆盖作为触发器传递,因为我不想使用按钮按下。现在,我只是想让EditText工作以确保我做的一切正常。我不确定我是否正确使用片段事务,或者我正在尝试收集编辑文本字段。后来我希望通过捆绑传递信息。任何帮助,将不胜感激。

主:

package valdes.fragmenttabsmenu;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.app.FragmentTransaction;

public class MainActivity extends FragmentActivity implements WelcomeFragment.WelcomeListener {

private FragmentTabHost mTabHost;
private String firstName;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
    mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);
    mTabHost.addTab(mTabHost.newTabSpec("Home").setIndicator("Home", null), WelcomeFragment.class, null);
    mTabHost.addTab(mTabHost.newTabSpec("Demographics").setIndicator("Demographics", null), DemographicsFragment.class, null);
    mTabHost.addTab(mTabHost.newTabSpec("Questions").setIndicator("Questions", null), QuestionsFragment.class, null);
    mTabHost.addTab(mTabHost.newTabSpec("Overview").setIndicator("Overview", null), OverviewFragment.class, null);
}


@Override
public void getFristName(String first_Name){
    firstName = first_Name;
    OverviewFragment fragment = new OverviewFragment();
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    fragment.setFirstName(firstName);
    ft.addToBackStack(null);
    ft.commit();

}
}

片段1 - 获取信息

package valdes.fragmenttabsmenu;


import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;


public class WelcomeFragment extends Fragment {


public WelcomeFragment() {
    // Required empty public constructor
}

interface WelcomeListener{
    public void getFristName(String firstName);

}

private WelcomeListener listener;
private String firstName;

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


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_welcome, container, false);
    EditText editText = (EditText) view.findViewById(R.id.first_name);
    firstName = editText.getText().toString();
    return view;
}

@Override
public void onAttach(Context context){
    super.onAttach(context);
    this.listener = (WelcomeListener)context;
}



@Override
public void onPause(){
    super.onPause();
   if(listener != null){
       listener.getFristName(firstName);
   }
}
}

Fragment 1 XML

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

<TextView
    android:id="@+id/welcome"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_gravity="center_horizontal"
    android:textSize="25sp"
    android:text="@string/welcome_message"/>


<EditText
    android:id="@+id/first_name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/first_name"/>

<EditText
    android:id="@+id/last_name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/last_name"/>

<Spinner
    android:id="@+id/birthday"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:entries="@array/months"/>


</LinearLayout>

片段2 - 获取信息

package valdes.fragmenttabsmenu;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


public class OverviewFragment extends Fragment {

private String firstName;
private String lastName;


public OverviewFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_overview, container, false);
}

@Override
public void onStart(){
    super.onStart();
    View view = getView();
    if(view != null){
        TextView name = (TextView) view.findViewById(R.id.OV_name);
        name.setText(firstName);

    }
}

public void setFirstName(String firstName){this.firstName = firstName;}

}

Fragment 2 XML

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

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_gravity="center_horizontal"
    android:textSize="25sp"
    android:layout_weight="1"
    android:text="@string/overview"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:layout_gravity="center_horizontal"
    android:textSize="40sp"
    android:text="Responses go here"/>

<TextView
    android:id="@+id/OV_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="20sp"
    android:text="TEST"/>

<Button
    android:id="@+id/submit_button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/submit"/>

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

为片段之间的get / set数据创建一个Listner,并在像这样的

这样的活动上实现它

InteractionLister.java

public interface InteractionLister {

    void setData(String data);

    String getData();

}

MainActivity.java

public class TabActivity extends Activity implements InteractionLister{

    private String mData;

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

    @Override
    public void setData(String data) {
        this.mData = data;
    }

    @Override
    public String getData() {
        return mData;
    }
}

TabFragment1.java

public class TabFragment1 extends Fragment {
    InteractionLister interactionLister;
    private static final String TAG = TabFragment1.class.getSimpleName();

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        Log.w(TAG,"Data from other fragment " + interactionLister.getData());

    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        interactionLister = (InteractionLister) activity;
    }
}