java - fragment recyclerview 不同的 View 类型不起作用

标签 java android android-fragments android-recyclerview

Android fragment recyclerview 不同的 View 类型不工作

我已经使用 recyclerview 适配器实现了不同的 View 类型,但是这次列表是空的,不显示空 View 类型。
首先实现 fragment
其次在 fragment 中添加 recyclerview。

fragment 类

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

适配器类

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);
}
}

无布局

<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 布局

<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" />

家庭 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();
            }

最佳答案

空项目本身就是一个项目。要显示的项目计数不能为 0,否则永远不会调用其他方法(getItemViewType()onCreateViewHolder() ...)。

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

如果 locationlist 为空,您也可以返回 1 但您必须在其他方法中处理这种情况:

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

关于java - fragment recyclerview 不同的 View 类型不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49008150/

相关文章:

android - Activity 到下一个 Activity

java - 在 HttpClient 中保留 session

Android:- 如何在 6.0 操作系统以下的分词器 android TextView 中添加连字符 "-"

android - 持久 fragment 和 viewpager

java - 如何更新 ProgressDialog fragment

java - 实例方法是按对象还是按类加载到内存中?

java - JPA 实体中的枚举类型字段

java - 如何在JunitPerf中使用@BeforeClass和@AfterClass?

java - Android Studio Java 字符串比较

java - 从 java 文件中提取完整的属性类型