Я создаю fragment, который загружает данные notifications с сервера и отображается в виде recyclerview. Данные загружаются с сервера правильно. Но recycler view не отображается в fragment.

В logcat ошибок не отображается.

Фрагмент (этот фрагмент загружается по умолчанию в панели навигации (домашний фрагмент).):

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link HomeFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private SessionManagement session;
    private Context mContext;

    private RequestQueue requestQueue;
    private Gson gson;

    private RecyclerView recyclerViewNotifications;
    private List<Notifications> listNotifications;
    private NotificationsRecyclerAdapter notificationsRecyclerAdapter;

    private String loggedUserId;

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private OnFragmentInteractionListener mListener;

    public HomeFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment HomeFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static HomeFragment newInstance(String param1, String param2) {
        HomeFragment fragment = new HomeFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_home, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        //----------------------------------------------------------------------------------------

        // Session class instance
        session = new SessionManagement(getActivity().getApplicationContext());
        // get user data from session
        HashMap<String, String> user = session.getUserDetails();
        String loggedUserId = user.get(SessionManagement.KEY_USERID);

        //----------------------------------------------------------------------------------------

        mContext = getActivity().getBaseContext();

        recyclerViewNotifications = (RecyclerView) view.findViewById(R.id.recyclerViewNotifications);

        listNotifications = new ArrayList<>();
        notificationsRecyclerAdapter = new NotificationsRecyclerAdapter(listNotifications);

        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
        recyclerViewNotifications.setLayoutManager(mLayoutManager);
        recyclerViewNotifications.addItemDecoration(new DividerItemDecoration(mContext, LinearLayoutManager.VERTICAL));
        recyclerViewNotifications.setItemAnimator(new DefaultItemAnimator());
        recyclerViewNotifications.setHasFixedSize(true);

        // Manually checking internet connection
        //if network not available
        if (!ConnectivityReceiver.isConnected()) {
            Log.d("x", "inside offline work.........View List");
            //Toast.makeText(mContext, "view list", Toast.LENGTH_SHORT).show();
            // launch new intent instead of loading fragment
        } else {
            //if network available
            Log.d("c", "inside online work.........Fetch notifications data");
            //Toast.makeText(getBaseContext(), "fetch enquiries", Toast.LENGTH_SHORT).show();

            requestQueue = null;
            // Get a RequestQueue
            requestQueue = Volley.newRequestQueue(mContext);
            fetchNotifications(loggedUserId);
        }

        //setting itemclick listener
        ItemClickSupport.addTo(recyclerViewNotifications).setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {
            @Override
            public void onItemClicked(RecyclerView recyclerView, int position, View v) {
                // do it
               /* Notifications noti=(Notifications) recyclerViewNotifications.getMyNotification(position);
                Intent viewIntent=new Intent(mContext, EnquiryViewActivity.class);
                if(enquiry!=null) {
                    Log.d("dash", "get enqid" + enquiry.getId());
                    viewIntent.putExtra("EnquiryId",enquiry.getId());
                    viewIntent.putExtra("UserId",enquiry.getUser_id());
                }
                startActivity(viewIntent);*/
            }
        });

        try {
            recyclerViewNotifications.setAdapter(notificationsRecyclerAdapter);
        }catch (NullPointerException e){
            Log.d("Error :","No notifications exists!");
            //Toast.makeText(mContext,"No business details exists!",Toast.LENGTH_LONG).show();
        }

        listNotifications.clear();

        try {
            fetchNotifications(loggedUserId);
            // listNotifications.addAll();
        }catch (NullPointerException e){
            Log.d("sdfs", "No Data available");
        }

        notificationsRecyclerAdapter.notifyDataSetChanged();


    }



        // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
//        if (context instanceof OnFragmentInteractionListener) {
//            mListener = (OnFragmentInteractionListener) context;
//        } else {
//            throw new RuntimeException(context.toString()
//                    + " must implement OnFragmentInteractionListener");
//        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }


    //-----------------------------------------------------------------------------------------

    private void fetchNotifications(final String LogId) {

        /*// Get a RequestQueue
        // RequestQueue requestQueue = MyVolleySingleton.getInstance(this.getApplicationContext()).getRequestQueue();
        requestQueue = Volley.newRequestQueue(this);*/

            //create gson instance which is used to parse json
            GsonBuilder gsonBuilder = new GsonBuilder();
            gsonBuilder.excludeFieldsWithoutExposeAnnotation();
            //gsonBuilder.setDateFormat("M/d/yy hh:mm a");
            gson = gsonBuilder.create();

            StringRequest sr = new StringRequest(Request.Method.POST, EndPoints.GET_ALL_NOTIFICATIOS_URL, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {

                    Log.d("d", "getting response.........\n" + response);
                    //Toast.makeText(getApplicationContext(), "Enquiry JSON data :" + response, Toast.LENGTH_LONG).show();

                    List<Notifications> notiList = Arrays.asList(gson.fromJson(response, Notifications[].class));
                   /* for (Notifications noti : notiList) {
                        listNotifications.add(noti);
                    }*/
                    listNotifications.addAll(notiList);
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                    Log.e("MainActivity", error.toString());
                    Toast.makeText(mContext, "Error: " + error.getMessage(), Toast.LENGTH_LONG).show();
                }
            }) {
                @Override
                protected Map<String, String> getParams() throws AuthFailureError {

                    Log.d("d", "inside getparams()...\n");
                    Map<String, String> params = new HashMap<String, String>();
                    params.put("userid", LogId);
                    Log.d("d", "userid to pass:" + LogId + "\n");
                    return params;
                }
            };

            // Add the request to RequestQueue.
            // MyVolleySingleton.getInstance(this).addToRequestQueue(sr);
            requestQueue.add(sr);
            Log.d("d", "Added to queue");
        }

    }

Адаптер:

public class NotificationsRecyclerAdapter extends RecyclerView.Adapter<NotificationsRecyclerAdapter.NotificationsViewHolder> {

    private List<Notifications> listNotifications;

    public NotificationsRecyclerAdapter(List<Notifications> listNotifications) {
        this.listNotifications = listNotifications;
    }

    public Notifications getMyNotification(int position) {
        Log.d("EnquiryAdapter : ", "position: " + position);
        Log.d("EnquiryAdapter : ", "item : " + listNotifications.get(position).getId());
        return listNotifications.get(position);
    }

    @Override
    public NotificationsRecyclerAdapter.NotificationsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // inflating recycler item view
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_notifications_recycler, parent, false);

        return new NotificationsViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(NotificationsViewHolder holder, int position) {

        Notifications noti=new Notifications();

        Log.d("NotificationsAdapter : ", "type : " + noti.getAction());
        Log.d("NotificationsAdapter : ", "status : " + noti.getCreateddate());

        holder.textViewNotiAction.setText(listNotifications.get(position).getAction());
        holder.textViewNotiDate.setText(listNotifications.get(position).getCreateddate());
    }


    @Override
    public int getItemCount() {
        Log.v(NotificationsRecyclerAdapter.class.getSimpleName(), "" + listNotifications.size());
        return listNotifications.size();
    }


    /**
     * ViewHolder class
     */
    public class NotificationsViewHolder extends RecyclerView.ViewHolder {

        public AppCompatTextView textViewNotiAction;
        public AppCompatTextView textViewNotiDate;

        public NotificationsViewHolder(View view) {
            super(view);

            textViewNotiAction = (AppCompatTextView) view.findViewById(R.id.textViewNotiAction);
            textViewNotiDate = (AppCompatTextView) view.findViewById(R.id.textViewNotiDate);

        }
    }


}

Макет фрагмента:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.anjana.decorightkitchen.fragment.HomeFragment">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recyclerViewNotifications"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

        </ScrollView>
    </LinearLayout>
</FrameLayout>

Макет элемента:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    card_view:cardCornerRadius="4dp">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="16dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <android.support.v7.widget.AppCompatTextView
                android:id="@+id/textViewNotiAction"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Notification"
                android:textStyle="bold"
                android:textColor="@color/colorAccentBlue" />

            <android.support.v7.widget.AppCompatTextView
                android:id="@+id/textViewNotiDate"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="end"
                android:layout_weight="1"
                android:text="date"
                android:textColor="@color/colorHintText" />

        </LinearLayout>

    </LinearLayout>

</android.support.v7.widget.CardView>
0
anj kri 1 Янв 2018 в 10:34

2 ответа

Лучший ответ

Вы должны отправить обновленный список уведомлений адаптеру после получения списка из службы, а затем установить адаптер.

@Override
public void onResponse(String response) {

Log.d("d", "getting response.........\n" + response);

List<Notifications> notiList = Arrays.asList(gson.fromJson(response, Notifications[].class));

listNotifications.addAll(notiList);
notificationsRecyclerAdapter = new NotificationsRecyclerAdapter(listNotifications);
recyclerViewNotifications.setAdapter(notificationsRecyclerAdapter);
}

ОБНОВЛЕНИЕ

  1. Не помещайте RecylerView в ScrollView (поскольку RecylerView имеет его прокрутку), пока вы не захотите иметь два прокрученных представления (например, 2 recylerview) на одной странице, а затем используйте ** NestedScrollView ** для вложенной прокрутки.

  2. Дайте некоторый запас снизу

      <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerViewNotifications"
            android:layout_width="match_parent"
            android:layout_height="match_parent" 
            android:layout_marginBottom="20dp" />
    

Надеюсь, что это поможет вам.

0
Vishal Chauhan 1 Янв 2018 в 10:25

Вы устанавливаете свой адаптер с пустым списком, чтобы он не отображался, вам нужно либо определить адаптер, либо установить его со значениями после получения ответа, например:

    @Override
        public void onResponse(String response) {

                        Log.d("d", "getting response.........\n" + response);
                        //Toast.makeText(getApplicationContext(), "Enquiry JSON data :" + response, Toast.LENGTH_LONG).show();

                        List<Notifications> notiList = Arrays.asList(gson.fromJson(response, Notifications[].class));
                       /* for (Notifications noti : notiList) {
                            listNotifications.add(noti);
                        }*/
                        listNotifications.addAll(notiList);
                        notificationsRecyclerAdapter = new 
                                    NotificationsRecyclerAdapter(listNotifications);
recyclerViewNotifications.setAdapter(notificationsRecyclerAdapter);    
                    }

Или

@Override
                public void onResponse(String response) {

                    Log.d("d", "getting response.........\n" + response);
                    //Toast.makeText(getApplicationContext(), "Enquiry JSON data :" + response, Toast.LENGTH_LONG).show();

                    List<Notifications> notiList = Arrays.asList(gson.fromJson(response, Notifications[].class));
                   /* for (Notifications noti : notiList) {
                        listNotifications.add(noti);
                    }*/
                    listNotifications.addAll(notiList);
                   notificationsRecyclerAdapter.notifyItemRangeInserted(0,listNotifications.size());
                }

Оба способа равны, но я не уверен, должен ли он иметь фиксированный размер в вашем коде

recyclerViewNotifications.setHasFixedSize(true);

Я думаю, его следует добавить после того, как ваш новый список будет установлен, надеюсь, это поможет вам

0
Esraa 1 Янв 2018 в 08:23