<small id='X4Ozx'></small><noframes id='X4Ozx'>

      <i id='X4Ozx'><tr id='X4Ozx'><dt id='X4Ozx'><q id='X4Ozx'><span id='X4Ozx'><b id='X4Ozx'><form id='X4Ozx'><ins id='X4Ozx'></ins><ul id='X4Ozx'></ul><sub id='X4Ozx'></sub></form><legend id='X4Ozx'></legend><bdo id='X4Ozx'><pre id='X4Ozx'><center id='X4Ozx'></center></pre></bdo></b><th id='X4Ozx'></th></span></q></dt></tr></i><div id='X4Ozx'><tfoot id='X4Ozx'></tfoot><dl id='X4Ozx'><fieldset id='X4Ozx'></fieldset></dl></div>

        <bdo id='X4Ozx'></bdo><ul id='X4Ozx'></ul>
      <tfoot id='X4Ozx'></tfoot>

    1. <legend id='X4Ozx'><style id='X4Ozx'><dir id='X4Ozx'><q id='X4Ozx'></q></dir></style></legend>

        在适配器的构造函数中传递片段

        时间:2023-10-03

        <tfoot id='8pPdP'></tfoot>
      1. <legend id='8pPdP'><style id='8pPdP'><dir id='8pPdP'><q id='8pPdP'></q></dir></style></legend>
          <bdo id='8pPdP'></bdo><ul id='8pPdP'></ul>

            <tbody id='8pPdP'></tbody>

          • <i id='8pPdP'><tr id='8pPdP'><dt id='8pPdP'><q id='8pPdP'><span id='8pPdP'><b id='8pPdP'><form id='8pPdP'><ins id='8pPdP'></ins><ul id='8pPdP'></ul><sub id='8pPdP'></sub></form><legend id='8pPdP'></legend><bdo id='8pPdP'><pre id='8pPdP'><center id='8pPdP'></center></pre></bdo></b><th id='8pPdP'></th></span></q></dt></tr></i><div id='8pPdP'><tfoot id='8pPdP'></tfoot><dl id='8pPdP'><fieldset id='8pPdP'></fieldset></dl></div>

            <small id='8pPdP'></small><noframes id='8pPdP'>

                1. 本文介绍了在适配器的构造函数中传递片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  因为在 ViewPager 上永远不会触发 onClick 事件(请参阅 这个问题),我将把onClickListener设置为适配器中的item容器.

                  Since the onClick event is never triggered on a ViewPager (see this question), I'm going to set the onClickListener to the item container in the adapter.

                  这个想法是,当点击一个项目时,从适配器调用一个方法,该方法来自该适配器设置为 ViewPager 的 Fragment.

                  The idea is, when an item is clicked, to call from the adapter a method from the Fragment in which this adapter is set to the ViewPager.

                  对于 Activity,我将传递上下文并使用此上下文调用方法(类似于 ((myActivity)context).itemContainerOnClick()).

                  For an Activity I would pass the context and call the method using this context (something like ((myActivity)context).itemContainerOnClick()).

                  但是我该如何处理 Fragment?将 Fragment 作为参数传递给适配器的构造函数时,我会遇到任何问题吗?

                  But how can I do with a Fragment? Could I face any issue passing the Fragment as an argument to my adapter's constructor?

                  谢谢

                  片段

                  public class MyFragment extends Fragment {
                  
                      ViewPager mViewPager;
                  
                      int mPosition = 0;
                  
                      @Override
                      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                              Bundle savedInstanceState) {
                  
                          view = (RelativeLayout) inflater.inflate(R.layout.my_fragment_container, container, false);
                  
                          ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewpager);
                  
                          mViewPager.setAdapter(new MyAdapter(getActivity(), myObjectArray);
                  
                          mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
                  
                              @Override
                              public void onPageSelected(int position) {
                                  mPosition = position;
                              }
                  
                              @Override
                              public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
                  
                              @Override
                              public void onPageScrollStateChanged(int state) {}
                          });
                  
                          mViewPager.setOnClickListener(new OnClickListener() {
                              @Override
                              public void onClick(View v) {
                                  // I want to start some activity, passing mPosition
                                  // but this is never called
                              }
                          }); 
                  
                  
                          return view;
                      }
                  
                      // Alternative to mViewPager.setOnClickListener, called from the adapter
                      public void onViewPagerClick()
                      {
                          // start some activity, passing mPosition
                      } 
                  }
                  

                  页面适配器

                  class MyAdapter extends PagerAdapter {
                  
                      private MyObject[] mItemArray;
                      private Context mContext;
                  
                      public SejoursWidgetAdapter(Context context, MyObject[] itemArray) {
                          mContext = context;
                          mItemArray = itemArray;
                      }
                  
                      @Override
                      public Object instantiateItem(View collection, int position) {
                  
                          LayoutInflater inflater = (LayoutInflater) collection.getContext()
                          .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                  
                          View row = inflater.inflate(R.layout.item, null);      
                  
                          // Add click listener here because setting it directly on the ViewPager does not work (onClick not triggered)       
                          row.setOnClickListener(new OnClickListener() {
                              @Override
                              public void onClick(View v) {
                                  // How can I call MyFragment's onViewPagerClick() ?
                              }
                          }); 
                  
                          ((ViewPager) collection).addView(row);        
                  
                  
                          return row;
                      }
                  
                      /* More stuff */
                  
                  }
                  

                  推荐答案

                  用 ItemClick 的变化检查我下面的代码.

                  Check my below code with Change of ItemClick.

                   class MyAdapter extends PagerAdapter {
                  
                     private MyObject[] mItemArray;
                     private Context mContext;
                  
                  public SejoursWidgetAdapter(Context context, MyObject[] itemArray) {
                      mContext = context;
                      mItemArray = itemArray;
                  }
                  
                  @Override
                  public Object instantiateItem(View collection, int position) {
                  
                      LayoutInflater inflater = (LayoutInflater) collection.getContext()
                      .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                  
                      View row = inflater.inflate(R.layout.item, null);      
                       row .setTag(position);
                      // Add click listener here because setting it directly on the ViewPager does not work (onClick not triggered)       
                      row.setOnClickListener(new OnClickListener() {
                          @Override
                          public void onClick(View v) {
                              // How can I call MyFragment's onViewPagerClick() ?
                  
                            int position=(Integer) v.getTag();
                       Log.v("Log_tag", "Here image is clicked"+ position);
                          //now you known which Page is click you can do what you want.
                          }
                      }); 
                  
                      ((ViewPager) collection).addView(row);        
                  
                  
                      return row;
                  }
                  
                  /* More stuff */
                  
                   }
                  

                  这篇关于在适配器的构造函数中传递片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:嵌套片段可能吗? 下一篇:Android ViewPager 方向改变

                  相关文章

                  <i id='BoXnA'><tr id='BoXnA'><dt id='BoXnA'><q id='BoXnA'><span id='BoXnA'><b id='BoXnA'><form id='BoXnA'><ins id='BoXnA'></ins><ul id='BoXnA'></ul><sub id='BoXnA'></sub></form><legend id='BoXnA'></legend><bdo id='BoXnA'><pre id='BoXnA'><center id='BoXnA'></center></pre></bdo></b><th id='BoXnA'></th></span></q></dt></tr></i><div id='BoXnA'><tfoot id='BoXnA'></tfoot><dl id='BoXnA'><fieldset id='BoXnA'></fieldset></dl></div>

                    <small id='BoXnA'></small><noframes id='BoXnA'>

                      <bdo id='BoXnA'></bdo><ul id='BoXnA'></ul>
                    <tfoot id='BoXnA'></tfoot>
                    <legend id='BoXnA'><style id='BoXnA'><dir id='BoXnA'><q id='BoXnA'></q></dir></style></legend>