当前位置: 首页 > 面试题库 >

如何在recyclerView android中停止项目重复

段干华皓
2023-03-14
问题内容

其实我的问题是,当我单击该项目时,它会转到DetailsFragment。但是,当我返回到ItemListFragment时,项目编号增加了,并且发生了重复。请帮助我从中恢复..预先感谢....

public class ItemsListFragment extends Fragment {
    public static final String SINGLE_ITEMS = "single_items";
    String[] mNames;
    String[] mPrices;
    int[] mImages;
    Context mContext;
    ArrayList<Data> mArrayList = new ArrayList<Data>();


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_items_list, container, false);
        RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.items_list_recycler);

        String allFields = getArguments().getString(CategoryFragment.ALL_FIELD);

        assert allFields != null;
        switch (allFields) {
            case "women_clothing":
                mNames = getResources().getStringArray(R.array.women_clothing_names);
                mPrices = getResources().getStringArray(R.array.women_clothing_prices);
                mImages = DataValues.women_clothing_images;

                break;
            case "women_shoes":
                mNames = getResources().getStringArray(R.array.women_shoes_names);
                mPrices = getResources().getStringArray(R.array.women_shoes_prices);
                mImages = DataValues.women_shoes_images;

                break;
            case "women_bags":
                mNames = getResources().getStringArray(R.array.women_bags_names);
                mPrices = getResources().getStringArray(R.array.women_bags_prices);
                mImages = DataValues.women_bags_images;

                break;
            case "women_watches":
                mNames = getResources().getStringArray(R.array.women_watches_names);
                mPrices = getResources().getStringArray(R.array.women_watches_prices);
                mImages = DataValues.women_watches_images;

                break;
            case "women_sunglasses":
                mNames = getResources().getStringArray(R.array.women_sunglasses_names);
                mPrices = getResources().getStringArray(R.array.women_sunglasses_prices);
                mImages = DataValues.women_sunglasses_images;

                break;
            case "women_accessories":
                mNames = getResources().getStringArray(R.array.women_accessories_names);
                mPrices = getResources().getStringArray(R.array.women_accessories_prices);
                mImages = DataValues.women_accessories_images;

                break;
        }

        int i = 0;
        for (String name : mNames) {
            Data data = new Data(name, mPrices[i], mImages[i]);
            mArrayList.add(data);
            i++;
        }

        ItemListGridAdapter adapter = new ItemListGridAdapter(getActivity(), mArrayList, allFields);
        recyclerView.setAdapter(adapter);

        RecyclerView.LayoutManager manager = new GridLayoutManager(getActivity(), 2);
        recyclerView.setLayoutManager(manager);
        recyclerView.setHasFixedSize(true);


        return view;
    }

}

ItemListGridAdapter

public class ItemListGridAdapter extends RecyclerView.Adapter<ItemListGridAdapter.ItemListViewHolder> {

    public static final String POSITION = "position";
    private Context mContext;
    private ArrayList<Data> mArrayList = new ArrayList<Data>();

    private String mAllFields;

    public ItemListGridAdapter(Context context, ArrayList<Data> arrayList, String allfields) {
        mContext = context;
        mArrayList = arrayList;
        mAllFields = allfields;
    }

    @Override
    public ItemListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_itemlist, parent, false);

        ItemListViewHolder viewHolder = new ItemListViewHolder(view);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ItemListViewHolder holder, final int position) {

        Data data = mArrayList.get(position);
        holder.mName.setText(data.getName());
        holder.mPrice.setText(data.getPrice());
        holder.mImage.setImageResource(data.getImageId());
        final Bundle bundle = new Bundle();

        holder.mRelativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (mAllFields) {
                    case "women_clothing":
                        DetailsFragment fragment = new DetailsFragment();
                        bundle.putInt(POSITION, (position + 10));
                        fragment.setArguments(bundle);
                        FragmentManager fragmentManager = ((Activity) mContext).getFragmentManager();
                        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                        fragmentTransaction.replace(R.id.home_main_frame, fragment);
                        fragmentTransaction.addToBackStack(null);
                        fragmentTransaction.commit();

                        break;
                    case "women_shoes":
                        DetailsFragment fragment1 = new DetailsFragment();
                        bundle.putInt("position", (position + 10));
                        fragment1.setArguments(bundle);
                        FragmentManager fragmentManager1 = ((Activity) mContext).getFragmentManager();
                        FragmentTransaction fragmentTransaction1 = fragmentManager1.beginTransaction();
                        fragmentTransaction1.replace(R.id.home_main_frame, fragment1);
                        fragmentTransaction1.addToBackStack(null);
                        fragmentTransaction1.commit();

                        break;
                    case "women_bags":
                        DetailsFragment fragment2 = new DetailsFragment();
                        bundle.putInt("position", (position + 10));
                        fragment2.setArguments(bundle);
                        FragmentManager fragmentManager2 = ((Activity) mContext).getFragmentManager();
                        FragmentTransaction fragmentTransaction2 = fragmentManager2.beginTransaction();
                        fragmentTransaction2.replace(R.id.home_main_frame, fragment2);
                        fragmentTransaction2.addToBackStack(null);
                        fragmentTransaction2.commit();

                        break;
                    case "women_watches":
                        DetailsFragment fragment3 = new DetailsFragment();
                        bundle.putInt("position", (position + 10));
                        fragment3.setArguments(bundle);
                        FragmentManager fragmentManager3 = ((Activity) mContext).getFragmentManager();
                        FragmentTransaction fragmentTransaction3 = fragmentManager3.beginTransaction();
                        fragmentTransaction3.replace(R.id.home_main_frame, fragment3);
                        fragmentTransaction3.addToBackStack(null);
                        fragmentTransaction3.commit();

                        break;
                    case "women_sunglasses":
                        DetailsFragment fragment4 = new DetailsFragment();
                        bundle.putInt("position", (position + 10));
                        fragment4.setArguments(bundle);
                        FragmentManager fragmentManager4 = ((Activity) mContext).getFragmentManager();
                        FragmentTransaction fragmentTransaction4 = fragmentManager4.beginTransaction();
                        fragmentTransaction4.replace(R.id.home_main_frame, fragment4);
                        fragmentTransaction4.addToBackStack(null);
                        fragmentTransaction4.commit();

                        break;
                    case "women_accessories":
                        DetailsFragment fragment5 = new DetailsFragment();
                        bundle.putInt("position", (position + 10));
                        fragment5.setArguments(bundle);
                        FragmentManager fragmentManager5 = ((Activity) mContext).getFragmentManager();
                        FragmentTransaction fragmentTransaction5 = fragmentManager5.beginTransaction();
                        fragmentTransaction5.replace(R.id.home_main_frame, fragment5);
                        fragmentTransaction5.addToBackStack(null);
                        fragmentTransaction5.commit();

                        break;

                }

            }
        });


    }

    @Override
    public int getItemCount() {
        return mArrayList.size();
    }

    public class ItemListViewHolder extends RecyclerView.ViewHolder {
        private TextView mName;
        private TextView mPrice;
        private ImageView mImage;
        private RelativeLayout mRelativeLayout;

        public ItemListViewHolder(View itemView) {
            super(itemView);

            mName = (TextView) itemView.findViewById(R.id.item_name_text);
            mPrice = (TextView) itemView.findViewById(R.id.item_price_text);
            mImage = (ImageView) itemView.findViewById(R.id.item_image);
            mRelativeLayout = (RelativeLayout) itemView.findViewById(R.id.item_click);


        }
    }
}

详细片段

public class DetailsFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_details, container, false);

        TextView mTitle = (TextView) view.findViewById(R.id.item_details_title);
        TextView mPrice = (TextView) view.findViewById(R.id.item_details_price);
        ImageView mImage = (ImageView) view.findViewById(R.id.item_details_image);
        TextView mDescription = (TextView) view.findViewById(R.id.item_details_description);

        int position = getArguments().getInt(ItemListGridAdapter.POSITION);

        Toast.makeText(getActivity(), ""+position, Toast.LENGTH_SHORT).show();



        return view;
    }

问题答案:

做这个

int i = 0;
    mArrayList.clear(); //Clear your array list before adding new data in it
    for (String name : mNames) {
        Data data = new Data(name, mPrices[i], mImages[i]);
        mArrayList.add(data);
        i++;
    }

这样,您的数据将不再重复。试试看,让我知道它是否对您有用。



 类似资料:
  • 问题内容: 我正在使用nodejs和mongoose —尝试在嵌套有递归函数和foreach的深层注释中找到特定的注释。有没有一种方法可以停止nodejs forEach?据我了解,每个forEach迭代都是一个函数,并且我不能只执行“ break”,而只能执行“ return”,但这不会停止foreach。 问题答案: 您无法打破。不过,我可以想到三种伪造方法。 1.The Ugly Way :

  • 问题内容: 我目前正在玩 Grand Central Dispatch ,发现了一个名为的课程。该文档似乎有些不完整,所以我不确定是否正确使用它。我创建了以下代码段,并期望有所不同。我预计该项目将在调用后被取消。但是由于某种原因,迭代仍在继续。有什么想法我做错了吗?该代码对我来说似乎很好。 问题答案: GCD不会执行抢先取消。因此,要停止已经开始的工作项,您必须自己测试取消。在斯威夫特的。在Obj

  • 问题内容: 我有一个每2000毫秒调用一次的javascript函数。我想停止此操作,以便我可以让用户在页面上执行其他操作,而无需再次调用它。这可能吗?这是每2000ms调用一次的函数: 问题答案: 没有内置的“暂停”功能,但是您可以停止它,然后使用相同的功能开始一个新的间隔。 首先,您需要捕获调用所返回的ID : 然后,当您要停止它时,请致电 在您的情况下,建议您自己定义,而不是在的内部进行定义

  • 当我开始在uitextfield中输入时,我希望发生一些动画,在我完成输入之前,它不应该反转回来。我正在使用此代码设置动画: 我想做的是:当我开始在第一个文本字段中键入时,我希望第二个文本字段从视图中隐藏,当我完成键入时,我希望它重新设置动画。 我的问题是:当我开始键入动画时,它会返回到原始位置。它不会等我完成打字。

  • 问题内容: 我有一个包含多个记录的表: 用户名(例如“ TOM”) Question_ID(例如“ q002”) 答案(例如“ D”) 我想创建一个触发器,以便没人可以两次提交相同问题的答案。它仅 是触发器 。 问题答案: 创建触发器

  • 问题内容: 我已经用GUI制作了一个Java程序,并且想要一个停止按钮功能,其中当用户单击停止按钮时,必须停止该程序。 在我的程序中,主线程启动其他10个线程,我希望每当单击“停止”按钮时,所有10个线程都必须在主线程之前停止。 其次,我还希望每当停止这10个线程中的任何一个线程时,它都必须先关闭它打开的所有资源,然后再连接数据库等。 我已经实现了.......回答的代码。 现在有一个问题。 我的