如何清除recyclerView列表?

时间:2016-11-07 12:10:27

标签: android android-layout android-recyclerview

(编辑)

怎么了。我已经搜索了很多关于如何设置清除recyclelerView的内容,但我找到了一堆解决方案。但是没有一个能解决我的问题。

片段类:

public class Fase2Fragment extends Fragment{

    private List<mAuxModel> mAuxModel= new mAuxModel<>();
    private RecyclerView mRecyclerView;
    public static boolean isOpenFase2 = true;

    public void onCreate(Bundle savedInstanceState){...}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View fase2frag = inflater.inflate(R.layout.fragment_fase2, container, false);
        setHasOptionsMenu(false);

        mRecyclerView = (RecyclerView) fase2frag.findViewById(R.id.recyclerView);

        fab_open = AnimationUtils.loadAnimation(getActivity(), R.anim.fab_open);
        fab_close = AnimationUtils.loadAnimation(getActivity(), R.anim.fab_close);
        rotate_forward = AnimationUtils.loadAnimation(getActivity(), R.anim.rotate_forward);
        rotate_backward = AnimationUtils.loadAnimation(getActivity(), R.anim.rotate_backward);

        mRecyclerView.setHasFixedSize(true);

        final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(linearLayoutManager);

        mRecyclerView.addOnItemTouchListener(...);

        fab.setOnClickListener(...);

        return fase2frag;
    }
    public void removeItems() {
        mAuxModel.clear();
        mRecyclerView.getAdapter().notifyDataSetChanged();
    }
}

适配器类:

公共类AdpRecyclerView扩展了RecyclerView.Adapter {

private dataBase DB;
private LayoutInflater mLayoutInflater;
private List<mAuxModel> mAuxModel;
private static final int ARRAY_A1 = 0;
private static final int ARRAY_A2 = 1;

public AdpRecyclerView (){}

public AdpRecyclerView (Context context, List<mAuxModel> mAuxModel) {
    DB = new dataBase(context);

    this.mAuxModel= mAuxModel;
    mLayoutInflater = (LayoutInflater) context.getSystemService(Context.
                                      LAYOUT_INFLATER_SERVICE);
}

@Override
public int getItemViewType(int position) {
    if (mAuxModel.get(position).getModel_tipoFase().
                                            equals("Att")) {
        return ARRAY_A1;
    } else {
        return ARRAY_A2;
    }
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());

    MyViewHolderA2 mvhA2;
    MyViewHolderA1 mvhA1;

    switch (viewType) {
        case ARRAY_A1:
            View viewA1 = inflater.inflate(R.layout.
                                             item_a1, parent, false);
            mvhAt = new MyViewHolderA1(viewA1);
            return mvhA1;

        case ARRAY_A2:
            View viewA2 = inflater.inflate(R.layout.
                                             item_a2, parent, false);
            mvhAg = new MyViewHolderA2(viewA2);
            return mvhA2;
    }

    return null;
}

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

    switch (holder.getItemViewType()) {
        case ARRAY_A1:
            MyViewHolderA1 myViewHolderA1 = (MyViewHolderA1) holder;
            myViewHolderA1.bindA1(position);
            break;

        case ARRAY_A2:
            MyViewHolderA2 myViewHolderA2 = (MyViewHolderA2) holder;
            myViewHolderA2.bindA2(position);
            break;
    }

}

@Override
public int getItemCount() {
    return mAuxModel.size();
}

public class MyViewHolder() {...}

记录错误:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.victorvicari.actionbarteste, PID: 2355
              java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.v7.widget.RecyclerView$Adapter android.support.v7.widget.RecyclerView.getAdapter()' on a null object reference

3 个答案:

答案 0 :(得分:3)

public void removeItems() {
    mAuxModel.clear();
    recyclerView.getAdapter().notifyDatasetChanged();
}

使用已存在的适配器,不要刷新适配器的新实例。

答案 1 :(得分:1)

List<Object> list = new ArrayList<>();

RecyclerViewAdapter adapter = new RecyclerViewAdapter();

recyclerView.setAdapter(adapter);

Now use the same list(same reference) & adapter.

list.clear();
adapter.notifyDataSetChanged();

答案 2 :(得分:-1)

public void clearData() {
        int size = this.filterList.size(); // filterlist(arraylist)
        if (size > 0) {
            for (int i = 0; i < size; i++) {
                this.filterList.remove(0); // filterlist(arraylist)
            }
            this.notifyItemRangeRemoved(0, size);
        }
}

Call method :  recyclerViewadapter.clearData();