add breadcrumbs

This commit is contained in:
tibbi 2016-07-17 16:52:18 +02:00
parent a03578d055
commit 31a4f65cc9
5 changed files with 144 additions and 17 deletions

View file

@ -0,0 +1,107 @@
package com.simplemobiletools.filemanager;
import android.content.Context;
import android.graphics.Point;
import android.util.AttributeSet;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
public class Breadcrumbs extends LinearLayout {
private LayoutInflater mInflater;
private int mDeviceWidth;
public Breadcrumbs(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
private void init(Context context) {
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
final Point deviceDisplay = new Point();
display.getSize(deviceDisplay);
mDeviceWidth = deviceDisplay.x;
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
final int paddingTop = getPaddingTop();
final int paddingLeft = getPaddingLeft();
final int childRight = getMeasuredWidth() - getPaddingRight();
final int childBottom = getMeasuredHeight() - getPaddingBottom();
final int childHeight = childBottom - paddingTop;
final int usableWidth = mDeviceWidth - paddingLeft - getPaddingRight();
int maxHeight = 0;
int curWidth;
int curHeight;
int curLeft = paddingLeft;
int curTop = paddingTop;
final int cnt = getChildCount();
for (int i = 0; i < cnt; i++) {
final View child = getChildAt(i);
child.measure(MeasureSpec.makeMeasureSpec(usableWidth, MeasureSpec.AT_MOST),
MeasureSpec.makeMeasureSpec(childHeight, MeasureSpec.AT_MOST));
curWidth = child.getMeasuredWidth();
curHeight = child.getMeasuredHeight();
if (curLeft + curWidth >= childRight) {
curLeft = paddingLeft;
curTop += maxHeight;
maxHeight = 0;
}
child.layout(curLeft, curTop, curLeft + curWidth, curTop + curHeight);
if (maxHeight < curHeight)
maxHeight = curHeight;
curLeft += curWidth;
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
final int usableWidth = mDeviceWidth - getPaddingLeft() - getPaddingRight();
int width = 0;
int rowHeight = 0;
int lines = 1;
final int cnt = getChildCount();
for (int i = 0; i < cnt; i++) {
final View child = getChildAt(i);
measureChild(child, widthMeasureSpec, heightMeasureSpec);
width += child.getMeasuredWidth();
rowHeight = child.getMeasuredHeight();
if (width / usableWidth > 0) {
lines++;
width = child.getMeasuredWidth();
}
}
final int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
final int calculatedHeight = getPaddingTop() + getPaddingBottom() + (rowHeight * lines);
setMeasuredDimension(parentWidth, calculatedHeight);
}
public void setInitialBreadcrumb() {
addBreadcrumb("home");
}
public void addBreadcrumb(String text) {
final View view = mInflater.inflate(R.layout.breadcrumb_item, null, false);
final TextView textView = (TextView) view.findViewById(R.id.breadcrumb_text);
textView.setText(text);
addView(view);
}
public void removeBreadcrumb() {
removeView(getChildAt(getChildCount() - 1));
}
}

View file

@ -6,20 +6,24 @@ import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import com.simplemobiletools.filemanager.Breadcrumbs;
import com.simplemobiletools.filemanager.Config;
import com.simplemobiletools.filemanager.Constants;
import com.simplemobiletools.filemanager.R;
import com.simplemobiletools.filemanager.Utils;
import com.simplemobiletools.filemanager.fragments.ItemsFragment;
import com.simplemobiletools.filemanager.models.FileDirItem;
import butterknife.BindView;
import butterknife.ButterKnife;
public class MainActivity extends AppCompatActivity implements ItemsFragment.ItemInteractionListener {
@BindView(R.id.breadcrumbs) Breadcrumbs mBreadcrumbs;
private static final int STORAGE_PERMISSION = 1;
@Override
@ -47,13 +51,12 @@ public class MainActivity extends AppCompatActivity implements ItemsFragment.Ite
private void initRootFileManager() {
final String path = Environment.getExternalStorageDirectory().toString();
openPath(path);
mBreadcrumbs.setInitialBreadcrumb();
}
private void openPath(String path) {
final Bundle bundle = new Bundle();
bundle.putString(Constants.PATH, path);
if (getSupportFragmentManager().getBackStackEntryCount() > 0)
setTitle(path);
final ItemsFragment fragment = new ItemsFragment();
fragment.setArguments(bundle);
@ -81,16 +84,11 @@ public class MainActivity extends AppCompatActivity implements ItemsFragment.Ite
@Override
public void onBackPressed() {
final FragmentManager manager = getSupportFragmentManager();
final int cnt = manager.getBackStackEntryCount();
final int cnt = getSupportFragmentManager().getBackStackEntryCount();
if (cnt == 1)
finish();
else {
if (cnt == 2) {
setTitle(getResources().getString(R.string.app_name));
} else {
setTitle(manager.getBackStackEntryAt(cnt - 2).getName());
}
mBreadcrumbs.removeBreadcrumb();
super.onBackPressed();
}
}
@ -110,7 +108,8 @@ public class MainActivity extends AppCompatActivity implements ItemsFragment.Ite
}
@Override
public void itemClicked(String path) {
openPath(path);
public void itemClicked(FileDirItem item) {
openPath(item.getPath());
mBreadcrumbs.addBreadcrumb(" -> " + item.getName());
}
}

View file

@ -163,7 +163,7 @@ public class ItemsFragment extends android.support.v4.app.Fragment
final FileDirItem item = mItems.get(position);
if (item.getIsDirectory()) {
if (mListener != null)
mListener.itemClicked(item.getPath());
mListener.itemClicked(item);
} else {
final String path = item.getPath();
final File file = new File(path);
@ -588,6 +588,6 @@ public class ItemsFragment extends android.support.v4.app.Fragment
}
public interface ItemInteractionListener {
void itemClicked(String path);
void itemClicked(FileDirItem item);
}
}

View file

@ -1,6 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
android:id="@+id/fragment_holder"
<RelativeLayout
android:id="@+id/main_screen"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
android:layout_height="match_parent">
<com.simplemobiletools.filemanager.Breadcrumbs
android:id="@+id/breadcrumbs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/activity_margin"/>
<FrameLayout
android:id="@+id/fragment_holder"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/breadcrumbs"/>
</RelativeLayout>

View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="@+id/breadcrumb_text"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>