Android软键盘显示隐藏事件监听

节选自:Android软键盘显示隐藏事件监听


由于安卓系统并没有提供软键盘弹入和弹出的系统方法,所以我们有时候需要用到监听软件盘的时候会非常的麻烦,今天我根据监听视图树中全局布局发生改变或者视图树中的某个视图的可视状态发生改变超过一定的数值来监听软键盘的弹入弹出事件,使用起来也非常简单.

继续阅读Android软键盘显示隐藏事件监听

使用单Activity多Fragment架构的一些总结

节选自:使用单Activity多Fragment架构的一些总结


MainFragment中,PagerAdapter的FragmentManager要使用Fragment的FragmentManager,通过getChildFragmentManager()获得,而不能使用getActivity().getSupportFragmentManager();

getChildFragmentManager()区别于getActivity().getSupportFragmentManager(),因为在Fragment中使用ViewPager显示多个Fragment,所以需要使用fragment中的FragmentManager;

如果使用getActivity().getSupportFragmentManager(),在ViewPager中的Fragment打开另一个Fragment时有两个问题:
(1)、两个Fragment的onCreateOptionsMenu都会被调用,从而导致被打开的Fragment的Toolbar菜单按钮点击事件失效,且会一起显示出来;
(2)、返回时,ViewPager中的Fragment的视图会为空,显示空白;

Fragment全解析系列(二):正确的使用姿势

节选自:Fragment全解析系列(二):正确的使用姿势


Fragment是可以让你的app纵享丝滑的设计,如果你的app想在现在基础上性能大幅度提高,并且占用内存降低,同样的界面Activity占用内存比Fragment要多,响应速度Fragment比Activty在中低端手机上快了很多,甚至能达到好几倍!如果你的app当前或以后有移植平板等平台时,可以让你节省大量时间和精力

Android压缩文件列表为ZIP格式

import android.os.Environment;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipUtil {
    // 线程池
    private static final ExecutorService ThreadPool = Executors.newCachedThreadPool();

    /**
     * 压缩文件列表到SD
     *
     * @param fileName 压缩文件名称
     * @param pathList 要压缩的文件路径列表
     * @param callback 回调
     */
    public static void ZipFilesToSD(String fileName, List<String> pathList, ZipCallback callback) {
        String zipFilePath = Environment.getExternalStorageDirectory() + File.separator + fileName;
        ZipFiles(zipFilePath, pathList, callback);
    }

    /**
     * 压缩文件列表
     *
     * @param zipFilePath 压缩文件保存路径
     * @param pathList    要压缩的文件路径列表
     * @param callback    回调
     */
    public static void ZipFiles(final String zipFilePath, final List<String> pathList, final ZipCallback callback) {
        ThreadPool.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    ZipOutputStream outZip = new ZipOutputStream(new FileOutputStream(zipFilePath));
                    for (String path : pathList) {
                        File file = new File(path);
                        ZipEntry zipEntry = new ZipEntry(file.getParentFile().getName() + File.separator + file.getName());
                        FileInputStream inputStream = new FileInputStream(file);
                        outZip.putNextEntry(zipEntry);
                        int len;
                        byte[] buffer = new byte[4096];
                        while ((len = inputStream.read(buffer)) != -1) {
                            outZip.write(buffer, 0, len);
                        }
                        outZip.closeEntry();
                    }
                    outZip.finish();
                    outZip.close();
                    callback.onSuccess(zipFilePath);
                } catch (IOException e) {
                    e.printStackTrace();
                    callback.onFailed(e);
                }
            }
        });
    }

    /**
     * 回调接口
     */
    public interface ZipCallback {
        void onSuccess(String zipFilePath);

        void onFailed(Exception e);
    }
}

参考链接:

Android文件或文件夹压缩成.zip格式的压缩包

三种方法把文件读成一个字符串

节选自:三种方法把文件读成一个字符串


//Example 1
 
//Read file content into string with - Files.lines(Path path, Charset cs)
 
private static String readLineByLineJava8(String filePath)
{
    StringBuilder contentBuilder = new StringBuilder();
    try (Stream<String> stream = Files.lines( 
                                     Paths.get(filePath), StandardCharsets.UTF_8))
    {
        stream.forEach(s -> contentBuilder.append(s).append("\n"));
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    return contentBuilder.toString();
}
 
//Example 2
 
//Read file content into string with - Files.readAllBytes(Path path)
 
private static String readAllBytesJava7(String filePath)
{
    String content = "";
    try
    {
        content = new String ( Files.readAllBytes( Paths.get(filePath) ) );
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    return content;
}
 
//Example 3
 
//Read file content into string with - using BufferedReader and FileReader
//You can use this if you are still not using Java 8
 
private static String usingBufferedReader(String filePath)
{
    StringBuilder contentBuilder = new StringBuilder();
    try (BufferedReader br = new BufferedReader(new FileReader(filePath)))
    {
 
        String sCurrentLine;
        while ((sCurrentLine = br.readLine()) != null)
        {
            contentBuilder.append(sCurrentLine).append("\n");
        }
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    return contentBuilder.toString();
}