当前位置: 移动互联网学院 > Android开发 > recyclerview下拉刷新的实现
recyclerview下拉刷新的实现 时间:2017-04-26     来源:Android开发学习网

RecyclerView是Android L版本中新添加的一个用来取代ListView的SDK,今天为大家分享下如何实现recyclerview下拉刷新。

public View onCreateView(LayoutInflater inflater, ViewGroup container,

Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.fragment_crime_list, container, false);

mCrimeRecyclerView = (RecyclerView) view

.findViewById(R.id.crime_recycler_view);

mEmptyView = (TextView)view.findViewById(R.id.empty_tip);

mCrimeRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

isRecycleViewEmpty();//判断是否为空

updateUI();

if (savedInstanceState != null) {

mSubtitleVisible = savedInstanceState.getBoolean(SAVED_SUBTITLE_VISIBLE);

}

mEmptyView.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

newIntent();}});

return view;}

对于RecyclerView的话,且是在fragment里面的,我们需要在onCreateView方法里先导入fragmentlist视图,再在此基础上导入RecyclerView视图。 然后为RecyclerView设置一个LayoutManager,可设置linearLayout或者是GridLayout等等

private void updateUI() {

isRecycleViewEmpty();

CrimeLab crimeLab = CrimeLab.get(getActivity());

List crimes = crimeLab.getCrimes();

if (mAdapter == null) {

mAdapter = new CrimeAdapter(crimes);

mCrimeRecyclerView.setAdapter(mAdapter);

} else {

mAdapter.setCrimes(crimes);

mAdapter.notifyDataSetChanged();

}

在updateUI里,我们先检查RecyclerView是否为空,如果为空,则显示带有提示文字的视图,如果不为空,则正常显示(忽略)。我们从crimeLab里获取Crimes数据,然后我们判断Adapter是否为空,若为空,调用setAdapter方法将数据设置进Adapter里面。若不为空,直接调用setCrime方法将数据源更换,并调用notifyDataSetChanged方法强制刷新View。

private class CrimeHolder extends RecyclerView.ViewHolder

implements View.OnClickListener {

private TextView mTitleTextView;

private TextView mDateTextView;

private CheckBox mSolvedCheckBox;

private Crime mCrime;

public CrimeHolder(View itemView) {

super(itemView);

itemView.setOnClickListener(this);

mTitleTextView = (TextView) itemView.findViewById(R.id.list_item_crime_title_text_view);

mDateTextView = (TextView) itemView.findViewById(R.id.list_item_crime_date_text_view);

mSolvedCheckBox = (CheckBox) itemView.findViewById(R.id.list_item_crime_solved_check_box);

}

public void bindCrime(Crime crime) {

mCrime = crime;

mTitleTextView.setText(mCrime.getTitle());

mDateTextView.setText(mCrime.getDate().toString());

mSolvedCheckBox.setChecked(mCrime.isSolved());

}

@Override

public void onClick(View v) {

Intent intent = CrimePagerActivity.newIntent(getActivity(), mCrime.getId());

startActivity(intent);

}}

private class CrimeAdapter extends RecyclerView.Adapter {

private List mCrimes;

public CrimeAdapter(List crimes) {

mCrimes = crimes;}

@Override

public CrimeHolder onCreateViewHolder(ViewGroup parent, int viewType) {

LayoutInflater layoutInflater = LayoutInflater.from(getActivity());

View view = layoutInflater.inflate(R.layout.list_item_crime, parent, false);

return new CrimeHolder(view);}

@Override

public void onBindViewHolder(CrimeHolder holder, int position) {

Crime crime = mCrimes.get(position);

holder.bindCrime(crime);}

@Override

public int getItemCount() {

return mCrimes.size();}

public void setCrimes(List crimes) {

mCrimes = crimes;}}}

我们在这里新建两个内部类 ViewHolder和Adapter,Adapter中泛型传入ViewHolder的类型,Adapter内重写onCreateViewHolder和onBindViewHolder和getItemCount方法,并添加了一个额外的setCrimes方法,以便在updateUI方法中Adapter不为空时直接修改数据源,提高了性能。

构造器Adapter:

这里用来将数据源就位。

onBindViewHolder(创建组件实质内容):

这里面传入两个参数,一个是ViewHolder类型的holder和int 的position ,holder就不用说了,position是指当前点击的位置,与Crimes里的数据对应,所以可以取得crime。之后调用holder中的bindCrime方法,绑定数据源(将bindCrime方法放入ViewHolder中ViewHolder与Adapter独立性更强)

onCreateViewHolder(创建组件视图):

正如其名这里用来创建ViewHolder,先取得托管fragment的Activity的LayoutInflater,并在里面创建layout的视图,并在里面实例化ViewHolder(将创建好的view传入viewHolder,ViewHolder便可以依赖该View创建按钮、文本之类的组件)。