我的回收站视图第二次未在片段中显示

Posted

技术标签:

【中文标题】我的回收站视图第二次未在片段中显示【英文标题】:My recycler view not shown for the second time in fragment 【发布时间】:2020-02-01 21:13:45 【问题描述】:

我的回收站视图仅在我第一次创建时显示,但当我按下另一个 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;
    






在我转到其他Fragment 并返回HomeFragment 后,我无法获得RecyclerView

已编辑

我点击这个链接Fragment, Volley and RecyclerView,这可能对你有帮助

【问题讨论】:

【参考方案1】:

从 onCreateView 内部调用 jsonParse() 方法而不是 onCreate

【讨论】:

第二次还是看不到回收站 没有错误,阵列工作完美,但回收站视图不显示 问题是什么@muhammad ammar ***.com/questions/47128561/… 我遵循此代码。也许我的适配器放错了。

以上是关于我的回收站视图第二次未在片段中显示的主要内容,如果未能解决你的问题,请参考以下文章

回收站视图未显示在片段中

导航后未附加回收站视图

回收站视图未在 Fragment (Android) 上显示项目

在片段内使用回收器视图时出错

片段在我的回收站视图中表现得很奇怪

不更新片段中的回收站视图