如何显示另一个片段中的一个片段的文本?

时间:2016-05-06 19:55:10

标签: android android-fragments

我正在通过这个例子工作你只是认为我是重复的,阅读整篇文章,TY

我通过使用android studio提供的模板制作一个带滑动菜单的简单应用程序。我必须使用片段在滑动菜单的项目之间切换。

我想做什么? (查看底部的应用程序设计)

当我点击按钮传递数据时,它应该将片段B中的文本设置为我在片段A中输入的文本但不直接进入片段B.所以当我按下按钮传递数据然后我想去到滑动菜单并选择包含片段B的项目,然后我想从片段A看到文本。提前感谢。

public class MainActivity extends AppCompatActivity implements FragmentA.DataPassListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem item) {
            int id = item.getItemId();

            if (id == R.id.nav_camera) {
                // Handle the camera action

                displayFragmentA();
            } else if (id == R.id.nav_gallery) {

                displayFragmentB();

            } else if (id == R.id.nav_slideshow) {

            } else if (id == R.id.nav_manage) {

            } else if (id == R.id.nav_share) {

            } else if (id == R.id.nav_send) {

            }

            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            drawer.closeDrawer(GravityCompat.START);
            return true;
        }
    });
}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, 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();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}


@Override
public void passData(String data) {

    FragmentB fragmentB = new FragmentB();
    Bundle args = new Bundle();
    args.putString("data", data);
    fragmentB.setArguments(args);
    //getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, fragmentB).commit();
    //it works with the commented part but I dont wanna go directly to the fragment when I press pass data button


}

public void displayFragmentA() {

    FragmentA frag = new FragmentA();
    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.fragment_container, frag);
    fragmentTransaction.commit();


}

public void displayFragmentB() {

    FragmentB frag = new FragmentB();
    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.fragment_container, frag);
    fragmentTransaction.commit();


}
}

片段代码:

public class FragmentA extends Fragment {

DataPassListener mCallback;

public interface DataPassListener {
    public void passData(String data);
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    // Make sure that container activity implement the callback interface
    try {
        mCallback = (DataPassListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement DataPassListener");
    }
}


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


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_a, container, false);

    final EditText input = (EditText) view.findViewById(R.id.etInputID);

    Button passDataButton = (Button) view.findViewById(R.id.bPassDataID);
    passDataButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            mCallback.passData(input.getText().toString());

        }
    });


    return view;
}

}

片段B代码:

public class FragmentB extends Fragment {

TextView showReceivedData;


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


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_fragment_b, container, false);

    showReceivedData = (TextView) view.findViewById(R.id.showReceivedData);

    Bundle args = getArguments();
    if (args != null) {

         showReceivedData.setText(args.getString("data"));

    } else {

        Toast.makeText(getActivity(), "didnt get the bundle from mainactivity", Toast.LENGTH_LONG).show();
    }


    return view;
}


}

应用程序的设计:

Fragment A

Fragment B

1 个答案:

答案 0 :(得分:2)

如果这两个片段使用相同的活动,

将值设置为片段:

 getActivity().getIntent().putExtra("key", "value");

从片段中获取价值:

getActivity().getIntent().getExtras().getString("key");