Skip to content

Commit 31050dd

Browse files
author
Majid Arabi
committed
support java projects,
added item click listener
1 parent f44684f commit 31050dd

File tree

14 files changed

+508
-192
lines changed

14 files changed

+508
-192
lines changed

app/src/main/AndroidManifest.xml

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
<category android:name="android.intent.category.LAUNCHER" />
1919
</intent-filter>
2020
</activity>
21+
<activity
22+
android:name=".JavaActivity"
23+
android:exported="true" />
2124
</application>
2225

2326
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package ir.one_developer.filepickerlibrary;
2+
3+
import android.graphics.Color;
4+
import android.os.Bundle;
5+
import android.util.Log;
6+
7+
import androidx.annotation.Nullable;
8+
import androidx.appcompat.app.AppCompatActivity;
9+
10+
import com.github.file_picker.FilePicker;
11+
import com.github.file_picker.model.Media;
12+
13+
import java.util.ArrayList;
14+
import java.util.List;
15+
16+
import ir.one_developer.filepickerlibrary.databinding.ActivityJavaBinding;
17+
18+
public class JavaActivity extends AppCompatActivity {
19+
20+
private FileAdapter adapter;
21+
private ActivityJavaBinding binding;
22+
private final List<Media> selectedFiles = new ArrayList<>();
23+
24+
@Override
25+
protected void onCreate(@Nullable Bundle savedInstanceState) {
26+
super.onCreate(savedInstanceState);
27+
binding = ActivityJavaBinding.inflate(getLayoutInflater());
28+
setContentView(binding.getRoot());
29+
setupViews();
30+
}
31+
32+
private void setupViews() {
33+
adapter = new FileAdapter();
34+
binding.rvFiles.setAdapter(adapter);
35+
binding.fab.setOnClickListener(v -> finish());
36+
binding.btnOpenFiles.setOnClickListener(v -> showFiles());
37+
}
38+
39+
private void showFiles() {
40+
new FilePicker.Builder(this)
41+
.setLimitItemSelection(3)
42+
.setAccentColor(Color.CYAN)
43+
.setCancellable(true)
44+
.setSelectedFiles(selectedFiles)
45+
.setOnSubmitClickListener(files -> {
46+
adapter.submitList(files);
47+
updateSelectedFiles(files);
48+
})
49+
.setOnItemClickListener((media, pos, adapter) -> {
50+
if (!media.getFile().isDirectory()) {
51+
adapter.setSelected(pos);
52+
}
53+
})
54+
.buildAndShow();
55+
}
56+
57+
private void updateSelectedFiles(List<Media> files) {
58+
selectedFiles.clear();
59+
selectedFiles.addAll(files);
60+
}
61+
62+
}

app/src/main/java/ir/one_developer/filepickerlibrary/MainActivity.kt

+22-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package ir.one_developer.filepickerlibrary
22

3+
import android.content.Intent
34
import android.os.Bundle
4-
import android.util.Log
55
import androidx.appcompat.app.AppCompatActivity
66
import androidx.core.content.ContextCompat
7+
import com.github.file_picker.listener.OnItemClickListener
8+
import com.github.file_picker.listener.OnSubmitClickListener
9+
import com.github.file_picker.adapter.ItemAdapter
710
import com.github.file_picker.model.Media
811
import com.github.file_picker.showFilePicker
912
import ir.one_developer.filepickerlibrary.databinding.ActivityMainBinding
@@ -31,18 +34,30 @@ class MainActivity : AppCompatActivity() {
3134
btnOpenFiles.setOnClickListener {
3235
showFiles()
3336
}
37+
fab.setOnClickListener {
38+
startActivity(Intent(this@MainActivity, JavaActivity::class.java))
39+
}
3440
}
3541

3642
private fun showFiles(): Unit = showFilePicker(
3743
limitItemSelection = 2,
3844
selectedFiles = selectedFiles,
3945
accentColor = ContextCompat.getColor(this@MainActivity, R.color.purple_700),
40-
titleTextColor = ContextCompat.getColor(this@MainActivity, R.color.purple_700)
41-
) {
42-
adapter.submitList(it)
43-
updateSelectedFiles(it)
44-
Log.i(TAG, "FilePicker:SelectedItems: $selectedFiles")
45-
}
46+
titleTextColor = ContextCompat.getColor(this@MainActivity, R.color.purple_700),
47+
onSubmitClickListener = object : OnSubmitClickListener {
48+
override fun onClick(files: List<Media>) {
49+
adapter.submitList(files)
50+
updateSelectedFiles(files)
51+
}
52+
},
53+
onItemClickListener = object : OnItemClickListener {
54+
override fun onClick(media: Media, position: Int, adapter: ItemAdapter) {
55+
if (!media.file.isDirectory) {
56+
adapter.setSelected(position)
57+
}
58+
}
59+
}
60+
)
4661

4762
private fun updateSelectedFiles(files: List<Media>) {
4863
selectedFiles.clear()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="24dp"
3+
android:height="24dp"
4+
android:viewportWidth="24"
5+
android:viewportHeight="24">
6+
<path
7+
android:fillColor="@android:color/white"
8+
android:pathData="M10,6L8.59,7.41 13.17,12l-4.58,4.59L10,18l6,-6z" />
9+
</vector>
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent"
7+
android:orientation="vertical"
8+
tools:context=".MainActivity">
9+
10+
<androidx.recyclerview.widget.RecyclerView
11+
android:id="@+id/rv_files"
12+
android:layout_width="match_parent"
13+
android:layout_height="match_parent"
14+
android:layout_above="@id/btn_open_files"
15+
android:clipToPadding="false"
16+
android:layoutDirection="rtl"
17+
android:padding="2dp"
18+
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
19+
app:spanCount="1"
20+
tools:itemCount="5"
21+
tools:listitem="@layout/file_layout" />
22+
23+
<com.google.android.material.button.MaterialButton
24+
android:id="@+id/btn_open_files"
25+
android:layout_width="match_parent"
26+
android:layout_height="wrap_content"
27+
android:layout_alignParentBottom="true"
28+
android:layout_marginHorizontal="16dp"
29+
android:layout_marginVertical="8dp"
30+
android:gravity="center"
31+
android:paddingVertical="12dp"
32+
android:text="Show Files"
33+
android:textColor="@color/white"
34+
android:textSize="22sp"
35+
app:backgroundTint="@color/purple_700"
36+
app:cornerRadius="16dp" />
37+
38+
<com.google.android.material.floatingactionbutton.FloatingActionButton
39+
android:id="@+id/fab"
40+
android:layout_width="wrap_content"
41+
android:layout_height="wrap_content"
42+
android:layout_above="@id/btn_open_files"
43+
android:layout_marginStart="16dp"
44+
android:rotation="180"
45+
app:backgroundTint="@color/purple_700"
46+
app:srcCompat="@drawable/ic_chevron_right"
47+
app:tint="@color/white" />
48+
49+
</RelativeLayout>

app/src/main/res/layout/activity_main.xml

+17-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
2+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:app="http://schemas.android.com/apk/res-auto"
44
xmlns:tools="http://schemas.android.com/tools"
55
android:layout_width="match_parent"
@@ -10,8 +10,8 @@
1010
<androidx.recyclerview.widget.RecyclerView
1111
android:id="@+id/rv_files"
1212
android:layout_width="match_parent"
13-
android:layout_height="0dp"
14-
android:layout_weight="1"
13+
android:layout_height="match_parent"
14+
android:layout_above="@id/btn_open_files"
1515
android:clipToPadding="false"
1616
android:layoutDirection="rtl"
1717
android:padding="2dp"
@@ -24,14 +24,26 @@
2424
android:id="@+id/btn_open_files"
2525
android:layout_width="match_parent"
2626
android:layout_height="wrap_content"
27+
android:layout_alignParentBottom="true"
2728
android:layout_marginHorizontal="16dp"
2829
android:layout_marginVertical="8dp"
2930
android:gravity="center"
30-
android:paddingVertical="16dp"
31+
android:paddingVertical="12dp"
3132
android:text="Show Files"
3233
android:textColor="@color/white"
3334
android:textSize="22sp"
3435
app:backgroundTint="@color/purple_700"
3536
app:cornerRadius="16dp" />
3637

37-
</androidx.appcompat.widget.LinearLayoutCompat>
38+
<com.google.android.material.floatingactionbutton.FloatingActionButton
39+
android:id="@+id/fab"
40+
android:layout_width="wrap_content"
41+
android:layout_height="wrap_content"
42+
android:layout_above="@id/btn_open_files"
43+
android:layout_alignParentEnd="true"
44+
android:layout_marginEnd="16dp"
45+
app:backgroundTint="@color/purple_700"
46+
app:srcCompat="@drawable/ic_chevron_right"
47+
app:tint="@color/white" />
48+
49+
</RelativeLayout>

file-picker/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ afterEvaluate {
5858
from components.release
5959
groupId = 'com.github.majidarabi'
6060
artifactId = 'file-picker'
61-
version = '0.0.6'
61+
version = '0.0.7'
6262
}
6363
}
6464
}

0 commit comments

Comments
 (0)