更新recyclerview的行项目数据

时间:2016-07-07 10:47:21

标签: android

  

该片段由View Pager组成,它显示了产品计数   需要在删除或添加产品时更新。

     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!");
            }
        }
  

更新列表视图

        private void updateListView()
        {
            if (subCategoryListAdapter == null)
            {
                subCategoryListAdapter = new SubCategoryListAdapter(superSubCategories,iconImageURL);
                subCategoryRecyclerView.setAdapter(subCategoryListAdapter);

            }
            else
            {
                subCategoryListAdapter.notifyDataSetChanged();
            }
            subCategoryListAdapter.notifyDataSetChanged();
        }
  

项目点击会打开一个显示产品详细信息的片段

        @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);
    }
}
  

这是我的活动中的View寻呼机

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);
    }

2 个答案:

答案 0 :(得分:0)

只需将此方法添加到viewPager适配器,问题就解决了。

shell=True

这是viewPager的覆盖方法。 当您将一个片段滑动到另一个片段时,它将自动刷新页面。

答案 1 :(得分:0)

尝试这种方法..也许它适合你...

将此代码放入SubCategoryListAdepter

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

将onClickListener设为ViewHolder: 假设您单击文本,该行将被删除。

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

现在您也可以像这样添加数据..这在运行时工作正常..无需刷新页面。