FileListFragment.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright (C) 2013 Paul Burke
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.ipaulpro.afilechooser;
  17. import android.app.Activity;
  18. import android.os.Bundle;
  19. import android.os.Environment;
  20. import android.support.v4.app.ListFragment;
  21. import android.support.v4.app.LoaderManager;
  22. import android.support.v4.content.Loader;
  23. import android.view.View;
  24. import android.widget.ListView;
  25. import com.xzjmyk.pm.activity.R;
  26. import java.io.File;
  27. import java.util.List;
  28. /**
  29. *
  30. * @项目名称: SkWeiChat-Baidu
  31. * @包名: com.ipaulpro.afilechooser
  32. * @作者:王阳
  33. * @创建时间: 2015年10月12日 上午9:26:50
  34. * @描述: TODO
  35. * @SVN版本号: $Rev: 2143 $
  36. * @修改人: $Author: luorc $
  37. * @修改时间: $Date: 2015-10-23 09:31:46 +0800 (Fri, 23 Oct 2015) $
  38. * @修改的内容: Fragment that displays a list of Files in a given path.
  39. */
  40. public class FileListFragment extends ListFragment implements
  41. LoaderManager.LoaderCallbacks<List<File>> {
  42. /**
  43. * Interface to listen for events.
  44. */
  45. public interface Callbacks {
  46. /**
  47. * Called when a file is selected from the list.
  48. *
  49. * @param file The file selected
  50. */
  51. public void onFileSelected(File file);
  52. }
  53. private static final int LOADER_ID = 0;
  54. private FileListAdapter mAdapter;
  55. private String mPath;
  56. private Callbacks mListener;
  57. /**
  58. * Create a new instance with the given file path.
  59. *
  60. * @param path The absolute path of the file (directory) to display.
  61. * @return A new Fragment with the given file path.
  62. */
  63. public static FileListFragment newInstance(String path) {
  64. FileListFragment fragment = new FileListFragment();
  65. Bundle args = new Bundle();
  66. args.putString(FileChooserActivity.PATH, path);
  67. fragment.setArguments(args);
  68. return fragment;
  69. }
  70. @Override
  71. public void onAttach(Activity activity) {
  72. super.onAttach(activity);
  73. try {
  74. mListener = (Callbacks) activity;
  75. } catch (ClassCastException e) {
  76. throw new ClassCastException(activity.toString()
  77. + " must implement FileListFragment.Callbacks");
  78. }
  79. }
  80. @Override
  81. public void onCreate(Bundle savedInstanceState) {
  82. super.onCreate(savedInstanceState);
  83. mAdapter = new FileListAdapter(getActivity());
  84. mPath = getArguments() != null ? getArguments().getString(
  85. FileChooserActivity.PATH) : Environment
  86. .getExternalStorageDirectory().getAbsolutePath();
  87. }
  88. @Override
  89. public void onActivityCreated(Bundle savedInstanceState) {
  90. setEmptyText(getString(R.string.empty_directory));
  91. setListAdapter(mAdapter);
  92. setListShown(false);
  93. getLoaderManager().initLoader(LOADER_ID, null, this);
  94. super.onActivityCreated(savedInstanceState);
  95. }
  96. @Override
  97. public void onListItemClick(ListView l, View v, int position, long id) {
  98. FileListAdapter adapter = (FileListAdapter) l.getAdapter();
  99. if (adapter != null) {
  100. File file = (File) adapter.getItem(position);
  101. mPath = file.getAbsolutePath();
  102. mListener.onFileSelected(file);
  103. }
  104. }
  105. @Override
  106. public Loader<List<File>> onCreateLoader(int id, Bundle args) {
  107. return new FileLoader(getActivity(), mPath);
  108. }
  109. @Override
  110. public void onLoadFinished(Loader<List<File>> loader, List<File> data) {
  111. mAdapter.setListItems(data);
  112. if (isResumed())
  113. setListShown(true);
  114. else
  115. setListShownNoAnimation(true);
  116. }
  117. @Override
  118. public void onLoaderReset(Loader<List<File>> loader) {
  119. mAdapter.clear();
  120. }
  121. }