0

我对 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 类: 这个类是 Object 类

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_width="match_parent"
   android:layout_height="wrap_content"
   android:padding="8dp"
   android:orientation="vertical">

   <TextView
      android:id="@+id/item_label"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      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_width="wrap_content"
      android:layout_height="wrap_content"
      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_width="fill_parent"
 android:layout_height="fill_parent"
 android:background="#ffffff">
    <android.support.v4.view.ViewPager
     android:id="@+id/pager_view"
     android:layout_width="match_parent"
     android:layout_height="match_parent" />
</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_width="match_parent"
    android:layout_height="match_parent">

      <ListView android:scrollbarAlwaysDrawVerticalTrack="true"
       android:id="@+id/list_view"
       android:scrollbars="vertical"
       android:layout_width="match_parent"
       android:layout_height="match_parent"/>
   </FrameLayout>

这些图像是我的应用程序外观 申请一览 应用程序浏览器

此图像是我的应用程序的 logcat 突出显示是我的问题。

请检查突出显示的文本

这是我的代码,你能帮帮我吗? 我是 Android 的新手,所以请帮忙

4

1 回答 1

0

FragmentStatePagerAdapter预加载总是至少 1 页。您可以尝试setUserVisibleHint仅用于处理可见片段的逻辑:

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if (isVisibleToUser) {
       // Your code
    }
}
于 2018-04-28T20:17:18.153 回答