create a grid of directories

This commit is contained in:
tibbi 2016-02-20 16:30:28 +01:00
parent 75288f6351
commit c157c5bb88
7 changed files with 150 additions and 9 deletions

View file

@ -3,9 +3,9 @@ package gallery.simplemobiletools.com;
public class Directory { public class Directory {
private final String path; private final String path;
private final String name; 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.path = path;
this.name = name; this.name = name;
this.photoCnt = photoCnt; this.photoCnt = photoCnt;
@ -19,7 +19,7 @@ public class Directory {
return name; return name;
} }
public int getPhotoCnt() { public String getPhotoCnt() {
return photoCnt; return photoCnt;
} }
} }

View file

@ -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<Directory> dirs;
private final LayoutInflater inflater;
public DirectoryAdapter(Context context, List<Directory> 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);
}
}
}

View file

@ -5,12 +5,14 @@ import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.provider.MediaStore; import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.widget.GridView;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.TreeMap;
public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity {
private List<Directory> dirs; private List<Directory> dirs;
@ -20,19 +22,23 @@ public class MainActivity extends AppCompatActivity {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
Map<String, Integer> directories = getImages(); final Map<String, Integer> directories = getImages();
dirs = new ArrayList<>(directories.size()); dirs = new ArrayList<>(directories.size());
for (Map.Entry<String, Integer> dir : directories.entrySet()) { for (Map.Entry<String, Integer> dir : directories.entrySet()) {
final String path = dir.getKey(); final String path = dir.getKey();
final String name = path.substring(path.lastIndexOf("/") + 1); 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)); 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<String, Integer> getImages() { private Map<String, Integer> getImages() {
Map<String, Integer> dirs = new HashMap<>(); final Map<String, Integer> dirs = new TreeMap<>();
final Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; final Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
final Cursor cursor = getContentResolver().query(uri, null, null, null, null); final Cursor cursor = getContentResolver().query(uri, null, null, null, null);

View file

@ -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());
}
}

View file

@ -1,9 +1,18 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout <RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent">
tools:context="gallery.simplemobiletools.com.MainActivity">
<GridView
android:id="@+id/photo_grid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/black"
android:columnWidth="@dimen/photo_size"
android:horizontalSpacing="1dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="1dp"/>
</RelativeLayout> </RelativeLayout>

View file

@ -0,0 +1,37 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<gallery.simplemobiletools.com.MyImageView
android:id="@+id/photo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="#44444444"
android:paddingBottom="4dp"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="4dp">
<TextView
android:id="@+id/dir_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="4dp"
android:textColor="@android:color/white"/>
<TextView
android:id="@+id/photo_cnt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/dir_name"
android:textColor="@android:color/white"/>
</RelativeLayout>
</RelativeLayout>

View file

@ -1,3 +1,4 @@
<resources> <resources>
<dimen name="activity_margin">16dp</dimen> <dimen name="activity_margin">16dp</dimen>
<dimen name="photo_size">150dp</dimen>
</resources> </resources>