在file_manage_fragment使用RecyclerView来作为文件管理器的布局,文件部分要申请本地读写权限,尤其是运行时权限,还要在manifest文件中的<application>标签中android:requestLegacyExternalStorage="true" **发现当物理机WiFi网络连不上互联网或者没开数据时会闪退,所以要通过广播机制判断当前的网络状态**,之后再改 添加了文件布局 代码参考《第二行代码》中对recyclerview的瀑布流应用,需要创建布局除了放置recyclerview的fragment外还需要一个表示recyclerview子项的file文件布局(图标+文字),图标是请一个熟人帮忙设计的.然后图标pressed状态下有一个遮罩,也是要写一个selector来修改样式,所以同样在colors文件中添加我要使用的颜色master
parent
ac26f754af
commit
aa488274e7
@ -0,0 +1,63 @@
|
||||
package com.thankvinci.CloudKey.Files;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.Message;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import com.thankvinci.CloudKey.R;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class FileAdapter extends RecyclerView.Adapter<FileAdapter.ViewHolder> {
|
||||
|
||||
private List<FileItem> fileList;
|
||||
|
||||
static class ViewHolder extends RecyclerView.ViewHolder{
|
||||
ImageView icon;
|
||||
TextView name;
|
||||
View file;
|
||||
public ViewHolder(View fileView){
|
||||
super(fileView);
|
||||
file = fileView;
|
||||
icon = file.findViewById(R.id.file_icon);
|
||||
name = file.findViewById(R.id.file_name);
|
||||
}
|
||||
}
|
||||
public FileAdapter(List<FileItem> fileList){
|
||||
this.fileList = fileList;
|
||||
}
|
||||
@Override
|
||||
public FileAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
||||
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.file_item,parent,false);
|
||||
ViewHolder holder = new ViewHolder(view);
|
||||
holder.file.setOnClickListener(new View.OnClickListener(){
|
||||
@Override
|
||||
public void onClick(View v){
|
||||
int position = holder.getAdapterPosition();
|
||||
FileItem fitem = fileList.get(position);
|
||||
Toast.makeText(view.getContext(),fitem.getName(),Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
});
|
||||
return holder;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(FileAdapter.ViewHolder holder, int position) {
|
||||
FileItem fileItem = fileList.get(position);
|
||||
holder.icon.setImageResource(fileItem.getIcon());
|
||||
holder.name.setText(fileItem.getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
return fileList.size();
|
||||
}
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
package com.thankvinci.CloudKey.Files;
|
||||
|
||||
import android.widget.ImageView;
|
||||
|
||||
import com.thankvinci.CloudKey.R;
|
||||
|
||||
public class FileItem {
|
||||
private int icon;
|
||||
private String name;
|
||||
private boolean type; //File为true,Directory为false
|
||||
private boolean isZip; //在type为true下才有效,是压缩文件就为true,为普通文件就为false; 这里的压缩文件格式为zip,可能会增加rar和7z
|
||||
|
||||
public FileItem(String name, boolean type, boolean isZip){
|
||||
this.type = type;
|
||||
this.name = name;
|
||||
if(type){
|
||||
if(isZip){
|
||||
icon = R.drawable.zip;
|
||||
}else{
|
||||
icon = R.drawable.file;
|
||||
}
|
||||
}else{
|
||||
icon = R.drawable.directory;
|
||||
}
|
||||
}
|
||||
public String getName(){
|
||||
return name;
|
||||
}
|
||||
public int getIcon(){
|
||||
return icon;
|
||||
}
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<selector xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<item android:state_pressed="false" android:drawable="@color/transparent"/>
|
||||
<item android:state_pressed="true" android:drawable="@color/translucent"/>
|
||||
</selector>
|
@ -0,0 +1,36 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<androidx.constraintlayout.widget.ConstraintLayout 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="10dp"
|
||||
android:orientation="vertical"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/file_icon"
|
||||
android:layout_width="60dp"
|
||||
android:layout_height="60dp"
|
||||
app:layout_constraintLeft_toLeftOf="parent"
|
||||
app:layout_constraintRight_toRightOf="parent"
|
||||
android:scaleType="centerInside"
|
||||
tools:ignore="MissingConstraints" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/file_name"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:maxLines="2"
|
||||
android:lines="2"
|
||||
app:layout_constraintTop_toBottomOf="@id/file_icon"
|
||||
app:layout_constraintLeft_toLeftOf="parent"
|
||||
app:layout_constraintRight_toRightOf="parent"
|
||||
tools:ignore="MissingConstraints" />
|
||||
<ImageView
|
||||
android:id="@+id/file_icon_up"
|
||||
android:layout_width="100dp"
|
||||
android:layout_height="100dp"
|
||||
android:src="@drawable/icon_be_clicked"
|
||||
app:layout_constraintTop_toTopOf="@id/file_icon"
|
||||
tools:ignore="MissingConstraints" />
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
Loading…
Reference in new issue