RecyclerView EmptyView具有RecyclerItem

时间:2018-06-21 01:02:21

标签: android android-recyclerview recycler-adapter android-viewholder

我正在尝试为我的空视图设置一个回收器项,以便空状态可以上下移动一点。

我已设置创建2个适配器。一个绑定项目列表,另一个绑定简单文本,表示该列表为空。我的问题是在updateUI()中,EmptyAdapter()返回null,因为我不确定如何使用构造函数。没有需要绑定的数据项,只需将视图充气即可。

专门查看方法和类:

updateUI(),
EmptyAdapter(),
CrimeAdapter(List<Crime> crimes)

我的问题是EmptyAdapter在updateUI()中返回null

public class CrimeListFragment extends Fragment {
private RecyclerView mRecyclerView;
private CrimeAdapter mCrimeAdapter;
private EmptyAdapter mEmptyAdapter;
private ImageView mSolvedImage;
private boolean subtitleVisible;
private static final int REQUEST_CRIME = 1;
private String SUBTITLE = "subtitle";

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle
        savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_crime_list, container, false);
    mRecyclerView = v.findViewById(R.id.crime_recycler_view);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    if (savedInstanceState != null) {
        subtitleVisible = savedInstanceState.getBoolean(SUBTITLE);
    }

    updateUI();
    setHasOptionsMenu(true);
    return v;
}

@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putSerializable(SUBTITLE, subtitleVisible);
}


private void updateUI() {
    //Singleton
    CrimeLab crimeLab = CrimeLab.get(getActivity());
    //List of crimes from singleton
    List<Crime> crimes = crimeLab.getCrimes();


    if (crimes.size() != 0) {
        //If adapter null, populate list
        if (mCrimeAdapter == null) {
            mCrimeAdapter = new CrimeAdapter(crimes);
            //hook-up adapter to recycler view
            mRecyclerView.setAdapter(mCrimeAdapter);
        } else {

            //else we are updating list b/c of some change in the data
            mCrimeAdapter.notifyDataSetChanged();
        }
    } else {
        if (mEmptyAdapter == null) {
            mEmptyAdapter = new EmptyAdapter();
            //hook-up empty adapter to recycler view
            mRecyclerView.setAdapter(mEmptyAdapter);
        } else {
            mEmptyAdapter.notifyDataSetChanged();
        }
    }

    //Update subtitle upon completing a crime and return to CrimeListActivity
    updateSubtitle();
}


//VIEW-HOLDER & ADAPTER FOR RECYCLER-VIEW

private class EmptyHolder extends RecyclerView.ViewHolder {

    private TextView emptyView;

    public EmptyHolder(LayoutInflater inflater, ViewGroup parent) {
        super(inflater.inflate(R.layout.empty_view1, parent, false));
        emptyView = itemView.findViewById(R.id.emptyTextView);
    }

    public void bind() {
        emptyView.setText("NO CRIMES HERE");
        emptyView.setVisibility(VISIBLE);
    }
}

private class EmptyAdapter extends RecyclerView.Adapter<EmptyHolder> {

    public EmptyAdapter() {
    }


    @Override
    public int getItemCount() {
        return 0;
    }

    @NonNull
    @Override
    public EmptyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(getActivity());

        return new EmptyHolder(inflater, parent);
    }

    @Override
    public void onBindViewHolder(@NonNull EmptyHolder holder, int position) {
        holder.bind();
    }

}

private class CrimeHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    private TextView mTitle;
    private TextView mDate;
    private Crime mCrime;


    public CrimeHolder(LayoutInflater inflater, ViewGroup parent) {
        //pass in itemView to constructor of ViewHolder
        // View heirarchy is now held in ViewHolders 'itemView' field
        super(inflater.inflate(R.layout.list_item_crime, parent, false));
        itemView.setOnClickListener(this);
        mTitle = itemView.findViewById(R.id.crime_title);
        mDate = itemView.findViewById(R.id.crime_date);
        mSolvedImage = itemView.findViewById(R.id.crime_solved_image);
    }

    public void bind(Crime crime) {
        mCrime = crime;
        mTitle.setText(mCrime.getTitle());
        mDate.setText(mCrime.getDate().toString());
        mSolvedImage.setVisibility(mCrime.isSolved() ? VISIBLE : GONE);
    }

    @Override
    public void onClick(View v) {
        Intent intent = CrimePagerActivity.newIntent(getContext(), mCrime.getId());
        startActivityForResult(intent, REQUEST_CRIME);

    }
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_CRIME) {
        //handle result
    }
}

private class CrimeAdapter extends RecyclerView.Adapter<CrimeHolder> {

    private List<Crime> mCrimes;

    public CrimeAdapter(List<Crime> crimes) {
        mCrimes = crimes;
    }

    @NonNull
    @Override
    public CrimeHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(getActivity());


        return new CrimeHolder(inflater, parent);

    }

    @Override
    public void onBindViewHolder(@NonNull CrimeHolder holder, int position) {
        Crime crime = mCrimes.get(position);
        holder.bind(crime);
    }

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

}

@Override
public void onResume() {
    super.onResume();
    updateUI();
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.fragment_crime_list, menu);

    MenuItem subtitle = menu.findItem(R.id.show_subtitle);
    if (subtitleVisible) {
        subtitle.setTitle("Hide Subtitle");
    } else {
        subtitle.setTitle(R.string.show_subtite);
    }
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.new_crime:
            Crime crime = new Crime();
            CrimeLab.get(getActivity()).addCrime(crime);
            Intent intent = CrimePagerActivity
                    .newIntent(getContext(), crime.getId());
            startActivity(intent);
            return true;
        case R.id.show_subtitle:
            subtitleVisible = !subtitleVisible;
            //RE-CREATES options menu, resulting in menu item text change
            getActivity().invalidateOptionsMenu();
            updateSubtitle();
            return true;

        default:
            return super.onOptionsItemSelected(item);
    }
}

public void updateSubtitle() {
    String subtitle;
    CrimeLab crimeLab = CrimeLab.get(getActivity());
    int crimeCount = crimeLab.getCrimes().size();
    if (subtitleVisible) {
        subtitle = getResources().getQuantityString(R.plurals.subtitle_plural, crimeCount, crimeCount);
    } else {
        subtitle = "Criminal Intent";
    }
    AppCompatActivity activity = (AppCompatActivity) getActivity();
    activity.getSupportActionBar().setTitle(subtitle);
}

}

2 个答案:

答案 0 :(得分:0)

我在空适配器中发现了一个问题

mcall <- match.call(dgamma, expr)
mcall$shape
# [1] 0.5 
mcall$rate
# [1] 0.1

因此不会显示任何内容,如果您想显示空文本,请使用

@Override
    public int getItemCount() {
        return 0;
    }

答案 1 :(得分:0)

使用adapterview.setEmptyView(View)

Android会为您处理所有这一切。

请注意,您的空白视图必须位于同一布局文件中。