Android recyclerview在每次活动创建时添加重复元素

时间:2016-06-10 23:08:01

标签: android android-recyclerview android-cardview

当我从导航抽屉中启动一个新片段时,它意味着带我到一个带有匹配配置文件的cardView的主页。它第一次运行很棒,我想要显示的每个配置文件都在那里。然而,当我离开片段然后再次单击它时,元素被复制,并且每次我刷新页面时,项目再次被复制。我相信这个问题存在于我的RecyclerView适配器中,而不是在活动创建中清除,但我无法确定它。这是我的片段类

public class HomeFragment extends Fragment {
    private CustomListAdapter listAdapter;
    private static final String profileUrl = "http://10.0.2.2:3000/apip/buyers/profiles";
    private static final String TAG = selectBuyerProfile.class.getSimpleName();
    private ProgressDialog pDialog;
    private ListView listView;
    private List<BuyerProfile> buyersProfiles = new ArrayList<BuyerProfile>();
    private View root;
    //private RVAdapter recyclerAdapter;

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

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        RecyclerView rv = (RecyclerView) getActivity().findViewById(R.id.rv);
        //rv.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));
        rv.setHasFixedSize(true);
        LinearLayoutManager llm = new LinearLayoutManager(getActivity());
        rv.setLayoutManager(llm);
        rv.setItemAnimator(new DefaultItemAnimator());
        final RVAdapter recyclerAdapter = new RVAdapter(buyersProfiles);
        rv.setAdapter(recyclerAdapter);

        RequestQueue mRequestQueue;

        Cache cache = new DiskBasedCache(getActivity().getCacheDir(), 1024 * 1024);

        Network network = new BasicNetwork(new HurlStack());
        mRequestQueue = new RequestQueue(cache, network);
        mRequestQueue.start();


        JsonArrayRequest profileRequest = new JsonArrayRequest(profileUrl,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        // Parsing json
                        for(int i = 0; i < response.length(); i++) {
                            try {
                                JSONObject obj = response.getJSONObject(i);
                                BuyerProfile parsedProfile = new BuyerProfile();
                                parsedProfile.setBuyerProfTitle(obj.getString("title"));
                                parsedProfile.setDescription(obj.getString("description"));
                                parsedProfile.setLocations(obj.getString("locations"));
                                parsedProfile.setAssetTypes(obj.getString("asset_type"));
                                parsedProfile.setPropertyStatuses(obj.getString("property_status"));
                                //parsedProfile.setBuyerId("Select");
                                buyersProfiles.add(parsedProfile);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                        recyclerAdapter.notifyDataSetChanged();
                        // notifying list adapter about data changes
                        // so that it renders the list view with updated data
                        //hidePDialog();
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                //Toast.makeText(selectBuyerProfile.this,"Error",Toast.LENGTH_LONG).show();

            }
        });
        mRequestQueue.add(profileRequest);

    }
}

这是我的RVAdapter类

public class RVAdapter extends RecyclerView.Adapter<RVAdapter.PersonViewHolder> {

    private Activity activity;
    private LayoutInflater inflater;
    private List<BuyerProfile> profileItems;
    private static boolean itemFavorited;

    RVAdapter(List<BuyerProfile> profiles) {
        this.profileItems = profiles;
    }

    public static class PersonViewHolder extends RecyclerView.ViewHolder {
        TextView name;
        TextView description;
        TextView locations;
        TextView id;
        TextView investmentRangeMin;
        TextView investmentRangeMax;
        TextView assetTypes;
        TextView propertyStatuses;
        ImageView favoriteButton;
        CardView cardView;
        PersonViewHolder(View itemView) {
            super(itemView);
            name = (TextView) itemView.findViewById(R.id.titleText);
            description = (TextView) itemView.findViewById(R.id.descriptionText);
            investmentRangeMin = (TextView) itemView.findViewById(R.id.investmentRangeMin);
            investmentRangeMax = (TextView) itemView.findViewById(R.id.investmentRangeMax);
            locations = (TextView) itemView.findViewById(R.id.locations);
            id = (TextView) itemView.findViewById(R.id.profileNumber);
            assetTypes = (TextView) itemView.findViewById(R.id.assetTypes);
            propertyStatuses = (TextView) itemView.findViewById(R.id.propertyStatuses);
            favoriteButton = (ImageView) itemView.findViewById(R.id.favorite_select);
            cardView = (CardView) itemView.findViewById(R.id.cv);
            //User selects favorite on a matched profile
            itemFavorited = false;
            favoriteButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(!itemFavorited) {
                        favoriteButton.setImageResource(R.drawable.ic_favorite);
                        //cardView.setCardBackgroundColor(R.color.colorPrimary);
                        itemFavorited = true;
                    } else {
                        favoriteButton.setImageResource(R.drawable.ic_favorite_border);
                        itemFavorited = false;
                    }
                }
            });
        }
    }

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

    @Override
    public PersonViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false);
        PersonViewHolder pvh = new PersonViewHolder(v);
        return pvh;
    }

    @Override
    public void onBindViewHolder(PersonViewHolder personViewHolder, int i) {
        personViewHolder.name.setText(profileItems.get(i).getBuyerProfTitle());
        personViewHolder.description.setText(profileItems.get(i).getDescription());
        personViewHolder.locations.setText(profileItems.get(i).getLocations());
        personViewHolder.assetTypes.setText(profileItems.get(i).getAssetTypes());
        personViewHolder.propertyStatuses.setText(profileItems.get(i).getPropertyStatuses());
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }

    /*
    @Override
    public Object getItem(int location) {
        return profileItems.get(location);
    }
    */

    @Override
    public long getItemId(int position) {
        return position;
    }

}

最后这是我的片段XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              xmlns:card_view="http://schemas.android.com/apk/res-auto"
              android:padding="0dp"
    >
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="8dp"
                >
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Matches"
                    android:layout_alignParentTop="true"
                    android:layout_alignParentEnd="false"
                    android:id="@+id/matchesText"
                    android:textAlignment="center"
                    android:textSize="20dp"
                    android:textColor="@color/navigationBarColor"
                    android:layout_alignParentStart="false"
                    android:layout_alignParentBottom="false"
                    android:layout_alignParentLeft="false"
                    android:layout_alignParentRight="true"/>

                    <android.support.v7.widget.RecyclerView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:id="@+id/rv"
                        android:layout_below="@id/matchesText"
                        />
                        <!-- Thumbnail Image -->
                        <ImageView
                            android:id="@+id/imgBillionaire"
                            android:src="@drawable/ic_perm_identity"
                            android:layout_width="80dp"
                            android:layout_height="80dp"
                            android:layout_alignParentLeft="true"
                            android:layout_marginRight="8dp"
                            android:layout_alignParentEnd="false"
                            android:layout_alignParentStart="true"
                            android:nestedScrollingEnabled="false"
                            android:visibility="invisible"/>

                        <!-- Name of Asset -->
                        <TextView
                            android:id="@+id/titleText"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignTop="@+id/imgBillionaire"
                            android:layout_toRightOf="@+id/imgBillionaire"
                            android:textSize="@dimen/Title"
                            android:textStyle="bold" />

                        <!-- Description -->
                        <TextView
                            android:id="@+id/descriptionText"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:layout_below="@id/titleText"
                            android:layout_marginTop="1dip"
                            android:layout_toRightOf="@+id/imgBillionaire"
                            android:textSize="@dimen/Description" />

                        <!-- Source -->
                        <TextView
                            android:id="@+id/locations"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:layout_below="@id/descriptionText"
                            android:layout_marginTop="5dp"
                            android:layout_toRightOf="@+id/imgBillionaire"
                            android:textColor="@color/wealthsource"
                            android:textSize="@dimen/InvestmentRange" />

                        <!-- Source -->
                        <TextView
                            android:id="@+id/assetTypes"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:layout_below="@id/locations"
                            android:layout_marginTop="5dp"
                            android:layout_toRightOf="@+id/imgBillionaire"
                            android:textColor="@color/wealthsource"
                            android:textSize="@dimen/InvestmentRange" />

                        <!-- Source -->
                        <TextView
                            android:id="@+id/propertyStatuses"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:layout_below="@id/assetTypes"
                            android:layout_marginTop="5dp"
                            android:layout_toRightOf="@+id/imgBillionaire"
                            android:textColor="@color/wealthsource"
                            android:textSize="@dimen/InvestmentRange" />

                        <!-- Year -->
                        <TextView
                            android:id="@+id/profileNumber"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentBottom="false"
                            android:layout_alignParentRight="true"
                            android:textColor="@color/year"
                            android:textSize="@dimen/Date" />
            </RelativeLayout>


</LinearLayout>

为什么我的recyclerview中的项目会在每次刷新页面时保持重复?

1 个答案:

答案 0 :(得分:1)

可能发生的是每次网络请求返回时,它都会尝试将每个新项添加到片段内已存在的列表中。

根据您实现导航逻辑的方式,一旦您离开它,您的片段就不会被系统销毁,而是调用一系列生命周期回调:

onPause() -> onStop() -> onDestroyView()

当您返回到片段时,将调用所有必需的生命周期回调,直到它达到活动状态。

onCreateView() -> onActivityCreated() -> onStart() -> onResume()

有关片段生命周期工作的更多信息,请访问:https://developer.android.com/guide/components/fragments.html

由于您的片段未被销毁并重新创建,因此您的buyersProfiles引用可能会在您返回时保留以前的数据。由于网络呼叫不会覆盖原始列表,因此每次调用它onResponse回调时,它都会将从网络中提取的新数据附加到现有数据旁边。

这也可能会影响您实施某种拉动刷新逻辑或刷新按钮的用例,因为新的网络调用不会清理列表。

你可以尝试避免这种重复的一件事是添加

buyersProfiles = new ArrayList<BuyerProfile>();

onResponse()回调的顶部。

希望它有所帮助。