gpt4 book ai didi

android - 三个 Fragments,第二个 Fragments 的接口(interface)需要在 Main 和 Second Activity 上实现

转载 作者:行者123 更新时间:2023-11-30 03:33:20 25 4
gpt4 key购买 nike

在 Eclipse 中采用默认的“Master/Detail”流程模板,并添加第三个 Fragment(我们称它为 Edit,从预先存在的 Detail Fragment 启动),我现在希望在用户时打开 Edit Fragment单击详细信息 fragment 中的项目。

我已经在 Detail fragment 上实现了一个接口(interface),但是根据应用程序是在 Tablet 还是 Phone(是否双面板)上,Iterface 需要由 Detail Activity 或 Main Activity 实现为了发挥作用。我认为这是由于当设备未处于双 Pane 模式时模板将详细 fragment 的打开作为 Activity 实现的方式。

我是否错误地实现了这一点,或者是否有最佳实践可以让我将接口(interface)的实现统一到主要 Activity 中?

这里是主 fragment 和细节 fragment 的一些简化 fragment ,显示了对细节 fragment 接口(interface)的双重实现的要求。

WaveListWactivity.java 的代码(第一个 fragment )

public class WaveListActivity extends FragmentActivity implements
WaveListFragment.Callbacks,WaveDetailFragment.Callbacks {
private boolean mTwoPane;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wave_list);
if (findViewById(R.id.wave_detail_container) != null) {
mTwoPane = true;
((WaveListFragment) getSupportFragmentManager().findFragmentById(
R.id.wave_list)).setActivateOnItemClick(true);
}
}

//Interface from WaveListFragment
@Override
public void onWaveSelected(int id) {
if (mTwoPane) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.wave_detail_container, new WaveDetailFragment()).commit();

} else {
Intent detailIntent = new Intent(this, WaveDetailActivity.class);
startActivity(detailIntent);
}
}

//Interface from WaveDetailFragment
@Override
public void onItemSelected(int id) {
if (mTwoPane) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.wave_detail_container, new WaveEditFragment()).addToBackStack(null).commit();

} else {
Intent detailIntent = new Intent(this, WaveDetailActivity.class);
startActivity(detailIntent);
}
}
}

WaveDetailActivity.java 代码(第二个 fragment )

public class WaveDetailActivity extends FragmentActivity implements WaveDetailFragment.Callbacks {
private boolean mTwoPane;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wave_detail);
if (findViewById(R.id.wave_detail_container) != null) {
mTwoPane = true;
}

if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().add(R.id.wave_detail_container, new WaveDetailFragment()).commit();
}
}

//Callback from WaveDetailFragment
@Override
public void onItemSelected(int id) {
if (mTwoPane) {
getSupportFragmentManager().beginTransaction().replace(R.id.wave_detail_container, new WaveEditFragment()).addToBackStack(null).commit();

} else {
Intent detailIntent = new Intent(this, WaveEditActivity.class);
startActivity(detailIntent);
}
}
}

最佳答案

我认为您可能对 FragmentActivityFragments 和您需要在 Activity 上实现的回调接口(interface)之间的区别感到有些困惑>。从外观上看,所有代码 fragment 都是 Activity 类,不是 Fragments。我希望 Fragment 看起来像这样:

/** From http://developer.android.com/training/basics/fragments/communicating.html */

public class HeadlinesFragment extends ListFragment {
OnHeadlineSelectedListener mCallback;

// Container Activity must implement this interface
public interface OnHeadlineSelectedListener {
public void onArticleSelected(int position);
}

@Override
public void onAttach(Activity activity) {
super.onAttach(activity);

// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
mCallback = (OnHeadlineSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnHeadlineSelectedListener");
}
}

...
}

然后是您的Activity:

public static class MainActivity extends Activity
implements HeadlinesFragment.OnHeadlineSelectedListener{
...

public void onArticleSelected(int position) {
// The user selected the headline of an article from the HeadlinesFragment
// Do something here to display that article
}
}

整体的“流程”应该是:

  • fragment 充当 UI 代码块,带有所有有趣内容的回调。
  • Activity 实现的回调。

您可以执行的操作总数取决于 fragment ,而 fragment 又取决于您的屏幕大小(因此您可能有 2 个 fragment ,它们会馈送到单个 Activity 中)。决定是否显示一个或两个 fragment 的逻辑应该在Activity中完成。

关于android - 三个 Fragments,第二个 Fragments 的接口(interface)需要在 Main 和 Second Activity 上实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17038696/

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