Android程式設計權威指南(第二版)— 第10章挑戰練習

fulinux發表於2017-09-28
diff --git a/app/src/main/java/com/example/android/criminalintent/CrimeFragment.java b/app/src/main/java/com/example/android/criminalintent/CrimeFragment.java
index b446bc9..af134aa 100644
--- a/app/src/main/java/com/example/android/criminalintent/CrimeFragment.java
+++ b/app/src/main/java/com/example/android/criminalintent/CrimeFragment.java
@@ -1,5 +1,7 @@
 package com.example.android.criminalintent;

+import android.app.Activity;
+import android.content.Intent;
 import android.os.Bundle;
 import android.support.v4.app.BundleCompat;
 import android.support.v4.app.Fragment;
@@ -17,6 +19,7 @@ import android.widget.EditText;
 import java.util.UUID;

 import static android.R.attr.format;
+import static android.R.attr.priority;

 /**
  * Created by Administrator on 2017/7/6.
@@ -27,6 +30,7 @@ public class CrimeFragment extends Fragment {
     private static final String ARG_CRIME_ID = "crime_id";

     private Crime mCrime;
+    private CrimeLab mCrimeLab;
     private EditText mTitleField;
     private Button mDateButton;
     private CheckBox mSolvedCheckBox;
@@ -40,6 +44,18 @@ public class CrimeFragment extends Fragment {
         return fragment;
     }

+    public static int getCrimeIndex(Intent intent) {
+        return intent.getIntExtra(ARG_CRIME_ID, 0);
+    }
+
+    public void returnResult() {
+        Intent data = new Intent();
+
+        mCrimeLab = CrimeLab.get(getActivity());
+        data.putExtra(ARG_CRIME_ID, mCrimeLab.getCrimeIndex(mCrime));
+        getActivity().setResult(Activity.RESULT_OK, data);
+    }
+
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
@@ -48,6 +64,8 @@ public class CrimeFragment extends Fragment {
 //                .getSerializableExtra(CrimeActivity.EXTRA_CRIME_ID);
         UUID crimeId = (UUID) getArguments().getSerializable(ARG_CRIME_ID);
         mCrime = CrimeLab.get(getActivity()).getCrime(crimeId);
+
+        returnResult();
     }

     @Override
diff --git a/app/src/main/java/com/example/android/criminalintent/CrimeLab.java b/app/src/main/java/com/example/android/criminalintent/CrimeLab.java
index cde1e11..e6baf0f 100644
--- a/app/src/main/java/com/example/android/criminalintent/CrimeLab.java
+++ b/app/src/main/java/com/example/android/criminalintent/CrimeLab.java
@@ -45,4 +45,8 @@ public class CrimeLab {

         return null;
     }
+
+    public int getCrimeIndex(Crime crime) {
+        return mCrimes.indexOf(crime);
+    }
 }
diff --git a/app/src/main/java/com/example/android/criminalintent/CrimeListFragment.java b/app/src/main/java/com/example/android/criminalintent/CrimeListFragment.java
index 8e323ca..791b8ae 100644
--- a/app/src/main/java/com/example/android/criminalintent/CrimeListFragment.java
+++ b/app/src/main/java/com/example/android/criminalintent/CrimeListFragment.java
@@ -1,10 +1,12 @@
 package com.example.android.criminalintent;

+import android.app.Activity;
 import android.content.Intent;
 import android.os.Bundle;
 import android.support.v4.app.Fragment;
 import android.support.v7.widget.LinearLayoutManager;
 import android.support.v7.widget.RecyclerView;
+import android.util.Log;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
@@ -19,8 +21,11 @@ import java.util.List;
  */

 public class CrimeListFragment extends Fragment {
+    private static final int REQUEST_CRIME = 1;
+    private static final String TAG = "CrimeListFragment";
     private RecyclerView mCrimeRecyclerView;
     private CrimeAdapter mAdapter;
+    private int mPosition;

     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container,
@@ -36,6 +41,16 @@ public class CrimeListFragment extends Fragment {
     }

     @Override
+    public void onActivityResult(int requestCode, int resultCode, Intent data) {
+        if(requestCode == REQUEST_CRIME) {
+            //Handle result
+            if(data != null) {
+                mPosition = CrimeFragment.getCrimeIndex(data);
+            }
+        }
+    }
+
+    @Override
     public void onResume() {
         super.onResume();
         updateUI();
@@ -45,8 +60,14 @@ public class CrimeListFragment extends Fragment {
         CrimeLab crimeLab = CrimeLab.get(getActivity());
         List<Crime> crimes = crimeLab.getCrimes();

-        mAdapter = new CrimeAdapter(crimes);
-        mCrimeRecyclerView.setAdapter(mAdapter);
+        if(mAdapter == null) {
+            mAdapter = new CrimeAdapter(crimes);
+            mCrimeRecyclerView.setAdapter(mAdapter);
+            Log.i(TAG, "updateUI: " + "fulinux1");
+        } else {
+            mAdapter.notifyItemChanged(mPosition);
+            Log.i(TAG, "updateUI: " + "fulinux2:" + mPosition);
+        }
     }

     private class CrimeHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
@@ -79,7 +100,8 @@ public class CrimeListFragment extends Fragment {
 //                    mCrime.getTitle() + " clicked!", Toast.LENGTH_SHORT).show();
 //            Intent intent = new Intent(getActivity(), CrimeActivity.class);
             Intent intent = CrimeActivity.newIntent(getActivity(), mCrime.getId());
-            startActivity(intent);
+//            startActivity(intent);
+            startActivityForResult(intent, REQUEST_CRIME);
         }
     }

diff --git a/app/src/main/res/layout/list_item_crime.xml b/app/src/main/res/layout/list_item_crime.xml
index 200bb19..baf29c0 100644
--- a/app/src/main/res/layout/list_item_crime.xml
+++ b/app/src/main/res/layout/list_item_crime.xml
@@ -8,6 +8,7 @@
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignParentRight="true"
+        android:clickable="false"
         android:padding="4dp"/>

     <TextView

相關文章