片段recyclerview不同的视图类型不工作

时间:2018-02-27 11:57:43

标签: java android android-fragments android-recyclerview

Android片段recyclerview不同的视图类型无法正常工作

我使用recyclerview适配器实现了不同的视图类型,但是这次列表为空时不显示空视图类型 首先实现片段
第二步在片段中添加recyclelerview。

片段类

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

片段布局

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

家庭活动

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

2 个答案:

答案 0 :(得分:2)

空项目本身就是一个项目。要显示,项目计数不能为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;
    }
}

答案 1 :(得分:0)

在Fragment layout xml中包含no_task_layout.xml内容,将其Visibility设置为“gone”,然后检查Fragment Class中locationlist是否为空,

mongod --repair --dbpath /path/to/data/db

否则设置rvLocationList.setVisibility(View.GONE)并显示空视图。