问题描述
我使用的材料设计标签如下:
以下是我的 MainActivity.class :
公共类 MainActivity 扩展 AppCompatActivity 实现 ViewPager.OnPageChangeListener {私有 TabLayout tabLayout;私有 ViewPager viewPager;@覆盖protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);tabLayout = (TabLayout) findViewById(R.id.tabs);viewPager = (ViewPager) findViewById(R.id.viewpager);ViewPagerAdapter 适配器 = new ViewPagerAdapter(getSupportFragmentManager());viewPager.setAdapter(适配器);viewPager.addOnPageChangeListener(this);tabLayout.setupWithViewPager(viewPager);}@覆盖public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}@覆盖public void onPageSelected(int position) {System.out.println("onPageSelected 调用");}@覆盖public void onPageScrollStateChanged(int state) {}}
我的 ViewPagerAdapter 如下:
public class ViewPagerAdapter extends FragmentPagerAdapter {私有静态整数计数 = 2;公共 ViewPagerAdapter(FragmentManager fm) {超级(调频);}@覆盖公共片段 getItem(int position) {开关(位置){案例0:返回新的 FragmentOne();情况1:返回新的 FragmentTwo();}返回空;}@覆盖公共 int getCount() {返回计数;}@覆盖公共 CharSequence getPageTitle(int position) {开关(位置){案例0:返回标签一";情况1 :返回标签二";}返回空;}}
这是我的片段:
这是我的第一个片段名称是 FragmentOne :
public class FragmentOne extends Fragment {私人 EditText editText;私人按钮 btnSendData;@覆盖公共视图 onCreateView(LayoutInflater inflater,ViewGroup 容器,Bundle savedInstanceState) {System.out.println("第一个片段");查看视图 = inflater.inflate(R.layout.fragment_one,container,false);editText = (EditText) view.findViewById(R.id.et_name);btnSendData = (Button) view.findViewById(R.id.btn_send);btnSendData.setOnClickListener(new View.OnClickListener() {@覆盖public void onClick(View v) {FragmentTwo 片段 = 新的 FragmentTwo();Bundle bundle = new Bundle();bundle.putString("用户名",editText.getText().toString());fragment.setArguments(bundle);getFragmentManager.beginTransaction.replace(R.id.frag_second,fragment).commit();}});返回视图;}}
这是另一个名为 FragmentTwo 的片段:
public class FragmentTwo extends Fragment {@Nullable@覆盖公共视图 onCreateView(LayoutInflater inflater,ViewGroup 容器,Bundle savedInstanceState) {System.out.println("第二个片段");查看视图 = inflater.inflate(R.layout.fragment_two,container,false);捆绑包 = getArguments();如果(捆绑!= null){String value = getArguments().getString("username");}返回视图;}@覆盖公共无效 onResume() {super.onResume();System.out.println("onResume 被调用");}}
在第二个片段中,我正在获取数据,但它在前一个视图中添加了另一个视图.看图:
所以我想在两种情况下将数据从 FragmentOne 传递到 FragmentTwo :
1. 当我点击按钮和
时我想传递数据2. 当我滑动到 FragmentTwo 时应该传递数据
此外,当我尝试滑动到 FragmentTwo 时,在 FragmentTwo 中没有调用任何内容?为什么会这样?此外,当我单击第二个选项卡时,没有任何内容被调用.请帮助我如何在单击按钮时将数据传递给 FragmentTwo?
以下是布局文件:这是fragment_one
<编辑文本android:layout_marginLeft="16dp"android:layout_marginRight="16dp"android:id="@+id/et_name"机器人:提示=用户名"android:layout_width="match_parent"android:layout_height="wrap_content"/><按钮机器人:填充=16dp"android:text="发送数据"android:layout_marginLeft="16dp"android:layout_marginRight="16dp"android:id="@+id/btn_send"android:layout_height="wrap_content"android:layout_width="match_parent"/>
和第二个片段,fragment_second.xml :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="垂直" android:layout_width="match_parent"android:layout_height="match_parent"android:id="@+id/second_frag"机器人:重力=中心"><文本视图android:textSize="24sp"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textAppearance="?android:attr/textAppearanceLarge"android:text="片段二"android:id="@+id/textView2"/>
每个片段都与父活动相关联.所以你不能直接从一个片段到另一个片段进行通信.您将需要使用界面浏览父活动.
检查此文档:
I am using material design tabs as follows :
Following is my MainActivity.class :
public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener {
private TabLayout tabLayout;
private ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabLayout = (TabLayout) findViewById(R.id.tabs);
viewPager = (ViewPager) findViewById(R.id.viewpager);
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(this);
tabLayout.setupWithViewPager(viewPager);
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
System.out.println("onPageSelected Called");
}
@Override
public void onPageScrollStateChanged(int state) {
}
}
My ViewPagerAdapter is as follows :
public class ViewPagerAdapter extends FragmentPagerAdapter {
private static int count = 2;
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new FragmentOne();
case 1:
return new FragmentTwo();
}
return null;
}
@Override
public int getCount() {
return count;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position)
{
case 0 :
return "Tab One";
case 1 :
return "Tab Two";
}
return null;
}
}
and here are my Fragments :
This is my first fragment name is FragmentOne :
public class FragmentOne extends Fragment {
private EditText editText;
private Button btnSendData;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
System.out.println("IN FRAGMENT ONE");
View view = inflater.inflate(R.layout.fragment_one,container,false);
editText = (EditText) view.findViewById(R.id.et_name);
btnSendData = (Button) view.findViewById(R.id.btn_send);
btnSendData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentTwo fragment = new FragmentTwo();
Bundle bundle = new Bundle();
bundle.putString("username",editText.getText().toString());
fragment.setArguments(bundle);
getFragmentManager.beginTransaction.replace(R.id.frag_second,fragment).commit();
}
});
return view;
}
}
and here is another fragment with name FragmentTwo :
public class FragmentTwo extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
System.out.println("IN FRAGMENT TWO");
View view = inflater.inflate(R.layout.fragment_two,container,false);
Bundle bundle = getArguments();
if(bundle!= null)
{
String value = getArguments().getString("username");
}
return view;
}
@Override
public void onResume() {
super.onResume();
System.out.println("onResume gets called");
}
}
In the second fragment i am getting data buts its adding another view with previous one.see the image :
so I want to pass data from FragmentOne to FragmentTwo in two scenario :
1. I want to pass the data when i click on button and
2. When I swipe to FragmentTwo data should be passed
Also when I try to swipe to FragmentTwo nothing gets called in FragmentTwo ? why is it so ?Also when I click to second tab nothing gets called .Please help me how should i pass data to FragmentTwo on button click ?
following are layout files:here is fragment_one
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<EditText
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:id="@+id/et_name"
android:hint="Username"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:padding="16dp"
android:text="Send Data"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:id="@+id/btn_send"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
and second fragment , fragment_second.xml :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/second_frag"
android:gravity="center">
<TextView
android:textSize="24sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Fragment Two"
android:id="@+id/textView2" />
Each fragment is associated with the parent activity. so you can't directly communicate from one fragment to another fragment. You will need to go through Parent Activity using interface.
Check this docs : https://developer.android.com/training/basics/fragments/communicating.html
On button click pass the value to methods in your custom interface and access those methods in second fragment.
For this you need to implement fragment life cycle - https://developer.android.com/reference/android/app/Fragment.html
UPDATE
Done with some modification in you code, just look at the foll. code -
Manifex.xml
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:theme="@style/Theme.AppCompat.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
MainActivity.java
package com.app.onkar.tabdemo;
import android.support.v4.app.Fragment;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
viewPager = (ViewPager) findViewById(R.id.viewpager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new FragmentOne(), "ONE");
adapter.addFragment(new FragmentTwo(), "TWO");
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(this);
tabLayout.setupWithViewPager(viewPager);
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
System.out.println("onPageSelected Called");
}
@Override
public void onPageScrollStateChanged(int state) {
}
public static class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
private static int count = 2;
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new FragmentOne();
case 1:
return new FragmentTwo();
}
return null;
}
@Override
public int getCount() {
return count;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Tab One";
case 1:
return "Tab Two";
}
return null;
}
}
}
FragmentOne.java
package com.app.onkar.tabdemo;
import android.content.Context;
import android.net.Uri;
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.Button;
import android.widget.EditText;
public class FragmentOne extends Fragment {
private EditText editText;
private Button btnSendData;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
System.out.println("IN FRAGMENT ONE");
View view = inflater.inflate(R.layout.fragment_one,container,false);
editText = (EditText) view.findViewById(R.id.et_name);
btnSendData = (Button) view.findViewById(R.id.btn_send);
btnSendData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentTwo fragment = new FragmentTwo();
Bundle bundle = new Bundle();
bundle.putString("username",editText.getText().toString());
fragment.setArguments(bundle);
getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.second_frag,fragment).commit();
}
});
return view;
}
}
FragmentTwo.java
package com.app.onkar.tabdemo;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class FragmentTwo extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
System.out.println("IN FRAGMENT TWO");
View view = inflater.inflate(R.layout.fragment_two,container,false);
TextView txt2 = (TextView) view.findViewById(R.id.textView2);
Bundle bundle = getArguments();
if(bundle!= null)
{
String value = getArguments().getString("username");
txt2.setText(value);
}
return view;
}
@Override
public void onResume() {
super.onResume();
System.out.println("onResume gets called");
}
}
No change in Layout files. Just try above code - it is working exactly as you want. Hope it will help!
SCREENS
这篇关于将数据从一个设计选项卡传递到另一个选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!