getItem 函数在 FragmentStatePagerAdapter 中调用两次?
Posted
技术标签:
【中文标题】getItem 函数在 FragmentStatePagerAdapter 中调用两次?【英文标题】:getItem function calling twice in FragmentStatePagerAdapter? 【发布时间】:2018-04-28 17:14:16 【问题描述】:我对 getItem() 函数有疑问,因为它在 FragmentStatePagerAdapter 类中被调用了两次。 实际上,主要原因是在具有 TextoSpeech 功能的应用程序中,因此 getItem() 函数两次文本也语音两次。这是我的代码,你能帮我吗....非常感谢。
这里是代码
这是 MainActivity 类:
public class MainActivity extends FragmentActivity
PagerFragment pagerFragment;
Cursor mCursor;
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.rhymes_activity_main);
DBUtils utils = new DBUtils(getApplicationContext());
new DBUtils(getApplicationContext());
try
DBUtils.createDatabase();
catch (IOException e)
Log.w(" Create Db "+e.toString(),"===");
DBUtils.openDatabase();
mCursor = utils.getResult("select * from Cflviewpagerdata order by title");
final ArrayList<PageData> myList = new ArrayList<PageData>();
while (mCursor.moveToNext())
myList.add(new PageData(mCursor.getInt(
mCursor.getColumnIndex("_id")),
mCursor.getString(mCursor.getColumnIndex("title")),
mCursor.getString(mCursor.getColumnIndex("view"))));
mCursor.close();
ListView lv = (ListView) findViewById(R.id.list_view);
ListViewAdapter lva = new ListViewAdapter(this, R.layout.list_item, myList);
lv.setAdapter(lva);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
//peace of code that create launch new fragment with swipe view inside
PagerFragment pagerFragment = new PagerFragment();
Bundle bundle = new Bundle();
bundle.putInt("CURRENT_POSITION", position);
bundle.putParcelableArrayList("DATA_LIST", myList);
pagerFragment.setArguments(bundle);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.replace(R.id.container, pagerFragment, "swipe_view_fragment").commit();
);
DBUtils.closeDataBase();
@Override
public void onBackPressed()
FragmentManager fm = getSupportFragmentManager();
Fragment f = fm.findFragmentByTag("swipe_view_fragment");
if(f!=null)
fm.beginTransaction().remove(f).commit();
else
super.onBackPressed();
这是 PagerFragment 类:
public class PagerFragment extends Fragment
private ArrayList<PageData> data;
private int currentPosition;
private String mTitle;
private FragmentActivity context;
@Override public void onAttach(Activity activity)
context = (FragmentActivity) activity;
super.onAttach(activity);
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
View v = inflater.inflate(R.layout.fragment_pager, container, false);
ViewPager mViewPager = (ViewPager) v.findViewById(R.id.pager_view);
currentPosition = getArguments().getInt("CURRENT_POSITION");
mTitle = getArguments().getString("T_TITLE");
data = getArguments().getParcelableArrayList("DATA_LIST");
FragmentItemPagerAdapter fragmentItemPagerAdapter = new FragmentItemPagerAdapter(getFragmentManager(), data);
mViewPager.setAdapter(fragmentItemPagerAdapter);
mViewPager.setCurrentItem(currentPosition);
return v;
这是 FragmentItemPagerAdapter 类:
public class FragmentItemPagerAdapter extends FragmentStatePagerAdapter
private static ArrayList<PageData> data;
public FragmentItemPagerAdapter(FragmentManager fm, ArrayList<PageData> data)
super(fm);
this.data = data;
@Override
public Fragment getItem(int position)
Fragment fragment = new PageFragment();
Bundle args = new Bundle();
args.putString(PageFragment.TITLE, data.get(position).getTitle());
args.putString(PageFragment.DESCRIPTION, data.get(position).getDes());
args.putInt("CURRENT_POSITION", position);
fragment.setArguments(args);
return fragment;
void deletePage(int position)
if (canDelete())
data.remove(position);
notifyDataSetChanged();
boolean canDelete()
return data.size() > 0;
@Override
public int getItemPosition(Object object)
// refresh all fragments when data set changed
return PagerAdapter.POSITION_NONE;
@Override
public int getCount()
return data.size();
public static class PageFragment extends Fragment implements OnInitListener
public static final String TITLE = "title";
public static final String DESCRIPTION = "view";
String om;
TextToSpeech tts;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
View rootView = inflater.inflate(R.layout.fragment_item, container, false);
((TextView) rootView.findViewById(R.id.item_label)).setText(getArguments().getString(TITLE));
View tv = rootView.findViewById(R.id.item_des);
((TextView) tv).setText(getArguments().getString(DESCRIPTION));
Bundle bundle = getArguments();
int currentPosition = bundle.getInt("CURRENT_POSITION");
tts = new TextToSpeech( getActivity(), PageFragment.this);
om = data.get(currentPosition).getDes();
tts.speak(om, TextToSpeech.QUEUE_FLUSH, null);
return rootView;
@Override
public void onDestroy()
if (tts != null)
tts.stop();
tts.shutdown();
super.onDestroy();
public void onInit(int status)
// TODO Auto-generated method stub
if (status == TextToSpeech.SUCCESS)
tts.speak(om, TextToSpeech.QUEUE_FLUSH, null);
这是 PageData 类: 这个类是对象类
public class PageData implements Parcelable
private String title;
private String view;
public PageData()
public PageData(Parcel in)
title = in.readString();
view = in.readString();
public int describeContents()
return 0;
public void writeToParcel(Parcel dest, int flags)
dest.writeString(title);
dest.writeString(view);
public PageData(int picture, String title, String description)
this.title = title;
this.view = description;
public String getTitle()
return title;
public String getDes()
return view;
这是 XML 代码 fragment_item.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
android:padding="8dp"
android:orientation="vertical">
<TextView
android:id="@+id/item_label"
android:layout_
android:layout_
android:layout_centerHorizontal="true"
android:layout_marginTop="10sp"
android:text="Image Name"
android:textColor="@android:color/black"
android:textSize="18sp"
android:layout_gravity="center_horizontal" />
<TextView
android:id="@+id/item_des"
android:layout_
android:layout_
android:layout_centerHorizontal="true"
android:layout_marginTop="10sp"
android:text="Image Name"
android:textColor="@android:color/black"
android:textSize="18sp"
android:layout_gravity="center_horizontal" />
</LinearLayout>
这是 fragment_pager.xml: 这是 viewpager 布局
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:background="#ffffff">
<android.support.v4.view.ViewPager
android:id="@+id/pager_view"
android:layout_
android:layout_ />
</FrameLayout>
这是 rhymes_activity_main.xml: 这是应用程序的列表视图。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_
android:layout_>
<ListView android:scrollbarAlwaysDrawVerticalTrack="true"
android:id="@+id/list_view"
android:scrollbars="vertical"
android:layout_
android:layout_/>
</FrameLayout>
这些图片是我的应用程序外观
这张图片是我的应用程序的 logcat 突出显示的是我的问题。
这是我的代码,你能帮我吗? 我是 Android 新手,请帮忙
【问题讨论】:
任何人都可以帮助我 谁能推荐我这个代码 【参考方案1】:FragmentStatePagerAdapter
总是至少预加载 1 页。
您可以尝试使用setUserVisibleHint
仅处理可见片段的逻辑:
@Override
public void setUserVisibleHint(boolean isVisibleToUser)
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser)
// Your code
【讨论】:
感谢妈妈的回复。你能告诉我在我的代码中放置这个代码的位置吗 嗨,妈妈,我使用了这段代码,但我仍然两次调用 getItem() 妈妈,我不明白你能简单解释一下吗以上是关于getItem 函数在 FragmentStatePagerAdapter 中调用两次?的主要内容,如果未能解决你的问题,请参考以下文章
pytorch 数据加载器和/或 __getitem__ 函数中的浅拷贝和深拷贝
从 pytorch 数据集返回索引:更改 __getitem__ 的函数导致元类冲突