【Android】 自定义文件上传列表

74 阅读2分钟

【Android】 自定义文件上传列表

c89dd607d91774cb77095716dfdeff8d.jpg

创建进度条样式文件(渐变色)

gradient_progress_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:id="@android:id/background">
        <shape>
            <corners android:radius="5dp"/>
        </shape>
        <!--    背景颜色-->
        <color android:color="#FFFFFF"/>
    </item>

    <item android:id="@android:id/progress">
        <clip
            android:clipOrientation="horizontal"
            android:gravity="left">
            <shape>
                <corners android:radius="5dp"/>
                <!--  开始颜色,中途颜色,最后颜色-->
                <gradient
                    android:startColor="@color/theme_color_cyan_05"
                    android:centerColor="@color/theme_color_cyan_08"
                    android:endColor="@color/theme_color_cyan"/>
            </shape>
        </clip>
    </item>
</layer-list>

创建List的Item

item_list_folder_progress.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingTop="8dp"
    android:paddingBottom="8dp"
>
    <TextView
        android:id="@+id/textFileName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="测试文件"
    />
    <LinearLayout
        android:layout_marginTop="8dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:orientation="horizontal"
        android:gravity="center"
    >
        <ProgressBar
            android:id="@+id/progress_bar"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="2dp"
            android:progress="90"
            android:max="100"
            android:progressDrawable="@drawable/gradient_progress_bar"
            style="@style/Widget.AppCompat.ProgressBar.Horizontal"
        />
        <TextView
            android:id="@+id/progressText"
            android:layout_marginStart="8dp"
            android:layout_width="30dp"
            android:layout_height="wrap_content"
            android:text="100%"
            android:textSize="12sp"
        />
    </LinearLayout>
</LinearLayout>

创建Dialog页面

dialog_uploader_file_progress.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="450dp"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/recyclerView"
    android:background="@drawable/rounded_dialog"
    android:orientation="vertical"
    android:paddingBottom="20dp"
>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal"
        android:gravity="center_vertical"
        android:background="@drawable/gradient_dialog_top"
        >
        <TextView
            android:id="@+id/dialog_title"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:gravity="center"
            android:textColor="@color/white"
            android:text="标题"
            android:layout_marginEnd="-50dp"
            android:textSize="20sp"
            />
        <ImageView
            android:id="@+id/dialog_close_view"
            android:layout_gravity="center"
            android:gravity="center"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_marginEnd="10dp"
            android:src="@mipmap/icon_close"
        />
    </LinearLayout>
    <ListView
        android:padding="16dp"
        android:id="@+id/folder_content"
        android:layout_width="match_parent"
        android:layout_height="4000dp"
        tools:ignore="NestedScrolling"
    />
</LinearLayout>

List的对象页面

FileProgressInfo.java

package com.hxtx.august.MyObject;

public class FileProgressInfo {
    private String name;
    private Integer progress;

    public FileProgressInfo(String name, Integer progress) {
        this.name = name;
        this.progress = progress;
    }

    public String getName() {
        return name;
    }

    public Integer getProgress() {
        return progress;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setProgress(Integer progress) {
        this.progress = progress;
    }
}

List的Adapter页面

FolderProgressAdapter.java

package com.hxtx.august.MyList;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.hxtx.august.MyObject.FileProgressInfo;
import com.hxtx.august.R;

import java.util.List;

public class FolderProgressAdapter extends BaseAdapter {

    private final List<FileProgressInfo> fileList;
    private final Context context;

    public FolderProgressAdapter(List<FileProgressInfo> fileList, Context context) {
        this.fileList = fileList;
        this.context = context;
    }

    @Override
    public int getCount() {
        return fileList.size();
    }

    @Override
    public FileProgressInfo getItem(int position) {
        return fileList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        FileProgressInfo itemData = getItem(position);
        ViewHolder viewHolder;
        if(convertView == null){
            viewHolder = new ViewHolder();
            LayoutInflater inflater = LayoutInflater.from(context);
            convertView = inflater.inflate(R.layout.item_list_folder_progress, parent, false);
            viewHolder.textFileName = convertView.findViewById(R.id.textFileName);
            viewHolder.progressText = convertView.findViewById(R.id.progressText);
            viewHolder.progressBar = convertView.findViewById(R.id.progress_bar);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        String FileName = itemData.getName();
        Integer progress = itemData.getProgress();

        viewHolder.textFileName.setText(FileName);
        viewHolder.progressText.setText(progress.toString()+"%");
        viewHolder.progressBar.setProgress(progress);

        return convertView;
    }

    private static final class ViewHolder{
        TextView textFileName;
        TextView progressText;
        ProgressBar progressBar;
    }
}

MainActivity.java

// 定义列表容器
private ListView recyclerView;  
// 定义列表数据
private List<FileProgressInfo> fileProgressData = new ArrayList<>();

// 显示上传列表弹框并且开始上传
public void ShowUploaderDialog(List<UploaderDataContent> folderContentList) {
	AlertDialog diaLog;
	AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
	LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
	View dialogView = inflater.inflate(R.layout.dialog_uploader_file_progress, null);
	recyclerView = dialogView.findViewById(R.id.folder_content);
	TextView dialogTitle = dialogView.findViewById(R.id.dialog_title);
	ImageView dialogCloseView = dialogView.findViewById(R.id.dialog_close_view);
	dialogTitle.setText("文件上传");
	builder.setCancelable(false);
	builder.setView(dialogView);
	diaLog = builder.show();
	dialogCloseView.setOnClickListener( v -> {
		diaLog.dismiss();
	});
	multerUploaderFiles(folderContentList);
}

// 在上传文件处
String fileName = filePath.getName();  
Message msgFileName = Message.obtain();  
msgFileName.what = 2;msgFileName.obj = fileName;  
handlerCallbackFunction.sendMessage(msgFileName);

// 在进度条处
@SuppressLint("SetTextI18n")
@Override
public void onProgress(long currentLength, long contentLength) {
   int temp_progress = (int) (((float) currentLength / contentLength) * 100);
	Message msg = Message.obtain();
	msg.what = 1;msg.obj = new FileProgressInfo(fileName,temp_progress);
	handlerCallbackFunction.sendMessage(msg);
}

/**
 * 回调处理获取到的文件内容
 */
@SuppressLint("HandlerLeak")
private Handler handlerCallbackFunction = new Handler() {
	@SuppressLint("DefaultLocale")
	@Override
	public void handleMessage(Message msg) {
		if(msg.what == 1){
			FileProgressInfo fileProgressInfo = (FileProgressInfo)msg.obj;
			for (FileProgressInfo fileProgress : fileProgressData) {
				if (fileProgressInfo.getName().equals(fileProgress.getName())) {
					fileProgress.setProgress(fileProgressInfo.getProgress());
					break;
				}
			}
			recyclerView.setAdapter(new FolderProgressAdapter(fileProgressData, mContext));
		} else if(msg.what == 2){
			String fileName = (String)msg.obj;
			fileProgressData.add(0, new FileProgressInfo(fileName,0));
			recyclerView.setAdapter(new FolderProgressAdapter(fileProgressData, mContext));
		}
	}
};