2016-07-07 9 views
0

このフラグメントは、製品が削除または追加されたときに更新する必要がある製品数を示すView Pagerで構成されています。recyclerviewの行アイテムデータを更新する

 public class SubCategoryFragment extends BaseFragment implements OnItemClickListener 
{ 

     private View rootView; 
     private MasterCategory subCategory; 
     private RecyclerView subCategoryRecyclerView; 
     private SubCategoryListAdapter subCategoryListAdapter; 
     private ArrayList<MasterCategory> superSubCategories; 
     private String iconImageURL; 
     private ArrayList<MerchantOrder> merchantorder; 
     /*private IRequestComplete iRequestComplete;*/ 
     private int categoryId; 

     @Nullable 
     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
     { 
      super.onCreateView(inflater, container, savedInstanceState); 
      return rootView = inflater.inflate(R.layout.fragment_category_list, container, false); 
     } 

     @Override 
     public void onActivityCreated(@Nullable Bundle savedInstanceState) 
     { 
      super.onActivityCreated(savedInstanceState); 
      initialiseUI(); 
     } 
    initialise fragment 
     protected void initialiseUI() 
     { 
      categoryId = getArguments().getInt("categoryId"); 
      iconImageURL = (String) getArguments().getSerializable("iconImageURL"); 
      subCategory = (MasterCategory) getArguments().getSerializable("data"); 
      subCategoryRecyclerView = (RecyclerView) rootView.findViewById(R.id.category_list_rc_view); 
      rootView.findViewById(R.id.dashboard_progressbar_newlyadded).setVisibility(View.GONE); 


      subCategoryRecyclerView.setHasFixedSize(true); 
      LinearLayoutManager mLayoutManager = new LinearLayoutManager(context); 
      subCategoryRecyclerView.setLayoutManager(mLayoutManager); 

      superSubCategories = subCategory.getCategories(); 
      rootView.findViewById(R.id.dashboard_progressbar_newlyadded).setVisibility(View.GONE); 

      if (superSubCategories != null && !superSubCategories.isEmpty()) 
      { 

       subCategoryListAdapter = new SubCategoryListAdapter(superSubCategories, iconImageURL); 
       subCategoryRecyclerView.setAdapter(subCategoryListAdapter); 
       subCategoryListAdapter.setmOnItemClickListener(this); 
       updateListView(); 
      } 
      else 
      { 
       rootView.findViewById(R.id.text_no_order_error).setVisibility(View.VISIBLE); 
       ((TextView) rootView.findViewById(R.id.text_no_order_error)).setText("No Category found!"); 
      } 
     } 

更新リストビュー

​​

によってitemClickが

 @Override 
     public void onItemClick(View view, int position) 
     { 
      /*MasterCategory superSubCategories = subCategoryListAdapter.getSuperSubCategory(position); 
      Bundle bundle = new Bundle(); 
      bundle.putSerializable("data", superSubCategories); 
      SuperSubCategoryProductsFragment superSubCategoryProductsFragment = new SuperSubCategoryProductsFragment(); 
      superSubCategoryProductsFragment.setArguments(bundle); 
      manageFragment(superSubCategoryProductsFragment, SuperSubCategoryProductsFragment.class.getName(), CategoryDetailsFragment.class.getName(), bundle);*/ 
      /*ArrayList<MasterCategory> superSubCategories = subCategoryListAdapter.getSuperSubCategory(position).getCategories(); 

      if (null != superSubCategories){ 
       Bundle bundle = new Bundle(); 
       bundle.putSerializable("data", superSubCategories); 

       SuperSubCategoryListFragment categoryDetailsFragment = new SuperSubCategoryListFragment(); 
       categoryDetailsFragment.setArguments(bundle); 
       manageFragment(categoryDetailsFragment, SuperSubCategoryListFragment.class.getName(), SubCategoryFragment.class.getName(), null); 
      }*/ 

      MasterCategory superSubCategories = subCategoryListAdapter.getSuperSubCategory(position); 
      superSubCategories.getSubCategoryCount(); 
      superSubCategories.getProductCount(); 

      subCategoryListAdapter.notifyDataSetChanged(); 

      if (superSubCategories.isHasChildCategory()) 
      { 
       Bundle bundle = new Bundle(); 
       bundle.putSerializable("data", superSubCategories); 
       Intent intent = new Intent(context, BaseFragmentActivity.class); 
       intent.putExtra("toolbarTitle", superSubCategories.getName()); 
       intent.putExtra("FragmentClassName", SuperSubCategoryFragment.class.getName()); 
       intent.putExtra("data", bundle); 
       startActivity(intent); 
      } 
      else 
      { 
       Intent intent = new Intent(context, BaseFragmentActivity.class); 
       Bundle bundle = new Bundle(); 
       bundle.putInt("categoryId", superSubCategories.getCategoryId()); 
       bundle.putString("categoryName", superSubCategories.getName()); 
       bundle.putBoolean("isSubCatProducts", !superSubCategories.isHasChildCategory()); 
       bundle.putInt("ProductCount", superSubCategories.getProductCount()); 
       intent.putExtra("toolbarTitle", superSubCategories.getName()); 
       intent.putExtra("FragmentClassName", SubCategoryProductsFragment.class.getName()); 
       intent.putExtra("data", bundle); 
       startActivity(intent); 
      } 

     } 

     @Override 
     public void onPause() 
     { 
      super.onPause(); 
     } 

     @Override 
     public void onResume() 
     { 
      super.onResume(); 

     } 

     @Override 
     public void onAttachedToRecyclerView(RecyclerView recyclerView) 
     { 
      super.onAttachedToRecyclerView(subCategoryRecyclerView); 
      subCategoryRecyclerView.getAdapter().notifyDataSetChanged(); 
     } 
    } 
製品の詳細を表示するフラグメントを開き

この私のアダプタは、フラグメント

public class SubCategoryListAdapter extends RecyclerView.Adapter<SubCategoryListAdapter.ViewHolder> implements View.OnClickListener { 

    private static final String TAG = SubCategoryListAdapter.class.getSimpleName(); 
    private ArrayList<MasterCategory> superSubCategories; 
    private ImageLoader imageloader; 
    private com.amoda.androidlib.intf.OnItemClickListener mOnItemClickListener; 
    private String iconImageURL; 

    @Override 
    public void onClick(View view) 
    { 
     if (mOnItemClickListener != null) 
      mOnItemClickListener.onItemClick(view, (Integer) view.getTag()); 

    } 

    public class ViewHolder extends RecyclerView.ViewHolder 
    { 
     public TextView name; 
     public TextView productCount; 
     public NetworkImageView image; 


     public ViewHolder(View itemLayoutView) 
     { 
      super(itemLayoutView); 
      productCount = (TextView) itemLayoutView.findViewById(R.id.product_count); 
      name = (TextView) itemLayoutView.findViewById(R.id.name); 
      image = (NetworkImageView) itemLayoutView.findViewById(R.id.image); 

     } 
    } 

    public SubCategoryListAdapter(ArrayList<MasterCategory> superSubCategories, String iconImageURL) 
    { 
     this.superSubCategories = superSubCategories; 
     imageloader = Global.getInstance().getImageLoader(); 
     this.iconImageURL = iconImageURL; 
    } 

    @Override 
    public SubCategoryListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) 
    { 
     View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.super_category_list_row, parent, false); 
     ViewHolder vh = new ViewHolder(v); 
     return vh; 
    } 

    @Override 
    public void onBindViewHolder(final ViewHolder holder, final int position) 
    { 
     holder.name.setText("" + superSubCategories.get(position).getName()); 
     holder.image.setDefaultImageResId(R.drawable.logo_amoda); 
     holder.image.setImageUrl(iconImageURL, imageloader); 

     if(!superSubCategories.get(position).isHasChildCategory()) 
     { 

      holder.productCount.setText("" + superSubCategories.get(position).getProductCount()); 
     } 
     else 
     { 
      holder.productCount.setText(""); 
      holder.productCount.setBackgroundResource(R.drawable.icn_right_arrow); 
     } 
     holder.itemView.setTag(position); 
     holder.itemView.setOnClickListener(this); 
    } 

    public void setmOnItemClickListener(com.amoda.androidlib.intf.OnItemClickListener mOnItemClickListener) 
    { 
     this.mOnItemClickListener = mOnItemClickListener; 
    } 

    @Override 
    public int getItemCount() 
    { 
     if (superSubCategories != null) 
      return superSubCategories.size(); 
     else 
      return 0; 
    } 

    public MasterCategory getSuperSubCategory(int position) 
    { 
     return superSubCategories.get(position); 
    } 
} 

に取り付けられている。これは、ちょうどあなたのviewPagerアダプタにこのメソッドを追加し、私の活動に

private void showSubCategoryTabs() 
    { 
     setToolbarTitle(category != null ? category.getName() : ""); 
     try 
     { 
      mPromotionalImage.setDefaultImageResId(R.drawable.nodeals_img); 
      mPromotionalImage.setImageUrl(category.getImageUrl(), imageLoader); 
     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 

     tabContent = new ArrayList<String>(); 

     for (MasterCategory subCategories : category.getCategories()) 
     { 
      /*Check if the sub-sub category has super-sub category or not.*/ 
      if (null != subCategories.getCategories()) 
       tabContent.add(subCategories.getName()); 
     } 

     mViewPager.setAdapter(mSectionsPagerAdapter); 
     final TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); 
     tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE); 

     mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() 
     { 
      @Override 
      public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) 
      { 

      } 


      @Override 
      public void onPageSelected(int position) 
      { 

       Fragment fragment = ((SectionsPagerAdapter) mViewPager.getAdapter()).getFragment(position); 
       if (fragment != null) 
       { 
        fragment.onResume(); 
       } 


      } 

      @Override 
      public void onPageScrollStateChanged(int state) 
      { 

      } 


     }); 


    } 

    public class SectionsPagerAdapter extends FragmentStatePagerAdapter 
    { 

     private SectionsPagerAdapter sectionspageradapter; 
     private FragmentManager fragmentManager=null; 
     private Bundle bundle=new Bundle(); 



     public SectionsPagerAdapter(FragmentManager fm) 
     { 
      super(fm); 
      fragmentManager=fm; 

     } 


     @Override 
     public Object instantiateItem(ViewGroup container,int position) 
     { 
       Object obj=super.instantiateItem(container,position); 
       if(obj instanceof Fragment) 
       { 
        Fragment f=(Fragment)obj; 
        String tag=f.getTag(); 
        f.onResume(); 
       } 
      return obj; 
     } 

     @Override 
     public Fragment getItem(int position) 
     { 

      MasterCategory subCategories = category.getCategories().get(position); 
      if (subCategories.isHasChildCategory()) 
      { 
       SubCategoryFragment subCategoryFragment = new SubCategoryFragment(); 
       Bundle bundle = new Bundle(); 
       bundle.putSerializable("iconImageURL", category.getIconImageUrl()); 
       bundle.putSerializable("data", category.getCategories().get(position)); 
       subCategoryFragment.setArguments(bundle); 
       return subCategoryFragment; 
      } 
      else 
      { 
       SubCategoryProductsFragment subCategoryProductsFragment = new SubCategoryProductsFragment(); 
       Bundle bundle = new Bundle(); 
       bundle.putInt("categoryId", subCategories.getCategoryId()); 
       bundle.putString("categoryName", subCategories.getName()); 
       bundle.putBoolean("isSubCatProducts", true); 
       subCategoryProductsFragment.setArguments(bundle); 
       return subCategoryProductsFragment; 
      } 


     } 


     @Override 
     public int getCount() 
     { 
      return tabContent.size(); 
     } 

     @Override 
     public CharSequence getPageTitle(int position) 
     { 
      Locale l = Locale.getDefault(); 
      return tabContent.get(position); 
     } 

     public Fragment getFragment(int position) 
     { 
      String tag = String.valueOf(mMerchantSubCategories.get(position)); 
      return fragmentManager.findFragmentByTag(tag); 
     } 
    } 

    @Override 
    public void onResume() 
    { 
     if (!EventBus.getDefault().isRegistered(this)) 
      EventBus.getDefault().register(this); 
     super.onResume(); 

    } 

    @Override 
    protected void onPause() 
    { 
     super.onPause(); 
    } 

    @Override 
    public void onStop() 
    { 
     super.onStop(); 
     //*Unregister event bus when the app goes in background*//* 
     if (EventBus.getDefault().isRegistered(this)) 
      EventBus.getDefault().unregister(this); 
    } 


    @Override 
    public void onDestroy() 
    { 
     super.onDestroy(); 
     if (EventBus.getDefault().isRegistered(this)) 
      EventBus.getDefault().unregister(this); 
    } 

    public void onError(VolleyError volleyError) 
    { 
     UIHelper.stopProgressDialog(mProgressDialog); 
     Functions.Application.VolleyErrorCheck(this, volleyError); 
    } 
+0

私はページャのコード –

+0

を表示し、setNotifyDataChangedはrecyclerviewで動作しません。 –

+0

あなたはgetAdapterPosition()で作業する必要があります –

答えて

0

私の見るページャであります問題は解決される。

 @Override 
     public int getItemPosition(Object object) { 
      return POSITION_NONE; 
     } 

これは、viewPagerのオーバーライドメソッドです。 1つのフラグメントを別のフラグメントにスワイプすると、自動的にページが更新されます。

+0

いいえ、カウントはまだ更新されていません –

+0

他のsuggesstionsは理解されます –

+0

それは仕事でなければなりません...あなたがこのコードを書く場所を教えてください。 ViewPager –

0

 public void delete(int position) { //removes the row    
      superSubCategories.remove(position); 
      notifyItemRemoved(position); 
     } 

あなたSubCategoryListAdepterにこのコードを入れ

があなたのViewHolderにonClickListener作る..あなたのために、多分その仕事...この方法を試してください: はあなたのテキストをクリックすると、この行がすると仮定削除される。

 @Override 
     public void onClick(View v) { 
      if(v.getId() == R.id.name){ 
      //calls the method above to delete 
      delete(getAdapterPosition()); 
      } 

このようにデータを追加することもできます。実行時に問題なく動作します。ページを更新する必要はありません。

+0

他の提案はすべて歓迎されています –

+0

onPageSelectedメソッドでフラグメントviewPagerをアタッチしたり、 –

関連する問題