Fragment recyclerview different types of view not working

Re-viewing an Android fragment for different types of viewing does not work

I have different types of views using the recyclerview adapter, but the list is empty, this time the view is empty.
First implement the fragment
. Second, add recyclerview to the fragment.

Fragment Class

 LocationAdapter locationAdapter = new LocationAdapter(getActivity(), locationList);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        rvLocationList.setLayoutManager(layoutManager);
        rvLocationList.setAdapter(locationAdapter);

Adapter class

public class LocationAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context mContext;
private ArrayList<LocationModel> locationlist;
private DataBaseHelper dataBaseHelper;
private OnItemClickListener onItemClickListener;
public final int EMPTY = 0;
public final int NOT_EMPTY = 1;
private String TAG = "LocationAdapter";

public LocationAdapter(Context context, ArrayList<LocationModel> list) {
    this.mContext = context;
    this.locationlist = list;
    dataBaseHelper = new DataBaseHelper(context);
}

@Override
public int getItemViewType(int position) {
    Log.d(TAG, "position = " + position);
    if (locationlist.size() == 0) {
        return EMPTY;
    } else {
        return NOT_EMPTY;
    }
}

public void refreshData(ArrayList<LocationModel> tasks) {
    locationlist.clear();
    locationlist.addAll(tasks);
    notifyDataSetChanged();
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    RecyclerView.ViewHolder viewHolder = null;
    LayoutInflater inflater = LayoutInflater.from(mContext);
    switch (viewType) {
        case EMPTY:
            View viewItem = inflater.inflate(R.layout.no_task_layout, parent, false);
            viewHolder = new EmptyViewHolder(viewItem);
            break;
        case NOT_EMPTY:
            View viewLoading = inflater.inflate(R.layout.location_list, parent, false);
            viewHolder = new NotEmptyViewHolder(viewLoading);
            break;
    }
    return viewHolder;
}

@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {

    switch (getItemViewType(position)) {

        case EMPTY:
            EmptyViewHolder emptyViewHolder = (EmptyViewHolder) holder;
            emptyViewHolder.ivNoItem.getLayoutParams().height = R.dimen._30sdp;
            emptyViewHolder.ivNoItem.getLayoutParams().width = R.dimen._30sdp;

            emptyViewHolder.tvNoitem.setText("No Location.");
            emptyViewHolder.tvAddItem.setText("Add Location");
            break;
        case NOT_EMPTY:
            break;
    }


}

@Override
public int getItemCount() {

    return locationlist == null ? 0 : locationlist.size();
}

public class NotEmptyViewHolder extends RecyclerView.ViewHolder {

    private ImageView ivFirstCharecter, ivMore;
    private TextView tvName, tvAddress;

    NotEmptyViewHolder(View itemView) {
        super(itemView);
        ivFirstCharecter = itemView.findViewById(R.id.ivFirstCharecter);
        ivMore = itemView.findViewById(R.id.ivMore);
        tvName = itemView.findViewById(R.id.tvName);
        tvAddress = itemView.findViewById(R.id.tvAddress);

        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.OnItemClick(locationlist.get(getAdapterPosition()).getLocationName());
            }
        });
    }
}

public class EmptyViewHolder extends RecyclerView.ViewHolder {
    private ImageView ivNoItem;
    private TextView tvNoitem, tvAddItem;

    EmptyViewHolder(View itemView) {
        super(itemView);
        ivNoItem = itemView.findViewById(R.id.ivNoItem);
        tvNoitem = itemView.findViewById(R.id.tvNoitem);
        tvAddItem = itemView.findViewById(R.id.tvAddItem);
    }
}

public void setOnItemClickListener(OnItemClickListener itemClickListener) {
    onItemClickListener = itemClickListener;
}

public interface OnItemClickListener {
    void OnItemClick(String locationName);
}
}

No layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/llNoTask"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:gravity="center"
android:orientation="vertical">


<ImageView
    android:id="@+id/ivNoItem"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:background="@drawable/ic_no_task" />


<TextView
    android:id="@+id/tvNoitem"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:fontFamily="@font/semibold"
    android:textColor="@color/black"
    android:textSize="@dimen/_30sdp" />


<TextView
    android:id="@+id/tvAddItem"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:fontFamily="@font/regular"
    android:textColor="@color/text_gray"
    android:textSize="@dimen/_18sdp" />

Fragment Layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.v7.widget.RecyclerView
    android:id="@+id/rvLocationList"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="@dimen/_60sdp" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fbtnAddLocation"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_margin="@dimen/_10sdp"
    android:gravity="center_vertical"
    app:backgroundTint="@color/Red"
    app:fabSize="normal"
    app:srcCompat="@drawable/ic_add" />

Main activity

fragment = new LocationFragment();
            try {
                FragmentManager fragmentManager = getSupportFragmentManager();
                fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
            } catch (IllegalStateException ieEx) {
                ieEx.printStackTrace();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
+4
source share
2 answers

An empty element is itself an element. To display the counter items should not be 0, otherwise other methods ( getItemViewType(), onCreateViewHolder(), ...) are never called.

@Override
public int getItemCount() {
    return locationlist == null ? 0 : Math.max(1, locationlist.size());
}

1, locationlist NULL, :

@Override
public int getItemViewType(int position) {
    Log.d(TAG, "position = " + position);
    if (locationlist == null || locationlist.size() == 0) {
        return EMPTY;
    } else {
        return NOT_EMPTY;
    }
}
+2

no_task_layout.xml xml, "", , ,

if(locationList.size > 0){ ///Set adapter to Recycler view

rvLocationList.setVisibility(View.GONE) .

0

Source: https://habr.com/ru/post/1694174/


All Articles