1. 程式人生 > >Android程式設計權威指南(第二版)— 第13章挑戰練習-13.8

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

commit 1cdecd578c81d3ec6a522a0e317f2d1a3ae47c7f

Author: fulinux <[email protected]>
Date:   Mon Oct 9 17:54:16 2017 +0800

    完成13.8挑戰練習 用於RecyclerView的空檢視

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 ec2821f..b336c57 100644
--- a/app/src/main/java/com/example/android/criminalintent/CrimeListFragment.java
+++ b/app/src/main/java/com/example/android/criminalintent/CrimeListFragment.java
@@ -13,6 +13,7 @@ import android.view.MenuInflater;
 import android.view.MenuItem;
 import android.view.View;
 import android.view.ViewGroup;
+import android.widget.Button;
 import android.widget.CheckBox;
 import android.widget.TextView;

@@ -31,6 +32,8 @@ public class CrimeListFragment extends Fragment {
     private CrimeAdapter mAdapter;
     private int mPosition;
     private boolean mSubtitleVisible;
+    private TextView mTextView;
+    private Button mButton;

     @Override
     public void onCreate(Bundle savedInstanceState) {
@@ -46,6 +49,18 @@ public class CrimeListFragment extends Fragment {
         mCrimeRecyclerView = (RecyclerView) view.findViewById(R.id.crime_recycler_view);
         mCrimeRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

+        mTextView = (TextView) view.findViewById(R.id.no_crime_to_show_text);
+        mButton = (Button) view.findViewById(R.id.new_crime_btn);
+        mButton.setOnClickListener(new View.OnClickListener(){
+            @Override
+            public void onClick(View v) {
+                Crime crime = new Crime();
+                CrimeLab.get(getActivity()).addCrime(crime);
+                Intent intent = CrimePagerActivity.newIntent(getActivity(), crime.getId());
+                startActivity(intent);
+            }
+        });
+
         if(savedInstanceState != null) {
             mSubtitleVisible = savedInstanceState.getBoolean(SAVED_SUBTITLE_VISIBLE);
         }
@@ -126,6 +141,14 @@ public class CrimeListFragment extends Fragment {
         CrimeLab crimeLab = CrimeLab.get(getActivity());
         List<Crime> crimes = crimeLab.getCrimes();

+        if(crimes.size() > 0) {
+            mTextView.setVisibility(View.GONE);
+            mButton.setVisibility(View.GONE);
+        } else {
+            mTextView.setVisibility(View.VISIBLE);
+            mButton.setVisibility(View.VISIBLE);
+        }
+
         if(mAdapter == null) {
             mAdapter = new CrimeAdapter(crimes);
             mCrimeRecyclerView.setAdapter(mAdapter);
diff --git a/app/src/main/res/layout/fragment_crime_list.xml b/app/src/main/res/layout/fragment_crime_list.xml
index 60c5277..f541f65 100644
--- a/app/src/main/res/layout/fragment_crime_list.xml
+++ b/app/src/main/res/layout/fragment_crime_list.xml
@@ -1,8 +1,38 @@
 <?xml version="1.0" encoding="utf-8"?>
-<android.support.v7.widget.RecyclerView
-    xmlns:android="http://schemas.android.com/apk/res/android"
-    android:id="@+id/crime_recycler_view"
-    android:layout_width="match_parent"
-    android:layout_height="match_parent">
+<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
+              android:layout_width="match_parent"
+              android:layout_height="match_parent"
+              android:orientation="vertical"
+              xmlns:tools="http://schemas.android.com/tools">

-</android.support.v7.widget.RecyclerView>
\ No newline at end of file
+    <android.support.v7.widget.RecyclerView
+        xmlns:android="http://schemas.android.com/apk/res/android"
+        android:id="@+id/crime_recycler_view"
+        android:layout_width="match_parent"
+        android:layout_height="match_parent">
+    </android.support.v7.widget.RecyclerView>
+
+    <LinearLayout
+        android:layout_centerInParent="true"
+        android:orientation="vertical"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content">
+
+        <TextView
+            android:id="@+id/no_crime_to_show_text"
+            android:layout_gravity="center|bottom"
+            android:gravity="center"
+            android:text="@string/no_crime_to_show"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"/>
+
+        <Button
+            android:id="@+id/new_crime_btn"
+            android:layout_gravity="center"
+            android:gravity="center"
+            android:text="@string/new_crime"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:background="@android:color/holo_orange_dark"/>
+    </LinearLayout>
+</RelativeLayout>
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index 3bd1316..be3163e 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -12,6 +12,7 @@
     <string name="show_subtitle">Show Subtitle</string>
     <string name="hide_subtitle">Hide Subtitle</string>
     <string name="subtitle_format">%1$s crimes</string>
+    <string name="no_crime_to_show">No Crime to Show</string>

     <plurals name="subtitle_plurals">
         <item quantity="one">%1$s crime</item>