"Android Programming Definitive Guide (Third Edition)" Chapter 10 Challenge Exercise Demo

"Android Programming Definitive Guide (Third Edition)" Chapter 10 Challenge Exercise Demo

I didn’t write it too late yesterday. I’ll make it up today. There are two challenging exercises in Chapter 10. At present, only the first one has been implemented. The second one has not been made yet.

CrimeListFragment.java:

public void onClick(View view) {
            //Toast.makeText(getActivity(), mCrime.getTitle() + " clicked!", Toast.LENGTH_SHORT).show();
            Intent intent = CrimeActivity.newIntent(getActivity(), mCrime.getId());
            /**
             * Get the position of the clicked item here, which is the position
             */
            itemPosition = getAdapterPosition();
            startActivity(intent);
        }

private void updateUI() {
        CrimeLab crimeLab = CrimeLab.get (getActivity ());
        List<Crime> crimes = crimeLab.getCrimes();

        if (mAdapter == null) {
            mAdapter = new CrimeAdapter(crimes);
            mCrimeRecyclerview.setAdapter(mAdapter);
        } else {
            /**
             * Then change to mAdapter.notifyItemChanged(itemPosition); here
             */
           mAdapter.notifyItemChanged(itemPosition);
           //mAdapter.notifyDataSetChanged();
        }
    }

Get the position of the current item in the click event, store it in a static variable itemPosition, and then add it to the notifyItemChanged of updateUI to refresh the item separately, but because the item is too small, I really can't see much improvement in performance. , but would be helpful in later projects.

Attach the code: Demo

If there is an error in the code, please put it forward and ask for advice humbly, thank you :)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325571477&siteId=291194637
Recommended