我将Recycler View放入片段活动中,其中包含许多Card View项目 但是,当我单击“刷新”按钮时,这些项目是第一次创建的
当下次加载Fragment时,不会再次获取这些项目,而这仅是第一次进行。
public class posts_fragment extends Fragment {
private List<PostData> PostDataList = new ArrayList<>();
PostDataAdapter adapter=new PostDataAdapter(PostDataList);
RecyclerView recyclerView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.posts_menu,container,false);
recyclerView =view.findViewById(R.id.PostRecycleView);
filldate();
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return view;
}
public void filldate() {
//Start Get And Set Json Into Card View
final String KeyUrl="http://www.rasamdev.ir/salehin/GetPost.php";
final String KeyUsername="Key";
StringRequest stringRequest = new StringRequest(Request.Method.POST, KeyUrl, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(getContext(),response, Toast.LENGTH_SHORT).show();
//Start Pars
if (response != null) {
try {
JSONObject jObject = new JSONObject(response);
JSONArray array = jObject.getJSONArray("Posts");
for (int i = 0; i < array.length(); i++) {
JSONObject object = array.getJSONObject(i);
String puid = object.getString("puid");
String Ptitr = object.getString("Ptitr");
String ptext = object.getString("ptext");
String pdate = object.getString("pdate");
String plike = object.getString("plike");
String pview = object.getString("pview");
String pimg = object.getString("pimg");
PostDataList.add(new PostData(pdate,ptext,puid,Ptitr,pimg,pview,plike));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
//End Pars
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getActivity(), error + "", Toast.LENGTH_SHORT).show();
}
}){
@Override
protected Map<String, String> getParams() {
HashMap<String, String> hashMap = new HashMap<>();
hashMap.put(KeyUsername,"13581358r@R");
return hashMap;
}
};
RequestQueue requestQueue= Volley.newRequestQueue(this.getActivity());
requestQueue.add(stringRequest);
//End
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
@Override
public void onResume() {
adapter.notifyDataSetChanged();
super.onResume();
}
适配器类:
public class PostDataAdapter extends RecyclerView.Adapter<PostDataAdapter.MyViewHolder> {
private List<PostData> PostDataList ;
public static class MyViewHolder extends RecyclerView.ViewHolder {
public TextView vPostContent, vPostDate, vPostAuthor, vPostTitr,VPostLikes,VPostViews;
public ImageView vPostPhoto;
public MyViewHolder(View v) {
super(v);
vPostContent = v.findViewById(R.id.PostContentTv);
vPostDate = v.findViewById(R.id.PostDateTv);
vPostAuthor = v.findViewById(R.id.PostAuthorTv);
vPostTitr = v.findViewById(R.id.PostTitrTv);
vPostPhoto = v.findViewById(R.id.PostPhoto);
VPostLikes=v.findViewById(R.id.PostLikeTv);
VPostViews=v.findViewById(R.id.PostViewTv);
}
}
public PostDataAdapter(List<PostData> postDataList) {
PostDataList = postDataList;
}
@Override
public PostDataAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_posts, parent, false);
MyViewHolder vh = new MyViewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.vPostDate.setText(PostDataList.get(position).getPostDate());
holder.vPostTitr.setText(PostDataList.get(position).getPostTitr());
holder.vPostContent.setText(PostDataList.get(position).getPostContent());
holder.vPostAuthor.setText(PostDataList.get(position).getPostAuthor());
holder.VPostViews.setText(PostDataList.get(position).getPostViews());
holder.VPostLikes.setText(PostDataList.get(position).getPostLikes());
new DownloadImageTask(holder.vPostPhoto).execute(PostDataList.get(position).getImgpost());
}
@Override
public int getItemCount() {
return PostDataList.size();
}
}
数据类:
public class PostData {
private String PostDate, PostContent, PostAuthor, PostTitr, imgpost, PostViews, PostLikes;
public PostData(String postDate, String postContent, String postAuthor, String postTitr, String imgpost, String postViews, String postLikes) {
PostDate = postDate;
PostContent = postContent;
PostAuthor = postAuthor;
PostTitr = postTitr;
this.imgpost = imgpost;
PostViews = postViews;
PostLikes = postLikes;
}
public String getPostDate() {
return PostDate;
}
public void setPostDate(String postDate) {
PostDate = postDate;
}
public String getPostContent() {
return PostContent;
}
public void setPostContent(String postContent) {
PostContent = postContent;
}
public String getPostAuthor() {
return PostAuthor;
}
public void setPostAuthor(String postAuthor) {
PostAuthor = postAuthor;
}
public String getPostTitr() {
return PostTitr;
}
public void setPostTitr(String postTitr) {
PostTitr = postTitr;
}
public String getImgpost() {
return imgpost;
}
public void setImgpost(String imgpost) {
this.imgpost = imgpost;
}
public String getPostViews() {
return PostViews;
}
public void setPostViews(String postViews) {
PostViews = postViews;
}
public String getPostLikes() {
return PostLikes;
}
public void setPostLikes(String postLikes) {
PostLikes = postLikes;
}
}