Android手機(jī)文件管理系統(tǒng)

本文主要介紹如何自定義顯示手機(jī)sd卡當(dāng)中的文件及文件夾竭业,這樣做的好處主要是能夠自己定義文件夾和文件的圖標(biāo)樣式成艘,而且能夠按照應(yīng)用的需要設(shè)計(jì)選擇的模式(本文介紹的是單選文件)爪飘。

第一步:創(chuàng)建ListView的自定義item文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp">

        //用于顯示文件和文件夾的圖標(biāo)
        <ImageView
            android:id="@+id/iv_icon"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_marginLeft="@dimen/interval_common"
            android:layout_centerVertical="true"
            android:src="@mipmap/ic_launcher_round"/>

        //用于顯示文件和文件夾的名稱
        <TextView
            android:id="@+id/tv_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/colorPrimaryDark"
            android:text="yinxinwdjwwwwwwwwwwwwww"
            android:textSize="20sp"
            android:layout_toRightOf="@+id/iv_icon"
            android:layout_toLeftOf="@+id/iv_check"
            android:layout_marginLeft="@dimen/interval_common"
            android:layout_marginRight="@dimen/interval_common"
            android:layout_centerVertical="true"
            android:ellipsize="middle"
            android:maxLines="1"/>
        
         //用于顯示文件被選中的圖標(biāo),文件夾則不顯示這個(gè)控件
        <ImageView
            android:id="@+id/iv_check"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_alignParentRight="true"
            android:layout_marginRight="@dimen/interval_common"
            android:layout_centerVertical="true"
            android:src="@mipmap/ic_launcher"/>
    </RelativeLayout>

</LinearLayout>

第二步:構(gòu)建Adapter砌左,這里采用的是itemview的方式脖咐。
Adapter文件

public class FileAdapter extends BaseAdapter {

    private Context context;
    private List<FileInfo> fileList;

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

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

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        FileInfo info = fileList.get(position);
        FileItemView itemView;
        if (convertView == null) {
            itemView = new FileItemView(context);
        } else {
            itemView = (FileItemView) convertView;
        }

        itemView.bind(info);
        itemView.setTag(info);
        return itemView;
    }
    
}

FileItemView類

public class FileItemView extends LinearLayout {

    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.iv_icon)
    ImageView ivIcon;
    @BindView(R.id.iv_check)
    ImageView ivCheck;

    public FileItemView(Context context) {
        super(context);
        init();
    }

    public FileItemView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public FileItemView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){
        inflate(getContext(), R.layout.item_file_adapter, this);
        ButterKnife.bind(this);
    }

    public void bind(FileInfo info){

        tvName.setText(info.getFile().getName());

        if (info.getFile().isDirectory()){
            ivIcon.setImageResource(R.mipmap.ic_launcher_round);
            ivCheck.setVisibility(View.GONE);
        }else {
          /**
           *這里可以根據(jù)文件的后綴名,設(shè)置對應(yīng)文件的圖標(biāo)
           **/
            ivIcon.setImageResource(R.mipmap.ic_launcher);
            ivCheck.setVisibility(View.VISIBLE);

            if (info.isCheck()){
                //設(shè)置文件選中時(shí)的圖標(biāo)
                ivCheck.setImageResource(R.color.colorPrimary);
            }else {
                //設(shè)置文件未選中時(shí)的圖標(biāo)
                ivCheck.setImageResource(R.color.colorAccent);
            }

        }
    }
}

FileInfo類

public class FileInfo {
    private File file;
    private boolean isCheck;

    public FileInfo(File file) {
        this.file = file;
        this.isCheck = false;
    }

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public boolean isCheck() {
        return isCheck;
    }

    public void setCheck(boolean check) {
        isCheck = check;
    }
}

注:FileItemView當(dāng)中的控件ID汇歹,采用的是BindView的方式屁擅,需要在gradle文件當(dāng)中添加依賴

compile 'com.jakewharton:butterknife:8.4.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'

第三步:在activity當(dāng)中使用adapter

activity的layout文件,添加一個(gè)ListView控件即可
對應(yīng)的xml文件

<ListView
        android:id="@+id/lv_phone_file"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></ListView>

activity文件

public class InputFileActivity extends BaseActivity {
    @BindView(R.id.lv_phone_file)
    ListView lvFile;


    private File currentFile;
    private List<FileInfo> fileList = new ArrayList<>();
    private FileAdapter fileAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_input_file);
        ButterKnife.bind(this);
        init();


    }

    private void init() {
        //初始為sdcard內(nèi)文件
        currentFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath());

        fileAdapter = new FileAdapter(InputFileActivity.this, fileList);
        lvFile.setAdapter(fileAdapter);
        lvFile.setOnItemClickListener(new LvItemClick());

        refreshFile();
    }


    /**
     * 刷新文件列表
     */
    private void refreshFile() {
        if (fileList != null) {
            fileList.clear();
        }

        File files[] = currentFile.listFiles();
        for (File file : files) {
            fileList.add(new FileInfo(file));
        }

        fileAdapter.notifyDataSetChanged();
    }

    /**
     * 返回上級(jí)文件
     */
    private void goBack() {
        File parent = currentFile.getParentFile();
        if (parent == null || (currentFile.getAbsolutePath().equals(Environment.getExternalStorageDirectory().getAbsolutePath()))) {
            InputFileActivity.this.finish();
        } else {
            if (currentFile.isFile()) {
                currentFile = parent.getParentFile();
            } else {
                currentFile = parent;
            }
            refreshFile();
        }
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            goBack();
            return true;
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }


    /**
     * listview的item單擊事件響應(yīng)
     */
    private class LvItemClick implements AdapterView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            FileInfo fileInfo = (FileInfo) view.getTag();
            if (fileInfo.getFile().isFile()) {
                if (fileInfo.isCheck()) {
                    fileInfo.setCheck(false);
                } else {
                    for (FileInfo info : fileList) {
                        info.setCheck(false);
                    }
                    fileInfo.setCheck(true);
                }
                fileAdapter.notifyDataSetChanged();

            } else {
                currentFile = fileInfo.getFile();
                refreshFile();
            }
        }
    }


}

最后結(jié)果如下:

Paste_Image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末产弹,一起剝皮案震驚了整個(gè)濱河市派歌,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖胶果,帶你破解...
    沈念sama閱讀 217,509評(píng)論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件匾嘱,死亡現(xiàn)場離奇詭異,居然都是意外死亡早抠,警方通過查閱死者的電腦和手機(jī)霎烙,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,806評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來贝或,“玉大人吼过,你說我怎么就攤上這事∵浣保” “怎么了盗忱?”我有些...
    開封第一講書人閱讀 163,875評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長羊赵。 經(jīng)常有香客問我趟佃,道長,這世上最難降的妖魔是什么昧捷? 我笑而不...
    開封第一講書人閱讀 58,441評(píng)論 1 293
  • 正文 為了忘掉前任闲昭,我火速辦了婚禮,結(jié)果婚禮上靡挥,老公的妹妹穿的比我還像新娘序矩。我一直安慰自己,他們只是感情好跋破,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,488評(píng)論 6 392
  • 文/花漫 我一把揭開白布簸淀。 她就那樣靜靜地躺著,像睡著了一般毒返。 火紅的嫁衣襯著肌膚如雪租幕。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,365評(píng)論 1 302
  • 那天拧簸,我揣著相機(jī)與錄音劲绪,去河邊找鬼。 笑死盆赤,一個(gè)胖子當(dāng)著我的面吹牛贾富,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播牺六,決...
    沈念sama閱讀 40,190評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼祷安,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了兔乞?” 一聲冷哼從身側(cè)響起汇鞭,我...
    開封第一講書人閱讀 39,062評(píng)論 0 276
  • 序言:老撾萬榮一對情侶失蹤凉唐,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后霍骄,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體台囱,經(jīng)...
    沈念sama閱讀 45,500評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,706評(píng)論 3 335
  • 正文 我和宋清朗相戀三年读整,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了簿训。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,834評(píng)論 1 347
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡米间,死狀恐怖强品,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情屈糊,我是刑警寧澤的榛,帶...
    沈念sama閱讀 35,559評(píng)論 5 345
  • 正文 年R本政府宣布,位于F島的核電站逻锐,受9級(jí)特大地震影響夫晌,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜昧诱,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,167評(píng)論 3 328
  • 文/蒙蒙 一晓淀、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧盏档,春花似錦凶掰、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,779評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至勺拣,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間鱼填,已是汗流浹背药有。 一陣腳步聲響...
    開封第一講書人閱讀 32,912評(píng)論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留苹丸,地道東北人愤惰。 一個(gè)月前我還...
    沈念sama閱讀 47,958評(píng)論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像赘理,于是被迫代替她去往敵國和親宦言。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,779評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容