当我从导航抽屉中启动一个新片段时,它意味着将我带到具有匹配配置文件的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中的项目会在每个页面上不断重复刷新?
可能发生的情况是,每次网络请求返回时,它都会尝试将每个新项目添加到该片段内已存在的列表中。
根据您实现导航逻辑的方式,一旦您离开该片段,它就不会被系统破坏,而是调用一系列生命周期回调:
onPause() -> onStop() -> onDestroyView()
当您返回到片段时,所有必需的生命周期回调将被调用,直到达到活动状态为止。
onCreateView() -> onActivityCreated() -> onStart() -> onResume()
有关片段生命周期工作的更多信息,请参见:https://developer.android.com/guide/components/fragments.html
由于您的片段不会被销毁和重新创建,因此buyersProfiles
返回时,引用可能包含先前的数据。由于网络调用不会覆盖原始列表,因此每次调用onResponse
回调时,它将将从网络获取的新数据附加到现有数据旁边。
这也可能会影响您实现某些类型的“按需刷新”逻辑或“刷新”按钮的用例,因为新的网络调用不会清除该列表。
您可以尝试避免重复的一件事是添加
buyersProfiles = new ArrayList<BuyerProfile>();
到onResponse()
回调的顶部。
希望能帮助到你。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句