gpt4 book ai didi

java - 使用 TabLayout 在 fragment 之间发送数据

转载 作者:行者123 更新时间:2023-12-01 17:53:17 25 4
gpt4 key购买 nike

在使用 TabLayout 时,我在将数据从一个 fragment 发送到另一个 fragment 时遇到了困难。我阅读了大量有关如何执行此操作的在线帖子,但自己实现这些帖子时没有成功。

我的应用程序中设置了四个 fragment 。我正在尝试将从第四个 fragment 中的 EditText 元素收集的数据发送到第二个 fragment 的 TextView 元素。

我没有收到任何错误,但文本没有显示在第二个 fragment 中。我不知道如何从这里继续。谢谢!

第四个 fragment (发送 fragment )

public class settings extends Fragment {
SendMessage SM;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(
R.layout.setl, container, false);
return rootView;


}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

Button btnPassData = (Button) view.findViewById(R.id.btnPassData);
final EditText inData = (EditText) view.findViewById(R.id.inMessage);
btnPassData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SM.sendData(inData.getText().toString().trim());
}
});

}

public interface SendMessage {
void sendData(String message);
}

@Override
public void onAttach(Context context) {
super.onAttach(context);

try {
SM = (SendMessage) getActivity();
} catch (ClassCastException e) {
throw new ClassCastException("Error in retrieving data. Please try again");
}
}
}

第二个 fragment (接收 fragment )

public class tt extends Fragment {

TextView txtData;

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

return rootView;



}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
txtData = (TextView)view.findViewById(R.id.txtData);


}

public void displayReceivedData(String message) {
txtData.setText("Data received: "+ message);

}
}

主要 Activity

public class MainActivity extends AppCompatActivity implements settings.SendMessage {

@Override
protected void onCreate(Bundle savedInstanceState) {

getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_test);
SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
ViewPager viewPager = findViewById(R.id.view_pager);
viewPager.setAdapter(sectionsPagerAdapter);
TabLayout tabs = findViewById(R.id.tabs);
tabs.setupWithViewPager(viewPager);
}

@Override
public void sendData(String message) {
String tag = "android:switcher:" + R.id.view_pager + ":" + 1;
tt f = (tt) getSupportFragmentManager().findFragmentByTag(tag);
f.displayReceivedData(message);
}
}

最佳答案

您的应用程序的其他部分可能出现问题。

下面是您的工作示例的代码。

类MainActivity

public class MainActivity extends AppCompatActivity implements Setting.SendMessage {

@Override
protected void onCreate(Bundle savedInstanceState) {

getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(),this );
ViewPager viewPager = findViewById(R.id.viewpager);
viewPager.setAdapter(sectionsPagerAdapter);
TabLayout tabs = findViewById(R.id.sliding_tabs);
tabs.setupWithViewPager(viewPager);
}

@Override
public void sendData(String message) {
String tag = "android:switcher:" + R.id.viewpager + ":" + 1;
tt f = (tt) getSupportFragmentManager().findFragmentByTag(tag);
f.displayReceivedData(message);
}
}

类(class)设置

 public class Setting extends Fragment {
SendMessage SM;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(
R.layout.setl, container, false);
return rootView;
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

Button btnPassData = (Button) view.findViewById(R.id.btnPassData);
final EditText inData = (EditText) view.findViewById(R.id.inMessage);
btnPassData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SM.sendData(inData.getText().toString().trim());
}
});

}
public interface SendMessage {
void sendData(String message);
}

@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
SM = (SendMessage) getActivity();
} catch (ClassCastException e) {
throw new ClassCastException("Error in retrieving data. Please try again");
}
}
}

tt类

public class tt extends Fragment {

TextView txtData;

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

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
txtData = (TextView)view.findViewById(R.id.txtData);
}

public void displayReceivedData(String message) {
txtData.setText("Data received: "+ message);

}
}

SectionsPagerAdapter 类

public class SectionsPagerAdapter  extends FragmentPagerAdapter {
final int PAGE_COUNT = 2;
private String tabTitles[] = new String[] { "Tab1", "Tab2"};
private Context context;

public SectionsPagerAdapter(FragmentManager fm, Context context) {
super(fm);
this.context = context;
}

@Override
public int getCount() {
return PAGE_COUNT;
}

@Override
public Fragment getItem(int position) {
if(position==0){
return new Setting();
}
else
return new tt();
}

@Override
public CharSequence getPageTitle(int position) {
return tabTitles[position];
}
}

关于java - 使用 TabLayout 在 fragment 之间发送数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60760894/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com