From c157c5bb882fffe4932853ee413a36af63c337d9 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 20 Feb 2016 16:30:28 +0100 Subject: [PATCH] create a grid of directories --- .../simplemobiletools/com/Directory.java | 6 +- .../com/DirectoryAdapter.java | 63 +++++++++++++++++++ .../simplemobiletools/com/MainActivity.java | 12 +++- .../simplemobiletools/com/MyImageView.java | 25 ++++++++ app/src/main/res/layout/activity_main.xml | 15 ++++- app/src/main/res/layout/photo_item.xml | 37 +++++++++++ app/src/main/res/values/dimens.xml | 1 + 7 files changed, 150 insertions(+), 9 deletions(-) create mode 100644 app/src/main/java/gallery/simplemobiletools/com/DirectoryAdapter.java create mode 100644 app/src/main/java/gallery/simplemobiletools/com/MyImageView.java create mode 100644 app/src/main/res/layout/photo_item.xml diff --git a/app/src/main/java/gallery/simplemobiletools/com/Directory.java b/app/src/main/java/gallery/simplemobiletools/com/Directory.java index 730a4b94a..53dd4b6f0 100644 --- a/app/src/main/java/gallery/simplemobiletools/com/Directory.java +++ b/app/src/main/java/gallery/simplemobiletools/com/Directory.java @@ -3,9 +3,9 @@ package gallery.simplemobiletools.com; public class Directory { private final String path; private final String name; - private final int photoCnt; + private final String photoCnt; - public Directory(String path, String name, int photoCnt) { + public Directory(String path, String name, String photoCnt) { this.path = path; this.name = name; this.photoCnt = photoCnt; @@ -19,7 +19,7 @@ public class Directory { return name; } - public int getPhotoCnt() { + public String getPhotoCnt() { return photoCnt; } } diff --git a/app/src/main/java/gallery/simplemobiletools/com/DirectoryAdapter.java b/app/src/main/java/gallery/simplemobiletools/com/DirectoryAdapter.java new file mode 100644 index 000000000..201353d14 --- /dev/null +++ b/app/src/main/java/gallery/simplemobiletools/com/DirectoryAdapter.java @@ -0,0 +1,63 @@ +package gallery.simplemobiletools.com; + +import android.content.Context; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.BaseAdapter; +import android.widget.TextView; + +import java.util.List; + +public class DirectoryAdapter extends BaseAdapter { + private final List dirs; + private final LayoutInflater inflater; + + public DirectoryAdapter(Context context, List dirs) { + this.dirs = dirs; + this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); + } + + @Override + public View getView(int position, View view, ViewGroup parent) { + ViewHolder holder; + if (view == null) { + view = inflater.inflate(R.layout.photo_item, parent, false); + holder = new ViewHolder(view); + view.setTag(holder); + } else { + holder = (ViewHolder) view.getTag(); + } + + Directory dir = dirs.get(position); + holder.dirName.setText(dir.getName()); + holder.photoCnt.setText(dir.getPhotoCnt()); + + return view; + } + + @Override + public int getCount() { + return dirs.size(); + } + + @Override + public Object getItem(int position) { + return dirs.get(position); + } + + @Override + public long getItemId(int position) { + return 0; + } + + static class ViewHolder { + TextView dirName; + TextView photoCnt; + + public ViewHolder(View view) { + dirName = (TextView) view.findViewById(R.id.dir_name); + photoCnt = (TextView) view.findViewById(R.id.photo_cnt); + } + } +} diff --git a/app/src/main/java/gallery/simplemobiletools/com/MainActivity.java b/app/src/main/java/gallery/simplemobiletools/com/MainActivity.java index c8822016d..370629873 100644 --- a/app/src/main/java/gallery/simplemobiletools/com/MainActivity.java +++ b/app/src/main/java/gallery/simplemobiletools/com/MainActivity.java @@ -5,12 +5,14 @@ import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore; import android.support.v7.app.AppCompatActivity; +import android.widget.GridView; import java.io.File; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.TreeMap; public class MainActivity extends AppCompatActivity { private List dirs; @@ -20,19 +22,23 @@ public class MainActivity extends AppCompatActivity { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); - Map directories = getImages(); + final Map directories = getImages(); dirs = new ArrayList<>(directories.size()); for (Map.Entry dir : directories.entrySet()) { final String path = dir.getKey(); final String name = path.substring(path.lastIndexOf("/") + 1); - final int cnt = dir.getValue(); + final String cnt = String.valueOf(dir.getValue()); dirs.add(new Directory(path, name, cnt)); } + + final GridView gridView = (GridView) findViewById(R.id.photo_grid); + DirectoryAdapter adapter = new DirectoryAdapter(this, dirs); + gridView.setAdapter(adapter); } private Map getImages() { - Map dirs = new HashMap<>(); + final Map dirs = new TreeMap<>(); final Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; final Cursor cursor = getContentResolver().query(uri, null, null, null, null); diff --git a/app/src/main/java/gallery/simplemobiletools/com/MyImageView.java b/app/src/main/java/gallery/simplemobiletools/com/MyImageView.java new file mode 100644 index 000000000..f2fd7800a --- /dev/null +++ b/app/src/main/java/gallery/simplemobiletools/com/MyImageView.java @@ -0,0 +1,25 @@ +package gallery.simplemobiletools.com; + +import android.content.Context; +import android.util.AttributeSet; +import android.widget.ImageView; + +public class MyImageView extends ImageView { + public MyImageView(Context context) { + super(context); + } + + public MyImageView(Context context, AttributeSet attrs) { + super(context, attrs); + } + + public MyImageView(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); + } + + @Override + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + super.onMeasure(widthMeasureSpec, heightMeasureSpec); + setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth()); + } +} diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index b7b0b0c48..729fd3ca3 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,9 +1,18 @@ + android:layout_height="match_parent"> + + diff --git a/app/src/main/res/layout/photo_item.xml b/app/src/main/res/layout/photo_item.xml new file mode 100644 index 000000000..78a038662 --- /dev/null +++ b/app/src/main/res/layout/photo_item.xml @@ -0,0 +1,37 @@ + + + + + + + + + + + + diff --git a/app/src/main/res/values/dimens.xml b/app/src/main/res/values/dimens.xml index e641ba457..846ddefce 100644 --- a/app/src/main/res/values/dimens.xml +++ b/app/src/main/res/values/dimens.xml @@ -1,3 +1,4 @@ 16dp + 150dp