《Android编程权威指南(第三版)》第八章挑战练习Demo

《Android编程权威指南(第三版)》第八章挑战练习Demo

第8章的直接就说RecyclerView,书本说的很详细就不阐述了,这次代码应该做注释的我都做了,废话少说,先发一张效果动态图:

贴代码,先是新建布局,其实就是加了一个按钮:




    

    

    

更改Crime类:

public class Crime {

    private UUID mId;
    private String mTitle;
    private Date mDate;
    private boolean mSolved;
    //是否联系警察
    private boolean mRequiresPolice;

    public Crime() {
        mId = UUID.randomUUID();
        mDate = new Date();
    }

更改CrimeLab的构造方法:

private CrimeLab(Context context) {
        mCrimes = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            Crime crime = new Crime();
            crime.setTitle("Crime #" + i);
            crime.setSolved(i % 2 == 0);
            //定义item序号为3或7的倍数时就是联系警察
            crime.setRequiresPolice(i % 3 == 0 || i % 7 == 0);
            mCrimes.add(crime);
        }
    }

最重要的的适配器代码和Holader:

/**
     * 新建一个专门给警察Police的Holder,其实和CrimeHolder差不多
     */
    private class CrimePoliceHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private TextView mTitleTextView;
        private TextView mDateTextView;
        private Button mConnect;
        private Crime mCrime;

        private CrimePoliceHolder(LayoutInflater inflater, ViewGroup parent) {
            super(inflater.inflate(R.layout.list_item_crime_police, parent, false));

            mTitleTextView = (TextView) itemView.findViewById(R.id.crime_title_police);
            mDateTextView = (TextView) itemView.findViewById(R.id.crime_date_police);
            mConnect = (Button) itemView.findViewById(R.id.connect_police);
            mConnect.setOnClickListener(this);
        }

        private void bind(Crime crime) {
            mCrime = crime;
            mTitleTextView.setText(mCrime.getTitle());
            mDateTextView.setText(mCrime.getDate().toString());
        }

        public void onClick(View view) {
            Toast.makeText(getActivity(), "已联系警察", Toast.LENGTH_SHORT).show();
        }
    }

    private class CrimeAdapter extends RecyclerView.Adapter {
        private List mCrimes;

        public CrimeAdapter(List crimes) {
            mCrimes = crimes;
        }

        /**
         * 这里最重要,获取当前item是否需要联系警察
         */
        @Override
        public int getItemViewType(int position) {
            int flag = 0;
            if (mCrimes.get(position).isRequiresPolice()){
                flag = 1;
            }
            return flag;
        }

        /**
         * getItemViewType的返回值返回到这里的viewType里面,通过判断viewType的值判断添加什么布局
         */
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
            if (viewType == 0) {
                return new CrimeHolder(layoutInflater, parent);
            } else {
                return new CrimePoliceHolder(layoutInflater, parent);
            }
        }

        /**
         * 这里就是直接添加控件的标题,功能等
         */
        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            if (holder instanceof CrimeHolder) {
                Crime crime = mCrimes.get(position);
                ((CrimeHolder) holder).bind(crime);
            } else if (holder instanceof CrimePoliceHolder){
                Crime crime = mCrimes.get(position);
                ((CrimePoliceHolder) holder).bind(crime);
            }
        }

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

这次的挑战练习主要是学会判断recyclerview的item判断,类似于recyclerview嵌套,关于recyclerview嵌套我有写过相关的博客,大家可以点击这里查看,本次挑战练习的Demo代码。

你可能感兴趣的:(Android)