Skip to content
This repository has been archived by the owner on Jun 28, 2022. It is now read-only.

create Master List Fragment with view holder pattern #6

Open
wants to merge 2 commits into
base: TFragments.04-Solution-CreateMasterListFragment
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,14 @@
android:supportsRtl="true"
android:theme="@style/AppTheme">

<!-- Declare the MainActivity in the manifest and set it to launch upon opening this app -->
<activity android:name=".ui.MainActivity" >
<activity android:name=".ui.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity android:name=".ui.AndroidMeActivity" />

</application>

</manifest>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,9 @@
// This activity is responsible for displaying the master list of all images
public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import com.example.android.android_me.R;

import java.util.List;


Expand All @@ -35,6 +36,7 @@ public class MasterListAdapter extends BaseAdapter {

/**
* Constructor method
*
* @param imageIds The list of images to display
*/
public MasterListAdapter(Context context, List<Integer> imageIds) {
Expand All @@ -60,25 +62,26 @@ public long getItemId(int i) {
return 0;
}

/**
* Creates a new ImageView for each item referenced by the adapter
*/
public View getView(final int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
// If the view is not recycled, this creates a new ImageView to hold an image
imageView = new ImageView(mContext);
// Define the layout parameters
imageView.setAdjustViewBounds(true);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
convertView = View.inflate(mContext, R.layout.image_item, null);

final ImageView imageView = (ImageView) convertView.findViewById(R.id.image_item);
final ViewHolder viewHolder = new ViewHolder(imageView);
convertView.setTag(viewHolder);
}

// Set the image resource and return the newly created ImageView
imageView.setImageResource(mImageIds.get(position));
return imageView;
final ViewHolder viewHolder = (ViewHolder) convertView.getTag();
viewHolder.imageViewItem.setImageResource(mImageIds.get(position));
return convertView;
}

private class ViewHolder {
private final ImageView imageViewItem;

ViewHolder(ImageView imageViewItem) {
this.imageViewItem = imageViewItem;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import com.example.android.android_me.R;
import com.example.android.android_me.data.AndroidImageAssets;


// This fragment displays all of the AndroidMe images in one large list
// The list appears as a grid of images
public class MasterListFragment extends Fragment {
Expand All @@ -43,7 +42,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
final View rootView = inflater.inflate(R.layout.fragment_master_list, container, false);

// Get a reference to the GridView in the fragment_master_list xml layout file
GridView gridView = (GridView) rootView.findViewById(R.id.images_grid_view);
GridView gridView = (GridView) rootView.findViewById(R.id.image_grid_view);

// Create the adapter
// This adapter takes in the context and an ArrayList of ALL the image resources to display
Expand All @@ -55,5 +54,4 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
// Return the root view
return rootView;
}

}
25 changes: 13 additions & 12 deletions app/src/main/res/layout/activity_android_me.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<!--Copyright (C) 2017 The Android Open Source Project
<?xml version="1.0" encoding="utf-8"?><!--Copyright (C) 2017 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Expand All @@ -10,8 +9,7 @@
See the License for the specific language governing permissions and
limitations under the License.-->

<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
Expand All @@ -32,21 +30,24 @@

<!-- Three containers for each Android-Me body part -->
<!-- This container holds the head BodyPartFragment of the custom Android-Me image -->
<FrameLayout android:id="@+id/head_container"
android:layout_width="match_parent"
android:layout_height="180dp"
android:scaleType="centerInside"/>
<FrameLayout
android:id="@+id/head_container"
android:layout_width="match_parent"
android:layout_height="180dp"
android:scaleType="centerInside" />

<!-- The remaining containers for the body and leg BodyPartFragments -->
<FrameLayout android:id="@+id/body_container"
<FrameLayout
android:id="@+id/body_container"
android:layout_width="match_parent"
android:layout_height="180dp"
android:scaleType="centerInside"/>
android:scaleType="centerInside" />

<FrameLayout android:id="@+id/leg_container"
<FrameLayout
android:id="@+id/leg_container"
android:layout_width="match_parent"
android:layout_height="180dp"
android:scaleType="centerInside"/>
android:scaleType="centerInside" />


</LinearLayout>
Expand Down
6 changes: 2 additions & 4 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<!--Copyright (C) 2017 The Android Open Source Project
<?xml version="1.0" encoding="utf-8"?><!--Copyright (C) 2017 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Expand All @@ -11,8 +10,7 @@
limitations under the License.-->

<!-- Display the static master list fragment -->
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/master_list_fragment"
android:name="com.example.android.android_me.ui.MasterListFragment"
android:layout_width="match_parent"
Expand Down
10 changes: 3 additions & 7 deletions app/src/main/res/layout/fragment_body_part.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<!--Copyright (C) 2017 The Android Open Source Project
<?xml version="1.0" encoding="utf-8"?><!--Copyright (C) 2017 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Expand All @@ -11,10 +10,7 @@
limitations under the License.-->

<!-- This fragment layout displays just one image of an Android-Me body part -->
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/body_part_image_view"
android:layout_width="match_parent"
android:layout_height="match_parent">

</ImageView>
android:layout_height="match_parent" />
13 changes: 5 additions & 8 deletions app/src/main/res/layout/fragment_master_list.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<!--Copyright (C) 2017 The Android Open Source Project
<?xml version="1.0" encoding="utf-8"?><!--Copyright (C) 2017 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Expand All @@ -11,17 +10,15 @@
limitations under the License.-->

<!-- GridView that displays AndroidMe images -->
<GridView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/images_grid_view"
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/image_grid_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:columnWidth="180dp"
android:gravity="center"
android:horizontalSpacing="8dp"
android:numColumns="3"
android:padding="16dp"
android:stretchMode="columnWidth"
android:verticalSpacing="8dp">
</GridView>
android:verticalSpacing="8dp" />
8 changes: 8 additions & 0 deletions app/src/main/res/layout/image_item.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/image_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:padding="8dp"
android:scaleType="centerCrop" />