gpt4 book ai didi

android - 没有标签或 ID 的 getSupportFragmentManager getFragment

转载 作者:太空宇宙 更新时间:2023-11-03 11:25:39 24 4
gpt4 key购买 nike

我正在开发 Android 3.1 平板电脑应用程序。

我在一项 Activity 中使用 ViewPager 和 fragment 。我的问题是我不知道如何为 fragment 设置 ID 或标签。

Activity

public class FillEReportFragmentActivity extends FragmentActivity
{
...

/**
* View Pager to manage form parts.
*/
private ViewPager mViewPager;
/**
* Page Adapter to handle fragments.
*/
private FillEReportFragmentPagerAdapter mPagerAdapter;

@Override
protected void onCreate(Bundle savedInstance)
{
Log.v("FillEReportFragmentActivity", "onCreate");

super.onCreate(savedInstance);
setContentView(R.layout.fill_ereport_paged);

// Recoge los argumentos que le llegan de la actividad padre.
Bundle extras = getIntent().getExtras();
if (extras != null)
{
qapId = extras.getLong(BundleKeys.qapIdKey);
orderId = extras.getLong(BundleKeys.orderIdKey);
eReportId = extras.getLong(BundleKeys.eReportIdKey);
}

// Cargamos el E-Report o creamos uno nuevo. Además se recuperan los
// artículos de la orden actual.
LoadEReportAsyncTask task = new LoadEReportAsyncTask(this);
task.execute(Long.toString(this.qapId));

// Establezco los fragmentos (o partes del form) que se mostrarán en el
// View Pager.
mViewPager = (ViewPager)findViewById(R.id.pager);
mPagerAdapter = new FillEReportFragmentPagerAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mPagerAdapter);

// Muestro en la parte inferior unos círculos para indicar donde me
// encuentro dentro del E-Report.
CirclePageIndicator mIndicator = (CirclePageIndicator)findViewById(R.id.indicator);
mIndicator.setViewPager(mViewPager);

// Creo unos tabs para poder navegar más facilmente a través de las
// partes del E-Report.
TabPageIndicator titleIndicator = (TabPageIndicator)findViewById(R.id.tabIndicator);
titleIndicator.setViewPager(mViewPager);
}

...
}

FragmentPagerAdapter

public class FillEReportFragmentPagerAdapter extends FragmentPagerAdapter
{
private static final String[] TITLES = new String[] {
"Factory",
"Items"
};

public FillEReportFragmentPagerAdapter(FragmentManager fm)
{
super(fm);
// TODO Auto-generated constructor stub
}

@Override
public Fragment getItem(int index)
{
switch (index)
{
case 0:
return FactoryInfoFragment.newInstance();
case 1:
return ItemsFragment.newInstance();

default:
return null;
}
}

@Override
public int getCount()
{
return 2; // TODO: Este número hay que cambiarlo porque serán más.
}

public String getPageTitle(int position)
{
return TITLES[position];
}
}

res/values/ids.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<item type="id" name="frag_general_info_factory"/>
<item type="id" name="frag_general_info_items"/>
</resources>

工厂信息 fragment

public class FactoryInfoFragment extends Fragment
{
public static FactoryInfoFragment newInstance()
{
FactoryInfoFragment frag = new FactoryInfoFragment();

return frag;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
Log.v("FactoryInfoFragment", "onCreateView");

View v = inflater.inflate(R.layout.frag_general_info_factory, container, false);
view.setId(R.id.frag_general_info_factory);

return v;
}
}

frag_general_info_factory.xml

<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_margin="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:shrinkColumns="*"
android:stretchColumns="*">

<TableRow
android:weightSum="1"> <!-- 0 -->
<TextView
android:id="@+id/txtGenInfo"
android:layout_span="8"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/layout_title_general_information"
android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>

...

</TableLayout>

这里我要获取特定的 fragment

private class LoadEReportAsyncTask extends AsyncTask<String, Void, Boolean>
{
private Context mContext;
private ProgressDialog loadingDialog;
private EReport eReport;
private QAP qap;

public LoadEReportAsyncTask(Context context)
{
Log.v("LoadEReportAsyncTask", "constructor");
eReport = null;
qap = null;

this.mContext = context;
loadingDialog = new ProgressDialog(mContext);
loadingDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
loadingDialog.setMessage(getString(R.string.msg_loading_ereport));
loadingDialog.setCancelable(false);
loadingDialog.show();
}

@Override
protected Boolean doInBackground(String... params)
{
Log.v("LoadEReportAsyncTask", "doInBackground");
...
}

protected void onPostExecute(Boolean result)
{
Log.v("LoadEReportAsyncTask", "onPostExecute");

FillEReportFragmentActivity parentActivity = (FillEReportFragmentActivity) mContext;
if (result)
{
FactoryInfoFragment factFragment = (FactoryInfoFragment)getSupportFragmentManager().findFragmentById(R.id.frag_general_info_factory);
factFragment.fillEReport(eReport, qap);

Toast.makeText(getApplicationContext(), getString(R.string.msg_ereport_got_correct), Toast.LENGTH_SHORT).show();

ItemsFragment fragment = (ItemsFragment)getSupportFragmentManager().findFragmentById(R.id.frag_general_info_items);
fragment.setUpArticles(orderArticles);
}
else
{
Toast.makeText(getApplicationContext(), getString(R.string.msg_ereport_problem), Toast.LENGTH_LONG).show();
}

loadingDialog.dismiss();
}
}

此处:

(FactoryInfoFragment)getSupportFragmentManager().findFragmentById(R.id.frag_general_info_factory);

我使用 R.id.frag_general_info_factory 获取该 fragment 引用,但我得到一个 null

如何为 FactoryInfoFragment 设置标签或 ID?

最佳答案

我还没有尝试过,但我不知道 findFragmentById() 在这种情况下是否会正常工作,因为实际上有多个 fragment 与 ViewPager 容器相关联。

如果您查看 FragmentPagerAdapter 的源代码,您可以了解它是如何在内部使用标签的。然后,您可能会在自定义寻呼机适配器中覆盖 instantiateItem(),从 FragmentPagerAdapter 中复制代码并调整它以使用您也可以在代码中的其他地方使用的标签来检索自己的 fragment 。

例如,如果您的 PagerAdapter 实现可以特定于这个问题,您可以做一些像这样简单的事情:

private String getTag(int position) {
switch (position) {
case 0:
return FactoryInfoFragment.class.toString();
case 1:
return ItemsFragment.class.toString();
default:
return "unknown:" + position;
}

关于android - 没有标签或 ID 的 getSupportFragmentManager getFragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11134926/

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