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

Commit

Permalink
Merge pull request #99 from spacecowboy/divider_support
Browse files Browse the repository at this point in the history
Add ability to define a list divider in theme
  • Loading branch information
spacecowboy committed Aug 3, 2016
2 parents cf2012f + 3174cbb commit 41cd5d9
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 4 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ you like..
<item name="colorPrimaryDark">@color/primary_dark</item>
<item name="colorAccent">@color/accent</item>

<!-- Setting a divider is entirely optional -->
<item name="nnf_list_item_divider">?android:attr/listDivider</item>

<!-- Need to set this also to style create folder dialog -->
<item name="alertDialogTheme">@style/FilePickerAlertDialogTheme</item>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
Expand All @@ -33,13 +35,11 @@
import android.widget.TextView;
import android.widget.Toast;

import java.net.URI;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import static com.nononsenseapps.filepicker.Utils.appendPath;
import static com.nononsenseapps.filepicker.Utils.isValidFileName;

/**
* A fragment representing a list of Files.
Expand Down Expand Up @@ -85,6 +85,8 @@ public abstract class AbstractFilePickerFragment<T> extends Fragment
protected FileItemAdapter<T> mAdapter = null;
protected TextView mCurrentDirView;
protected EditText mEditTextFileName;
protected RecyclerView recyclerView;
protected LinearLayoutManager layoutManager;
protected SortedList<T> mFiles = null;
protected Toast mToast = null;
// Keep track if we are currently loading a directory, in case it takes a long time
Expand Down Expand Up @@ -167,13 +169,15 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
setupToolbar(toolbar);
}

RecyclerView recyclerView = (RecyclerView) view.findViewById(android.R.id.list);
recyclerView = (RecyclerView) view.findViewById(android.R.id.list);
// improve performance if you know that changes in content
// do not change the size of the RecyclerView
recyclerView.setHasFixedSize(true);
// use a linear layout manager
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
// Set Item Decoration if exists
configureItemDecoration(inflater, recyclerView);
// Set adapter
mAdapter = new FileItemAdapter<>(this);
recyclerView.setAdapter(mAdapter);
Expand Down Expand Up @@ -231,6 +235,22 @@ public void afterTextChanged(Editable s) {
return view;
}

/**
* Checks if a divider drawable has been defined in the current theme. If it has, will apply
* an item decoration with the divider. If no divider has been specified, then does nothing.
*/
protected void configureItemDecoration(@NonNull LayoutInflater inflater,
@NonNull RecyclerView recyclerView) {
final TypedArray attributes =
getActivity().obtainStyledAttributes(new int[]{R.attr.nnf_list_item_divider});
Drawable divider = attributes.getDrawable(0);
attributes.recycle();

if (divider != null) {
recyclerView.addItemDecoration(new DividerItemDecoration(divider));
}
}

/**
* Called when the cancel-button is pressed.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.nononsenseapps.filepicker;

import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
* Basic ItemDecoration which loads a drawable as a divider.
*/
public class DividerItemDecoration extends RecyclerView.ItemDecoration {
private Drawable mDivider;

public DividerItemDecoration(Drawable divider) {
mDivider = divider;
}

@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);

if (parent.getChildAdapterPosition(view) == 0) {
return;
}

outRect.top = mDivider.getIntrinsicHeight();
}

@Override
public void onDraw(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
int dividerLeft = parent.getPaddingLeft();
int dividerRight = parent.getWidth() - parent.getPaddingRight();

int childCount = parent.getChildCount();
for (int i = 0; i < childCount - 1; i++) {
View child = parent.getChildAt(i);

RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();

int dividerTop = child.getBottom() + params.bottomMargin;
int dividerBottom = dividerTop + mDivider.getIntrinsicHeight();

mDivider.setBounds(dividerLeft, dividerTop, dividerRight, dividerBottom);
mDivider.draw(canvas);
}
}
}
2 changes: 2 additions & 0 deletions library/src/main/res/values/attr.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@
<attr name="nnf_save_icon_color" format="color"/>
<!-- Color to apply to the directory icons in list -->
<attr name="nnf_dir_icon_color" format="color"/>
<!-- Drawable to use as a divider between list items (optional) -->
<attr name="nnf_list_item_divider" format="reference"/>
</resources>
2 changes: 2 additions & 0 deletions sample/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
</style>

<style name="SampleThemeLight" parent="NNF_BaseTheme.Light">
<item name="nnf_list_item_divider">?android:attr/listDivider</item>

<item name="colorPrimary">@color/primary</item>
<item name="colorPrimaryDark">@color/primary_dark</item>
<item name="colorAccent">@color/accent</item>
Expand Down

0 comments on commit 41cd5d9

Please sign in to comment.