mirror of https://github.com/FongMi/TV.git
parent
f338f5c8d5
commit
6ddf3090f6
@ -0,0 +1,67 @@ |
||||
package com.fongmi.android.tv.ui.activity; |
||||
|
||||
import android.Manifest; |
||||
import android.content.Intent; |
||||
import android.net.Uri; |
||||
import android.os.Bundle; |
||||
|
||||
import androidx.viewbinding.ViewBinding; |
||||
|
||||
import com.fongmi.android.tv.databinding.ActivityFileBinding; |
||||
import com.fongmi.android.tv.ui.adapter.FileAdapter; |
||||
import com.fongmi.android.tv.ui.base.BaseActivity; |
||||
import com.github.catvod.utils.Path; |
||||
import com.permissionx.guolindev.PermissionX; |
||||
|
||||
import java.io.File; |
||||
|
||||
public class FileActivity extends BaseActivity implements FileAdapter.OnClickListener { |
||||
|
||||
private ActivityFileBinding mBinding; |
||||
private FileAdapter mAdapter; |
||||
private File dir; |
||||
|
||||
private boolean isRoot() { |
||||
return Path.root().equals(dir); |
||||
} |
||||
|
||||
@Override |
||||
protected ViewBinding getBinding() { |
||||
return mBinding = ActivityFileBinding.inflate(getLayoutInflater()); |
||||
} |
||||
|
||||
@Override |
||||
protected void initView(Bundle savedInstanceState) { |
||||
setRecyclerView(); |
||||
PermissionX.init(this).permissions(Manifest.permission.WRITE_EXTERNAL_STORAGE).request((allGranted, grantedList, deniedList) -> update(Path.root())); |
||||
} |
||||
|
||||
private void setRecyclerView() { |
||||
mBinding.recycler.setHasFixedSize(true); |
||||
mBinding.recycler.setAdapter(mAdapter = new FileAdapter(this)); |
||||
} |
||||
|
||||
private void update(File dir) { |
||||
mBinding.recycler.scrollToPosition(0); |
||||
mAdapter.addAll(Path.list(this.dir = dir)); |
||||
} |
||||
|
||||
@Override |
||||
public void onItemClick(File file) { |
||||
if (file.isDirectory()) { |
||||
update(file); |
||||
} else { |
||||
setResult(RESULT_OK, new Intent().setData(Uri.fromFile(file))); |
||||
finish(); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public void onBackPressed() { |
||||
if (isRoot()) { |
||||
super.onBackPressed(); |
||||
} else { |
||||
update(dir.getParentFile()); |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,65 @@ |
||||
package com.fongmi.android.tv.ui.adapter; |
||||
|
||||
import android.view.LayoutInflater; |
||||
import android.view.ViewGroup; |
||||
|
||||
import androidx.annotation.NonNull; |
||||
import androidx.recyclerview.widget.RecyclerView; |
||||
|
||||
import com.fongmi.android.tv.R; |
||||
import com.fongmi.android.tv.databinding.AdapterFileBinding; |
||||
|
||||
import java.io.File; |
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
public class FileAdapter extends RecyclerView.Adapter<FileAdapter.ViewHolder> { |
||||
|
||||
private final OnClickListener mListener; |
||||
private final List<File> mItems; |
||||
|
||||
public FileAdapter(OnClickListener listener) { |
||||
this.mItems = new ArrayList<>(); |
||||
this.mListener = listener; |
||||
} |
||||
|
||||
public interface OnClickListener { |
||||
|
||||
void onItemClick(File file); |
||||
} |
||||
|
||||
public void addAll(List<File> items) { |
||||
mItems.clear(); |
||||
mItems.addAll(items); |
||||
notifyDataSetChanged(); |
||||
} |
||||
|
||||
@Override |
||||
public int getItemCount() { |
||||
return mItems.size(); |
||||
} |
||||
|
||||
@NonNull |
||||
@Override |
||||
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { |
||||
return new ViewHolder(AdapterFileBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false)); |
||||
} |
||||
|
||||
@Override |
||||
public void onBindViewHolder(@NonNull ViewHolder holder, int position) { |
||||
File file = mItems.get(position); |
||||
holder.binding.name.setText(file.getName()); |
||||
holder.binding.getRoot().setOnClickListener(v -> mListener.onItemClick(file)); |
||||
holder.binding.image.setImageResource(file.isDirectory() ? R.drawable.ic_folder : R.drawable.ic_file); |
||||
} |
||||
|
||||
static class ViewHolder extends RecyclerView.ViewHolder { |
||||
|
||||
private final AdapterFileBinding binding; |
||||
|
||||
ViewHolder(@NonNull AdapterFileBinding binding) { |
||||
super(binding.getRoot()); |
||||
this.binding = binding; |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,20 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" |
||||
xmlns:app="http://schemas.android.com/apk/res-auto" |
||||
xmlns:tools="http://schemas.android.com/tools" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="match_parent" |
||||
android:fitsSystemWindows="true"> |
||||
|
||||
<androidx.recyclerview.widget.RecyclerView |
||||
android:id="@+id/recycler" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="match_parent" |
||||
android:clipChildren="false" |
||||
android:clipToPadding="false" |
||||
android:overScrollMode="never" |
||||
android:padding="8dp" |
||||
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" |
||||
tools:listitem="@layout/adapter_file" /> |
||||
|
||||
</FrameLayout> |
||||
@ -0,0 +1,30 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" |
||||
xmlns:tools="http://schemas.android.com/tools" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
android:layout_margin="8dp" |
||||
android:background="@drawable/shape_vod_list" |
||||
android:foreground="@drawable/shape_vod" |
||||
android:gravity="center_vertical" |
||||
android:orientation="horizontal" |
||||
android:padding="12dp"> |
||||
|
||||
<ImageView |
||||
android:id="@+id/image" |
||||
android:layout_width="40dp" |
||||
android:layout_height="40dp" |
||||
android:layout_marginEnd="12dp" |
||||
android:scaleType="fitCenter" |
||||
tools:src="@drawable/ic_folder" /> |
||||
|
||||
<TextView |
||||
android:id="@+id/name" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
android:singleLine="true" |
||||
android:textColor="@color/white" |
||||
android:textSize="14sp" |
||||
tools:text="Download" /> |
||||
|
||||
</LinearLayout> |
||||
Loading…
Reference in new issue