我的回收器视图仅是我第一次创建时显示,但是当我按下另一个fragment
并返回到HomeFragment
RecyclerView
时未显示。
我已经尝试过很多方法,包括getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, selectedFragment).commit();
我的HomeFragment,我正在使用底部的NavNar进行导航。
public class HomeFragment extends Fragment {
IMyAPI iMyAPI;
CompositeDisposable compositeDisposable = new CompositeDisposable();
View rootView;
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
private RequestQueue mQueue;
public static String TAG = "HomeFragment";
private ArrayList<Event> eventsList;
public HomeFragment() {
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_home, container, false);
if (rootView == null) {
rootView = inflater.inflate(R.layout.fragment_home, container, false);
}
Log.d(TAG, "Restart view");
mRecyclerView = rootView.findViewById(R.id.eventRecyclerView);
mAdapter = new EventAdapter(eventsList, getContext());
mLayoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.setHasFixedSize(true);
return rootView;
}
private void jsonParse() {
String url = Constants.BASE_URL+"/api/event";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("events");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject event = jsonArray.getJSONObject(i);
int Id = event.getInt("id");
String Name = event.getString("name");
String Location = event.getString("location");
String State = event.getString("state");
String OrganizerName = event.getString("organizerName");
String OrganizerRegistrationNumber = event.getString("organizerRegistrationNumber");
String Description = event.getString("description");
Date StartDate = Constants.dateFormat.parse(event.getString("startDate"));
Date EndDate = Constants.dateFormat.parse(event.getString("endDate"));
String Status = event.getString("status");
UUID UserId = UUID.fromString(event.getString("userId"));
int MountainId =event.getInt("mountainId");
Event eventClass = new Event(Id, Name,Location,State,OrganizerName ,OrganizerRegistrationNumber ,Description,StartDate,EndDate ,Status ,UserId,MountainId);
eventsList.add(eventClass);
// Toast.makeText(getActivity(), Name, Toast.LENGTH_SHORT).show();
Log.d(TAG, eventClass.toString());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
mQueue.add(request);
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
Log.d(TAG, "OnCreated");
super.onCreate(savedInstanceState);
mQueue = Volley.newRequestQueue(getActivity());
eventsList = new ArrayList<>();
jsonParse();
}
}
// HomeActivity which setting up the bottom navbar
public class HomeActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
toggle.syncState();
if(savedInstanceState == null) {
}
BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
bottomNav.setOnNavigationItemSelectedListener(navListener);
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new HomeFragment()).commit();
}
private BottomNavigationView.OnNavigationItemSelectedListener navListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
Fragment selectedFragment = null;
switch (menuItem.getItemId()) {
case R.id.nav_home:
selectedFragment = new HomeFragment();
break;
case R.id.nav_chat:
selectedFragment = new ChatFragment();
break;
case R.id.nav_event:
selectedFragment = new EventFragment();
break;
case R.id.nav_search:
selectedFragment = new SearchFragment();
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, selectedFragment).commit();
return true;
}
};
}
// EventAdapter to setting up recyclerview
public class EventAdapter extends RecyclerView.Adapter<EventAdapter.EventViewHolder> {
private ArrayList<Event> mEventsList;
Context mContext;
@NonNull
@Override
public EventViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View v = LayoutInflater.from((viewGroup.getContext())).inflate(R.layout.event_item, viewGroup, false);
EventAdapter.EventViewHolder evh = new EventViewHolder(v);
return evh;
}
@Override
public void onBindViewHolder(@NonNull EventViewHolder eventViewHolder, int i) {
Event currentItem = mEventsList.get(i);
eventViewHolder.mEventName.setText(currentItem.getName());
eventViewHolder.mEventLocation.setText(currentItem.getLocation());
eventViewHolder.mState.setText(currentItem.getState());
eventViewHolder.mEventDate.setText(currentItem.getStartDate().toString());
eventViewHolder.mEventOrganizer.setText(currentItem.getOrganizerName());
}
@Override
public int getItemCount() {
return mEventsList.size();
}
public static class EventViewHolder extends RecyclerView.ViewHolder{
private TextView mEventName;
private TextView mEventLocation;
private TextView mState;
private TextView mEventDate;
private TextView mEventOrganizeBy;
private TextView mEventOrganizer;
public EventViewHolder(@NonNull View itemView) {
super(itemView);
mEventName = itemView.findViewById(R.id.tv_card_event_name);
mEventLocation = itemView.findViewById(R.id.tv_card_event_location);
mState = itemView.findViewById(R.id.tv_card_event_state);
mEventDate = itemView.findViewById(R.id.tv_card_event_date);
mEventOrganizer = itemView.findViewById(R.id.tv_card_event_organizer);
}
}
public EventAdapter(ArrayList<Event> eventsList, Context mContext) {
mEventsList = eventsList;
mContext = mContext;
}
}
回到其他
RecyclerView
回到Fragment
之后,我无法获得HomeFragment
已编辑
我点击此链接Fragment, Volley and RecyclerView,可能对您有帮助
最佳答案
从onCreateView内部而不是onCreate调用jsonParse()方法