gpt4 book ai didi

android - 删除页面并将其添加到 FragmentPagerAdapter

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:05:21 25 4
gpt4 key购买 nike

我正在使用带有 5 页的 fragment 寻呼机适配器。并且我将适配器设置为如下所示查看寻呼机

 public class xxx extends FragmentPagerAdapter
{
final int PAGE_COUNT_LOGGED_IN = 6;
final int PAGE_COUNT_LOGGED_OUT = 2;
TextView oTextView = null;
LayoutInflater inflater = null;
PagerTabStrip m_oPageTabStrip = null;
String m_strTab = null;
String[] m_strArray = null;
Context m_oContext = null;

/** Constructor of the class */
public xxxx (Context context, android.support.v4.app.FragmentManager oFragmentManager)
{
super (oFragmentManager);
m_oContext = context;
}

/** This method will be invoked when a page is requested to create */
@Override
public Fragment getItem(int arg0)
{
xxxxFragment myFragment = new xxxFragment ();
Bundle data = new Bundle();
data.putInt("current_page", arg0+1);
myFragment.setArguments(data);
return myFragment;
}

/** Returns the number of pages */
@Override
public int getCount()
{
if (Utility.m_bIsLoggedIn == true)
{
return PAGE_COUNT_LOGGED_IN;
}
else
{
return PAGE_COUNT_LOGGED_OUT;
}
}

@Override
public CharSequence getPageTitle(int position)
{
String strText = " ";

switch(position)
{
case 0:
strText = getBaseContext().getString(R.string.ccc);
break;
case 1:
strText = getBaseContext().getString(R.string.bbb);
break;
case 2:
strText = getBaseContext().getString(R.string.bbb);
break;
case 3:
strText = getBaseContext().getString(R.string.bbb);
break;
case 4:
strText = getBaseContext().getString(R.string.bbb);
break;
case 5:
strText = getBaseContext().getString(R.string.Sbbb);
break;
}

}

在 fragment 中,我为每个页面分别创建了一个线性布局,如下所示

bbb.m_oSharedPage = (LinearLayout) LayoutInflater.from(Utility.m_oService).inflate(R.layout.viewpage, null);
iView = Utility._YOURS_SHARED;
oCurrentPage = .m_oSharedPage;

我按如下方式设置适配器

m_oPager.setAdapter(m_oPagerAdapter);

我的问题是如何动态添加和删除第 5 页...比如在某些情况下我需要第 5 页,而在某些情况下我需要删除第 5 页。

最佳答案

我有一些可能有用的东西,我不会更改 Fragment,只是将其删除并重新添加。这只会从最后一个位置开始改变。换句话说,如果您有五个 Fragments 并且想要删除第三个​​,那么这将不起作用。我不确定这是最好的解决方案,但这是我所做的:

mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

然后,作为示例,我使用添加和删除 click 方法:

public void remove(View v) {
// Change the count to whatever you would like
mSectionsPagerAdapter.setCount(1);
// Triggers a redraw of the PageAdapter
mSectionsPagerAdapter.notifyDataSetChanged();
}

public void add(View v) {
// Change the count back to the initial count
mSectionsPagerAdapter.setCount(2);
// Triggers a redraw of the PageAdapter
mSectionsPagerAdapter.notifyDataSetChanged();
}

我向我的 SectionsPagerAdapter 添加了一个 setCount() 方法:

public void setCount(int count) {
this._count = count;
}

getCount() 更改为并添加了默认计数:

private int _count = 2;

@Override
public int getCount() {
return this._count;
}

这是我的完整SectionsPagerAdapter:

public class SectionsPagerAdapter extends FragmentPagerAdapter {
private int _count = 2;

public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
return super.instantiateItem(container, position);
}

@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new FragmentOne();
case 1:
return new FragmentTwo();
default:
return null;
}
}

public void setCount(int count) {
this._count = count;
}

@Override
public int getCount() {
return this._count;
}

@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return getString(R.string.fragment_title_one).toUpperCase(Locale.ENGLISH);
case 1:
return getString(R.string.fragment_title_two).toUpperCase(Locale.ENGLISH);
}
return null;
}
}

关于android - 删除页面并将其添加到 FragmentPagerAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15807952/

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