Activity 中多个Fragment实现同一个接口,公共类中调用 此接口方法,调用的一直是最后一个Fragment中的.

黛曦葛溪 发布于 2017/12/20 15:50
阅读 166
收藏 0
       
//如下,activity 中有四个Fragment,Fragment中实现第二段代码接口
 mAllFileFragment = (FileViewFragment) fragmentManager.findFragmentByTag(ALL_TAG);
        mPictureViewFragment = (FileCategoryFragment) fragmentManager.findFragmentByTag(PICTURE_TAG);
        mAudioViewFragment = (FileCategoryFragment) fragmentManager.findFragmentByTag(AUDIO_TAG);
        mAudioViewFragment = (FileCategoryFragment)fragmentManager.findFragmentByTag(VIDEO_TAG);

        if (mAllFileFragment == null) {
            mAllFileFragment = new FileViewFragment();
            mPictureViewFragment = new FileCategoryFragment();
            mAudioViewFragment = new FileCategoryFragment();
            mVideoViewFragment = new FileCategoryFragment();
            mPictureViewFragment.setCurrentCategory(TabState.PICTURE);
            mAudioViewFragment.setCurrentCategory(TabState.AUDIO);
            mVideoViewFragment.setCurrentCategory(TabState.VIDEO);

            transaction.add(R.id.pager, mAllFileFragment, ALL_TAG);
            transaction.add(R.id.pager, mPictureViewFragment, PICTURE_TAG);
            transaction.add(R.id.pager, mAudioViewFragment, AUDIO_TAG);
            transaction.add(R.id.pager, mVideoViewFragment, VIDEO_TAG);
        }

        // Hide all fragments for now.  We adjust visibility when we get onSelectedTabChanged()
        // from ActionBarAdapter.
        transaction.hide(mAllFileFragment);
        transaction.hide(mPictureViewFragment);
        transaction.hide(mAudioViewFragment);
        transaction.hide(mVideoViewFragment);
public interface IFileInteractionListener {

    public List<FileInfo> getAllFiles();

}

Fragment 中通过 以下方法创建FileViewUtil 的实例.  this 就是  Fragment 实现的接口,通过次方发将 接口传递给FileViewUtil.  问题是:  当我处于第一个Fragment (mAllFileFragment)中的时候 mFileViewListener.onOperationSelectAll() 中

mFileViewListener.getAllFiles()

获取到的数据 是 最后一个Fragment (mVideoViewFragment)中的数据.

mFileViewutil = new FileViewUtil(this);

 


public class FileViewUtil{

    public FileViewUtil(IFileInteractionListener fileViewListener) {
        mFileViewListener = fileViewListener;
    }

    public void onOperationSelectAll() {
        mCheckedFileNameList.clear();
            Log.i("ccc","onOperationSelectAll=======getAllFiles=====>"+mFileViewListener.getAllFiles());
        for (FileInfo f : mFileViewListener.getAllFiles()) {
             Log.i("ccc","onOperationSelectAll=======info=====>"+f.fileName);
            f.Selected = true;
            mCheckedFileNameList.add(f);
        }
        int totleFileCount = mFileViewListener.getAllFiles().size();
        mActionBarAdapter.setSelectionCount(mCheckedFileNameList.size(),totleFileCount);
        mFileViewListener.onDataChanged();
    }
}

 

加载中
返回顶部
顶部