如何在同一个recyclerView中显示两个不同的arraylists的项目?

时间:2017-12-31 13:59:17

标签: android android-fragments android-recyclerview android-music-player

我正在建立一个音频播放器并且有一个片段"我的文件"在哪个电话的内部存储器中查询包含音频文件并显示的所有文件夹。我所做的是在"我的文件"片段我显示文件夹(在recyclerView中)。在单击此recyclerView的任何项目(文件夹)时,将启动另一个活动(MyFilesSong.java),该活动显示已单击文件夹的音频文件。

我想要什么 - 我想要片段"我的文件"显示文件夹(就像它已经做的那样),当我点击任何项目(文件夹)时,音频文件应该加载到同一个片段中(我不想要另一个活动)。我希望我能说清楚。

PS:使用了两个recyclerviews,它们都有不同类型的arraylist-" MyFiles"使用foldermodel而#34; Songs.java"使用songinfomodel

在第二个gif(我的)中点击该项目会打开一个新活动。但我想要在第一个实现的内容。该片段显示文件夹,并在单击文件夹时显示音频文件。

MyFiles.java:

 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.my_files_activity, container, false);



    recyclerView= view.findViewById(R.id.recyclerView_files);
    pathTextView= view.findViewById(R.id.myFilesPath);

    myList = new ArrayList<Object>();

    linearLayoutManager=new LinearLayoutManager(getContext());
    recyclerView.setLayoutManager(linearLayoutManager);






    Songpath = SongPath();

    for(int i = 0; i<Songpath.size();i++) {

        Log.e("Paths: ", Songpath.get(i));

        file = new File(  Songpath.get(i) ) ;

        try {
            sforFolders = new FolderModel(file.getParentFile().getName(),file.getParentFile().getCanonicalPath());
        } catch (IOException e) {
            e.printStackTrace();
        }
        myList.add(sforFolders);



    }

    myFilesAdapter = new MyFilesAdapter(getContext(), new MyFilesAdapter.MyFilesItemClickListener() {
        @Override
        public void folderonclicklistener(FolderModel name, int position) {

            Toast.makeText(getContext(), "Folder Name: "+name.getFolderName()+" Position: "+position, Toast.LENGTH_SHORT).show();

            Intent i = new Intent(getContext(),MyFilesSongs.class);
            i.putExtra("parentPath",name.getFolderPath());
            startActivity(i);


        }
    });

    recyclerView.setAdapter(myFilesAdapter);
    myFilesAdapter.getFilesFolders(myList);
    myFilesAdapter.notifyDataSetChanged();







    return view;
   }

MyFilesAdapter.java:

   public class MyFilesAdapterFinal extends RecyclerView.Adapter<MyFilesAdapterFinal.ViewHolder> {

ArrayList<FolderModel> SongParentPath = new ArrayList<>();
Context context;
itemClickListener listener;

public MyFilesAdapterFinal(ArrayList<FolderModel> songParentPath, Context context,itemClickListener listener) {
    SongParentPath = songParentPath;
    this.context = context;
    this.listener = listener;
}

@Override
public MyFilesAdapterFinal.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View v = LayoutInflater.from(context).inflate(R.layout.row_myfiles,parent,false);

    return new ViewHolder(v);
}

@Override
public void onBindViewHolder(MyFilesAdapterFinal.ViewHolder holder, int position) {

    FolderModel name = SongParentPath.get(position);

    holder.rowtext.setText(String.valueOf(name.getFolderName()));
    holder.bind(name,listener);





}

@Override
public int getItemCount() {
    return (SongParentPath==null)? 0 : SongParentPath.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {

    TextView rowtext;

    public ViewHolder(View itemView) {
        super(itemView);

        rowtext = itemView.findViewById(R.id.rowtext);
    }

    public void bind(final FolderModel name, final itemClickListener listener) {

       itemView.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               listener.onClick(name,getLayoutPosition());
           }
       });
    }
}

public interface itemClickListener{

    void onClick(FolderModel name, int position);
}

Songs.java :(列出音频文件的活动):

 protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    LayoutInflater inflater = LayoutInflater.from(this);
    View view6 = inflater.inflate(R.layout.songs_activity, null);
    FrameLayout container6 = (FrameLayout) findViewById(R.id.container);
    container6.addView(view6);



    recyclerView = findViewById(R.id.recyclerView);
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext());
    recyclerView.setLayoutManager(linearLayoutManager);

    String parentPath = getIntent().getExtras().getString("parentPath");

    SongList = displayFolderFiles(parentPath);



    songAdapter = new SongAdapter(getApplicationContext(), SongList, new SongAdapter.RecyclerItemClickListener() {
        @Override
        public void onClickListener(SongInfoModel song, int position) {




            Toast.makeText(getApplicationContext(), song.getSongName(), Toast.LENGTH_SHORT).show();


            BaseActivity.setsongText(song);
            BaseActivity.ButtonPause();
       //     playAudio(position);
            BaseActivity.slidingUpPanelCollapsed();





        }

        @Override
        public void onLongClickListener(final SongInfoModel song, final int position, View v) {



          //  AddToPlayListDialog(song, v);


        }


    });


    recyclerView.setAdapter(songAdapter);

}

SongsAdapter.java:

       public class SongAdapter extends RecyclerView.Adapter<SongAdapter.SongHolder> {

ArrayList<SongInfoModel> SongList = new ArrayList<>();
Context context;
private RecyclerItemClickListener listener;








public SongAdapter(Context context, ArrayList<SongInfoModel> SongList, RecyclerItemClickListener listener) {

    this.context = context;
    this.SongList = SongList;
    this.listener = listener;

}




@Override
public SongAdapter.SongHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(context).inflate(R.layout.row_song, parent, false);
    return new SongHolder(view);
}

@Override
public void onBindViewHolder(final SongAdapter.SongHolder holder, final int position) {



         final SongInfoModel songInfoModel = SongList.get(position);


         holder.songName.setText(songInfoModel.SongName);
         holder.artistName.setText(songInfoModel.ArtistName);
         holder.duration.setText(String.valueOf(songInfoModel.duration));
         String duration = Utility.convertDuration(songInfoModel.getDuration());
         holder.duration.setText(duration);
         Picasso.with(context).load(songInfoModel.getAlbumIDArtwork()).placeholder(R.drawable.ic_launcher).into(holder.iv_artwork);
         holder.bind(songInfoModel, listener);






}

@Override
public int getItemCount() {
    return SongList.size();
}

public class SongHolder extends RecyclerView.ViewHolder  {
    TextView songName;
    TextView artistName;
    TextView duration;
    private ImageView iv_artwork;



    public SongHolder(View itemView)    {

        super(itemView);
        songName = (TextView)itemView.findViewById(R.id.SongName);
        artistName= (TextView)itemView.findViewById(R.id.ArtistName);
        duration = (TextView) itemView.findViewById(R.id.duration);
        iv_artwork = (ImageView) itemView.findViewById(R.id.iv_artwork);


    }

    public void bind(final SongInfoModel songInfoModel, final RecyclerItemClickListener listener) {
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onClickListener(songInfoModel, getLayoutPosition());

            }
        });

        itemView.setOnLongClickListener(new View.OnLongClickListener() {

            @Override
            public boolean onLongClick(View view) {

                listener.onLongClickListener(songInfoModel, getLayoutPosition(),view);
                return true;
            }
        });

    }

}

public interface RecyclerItemClickListener{

    void onClickListener(SongInfoModel songInfoModel, int position);
    void onLongClickListener(SongInfoModel songInfoModel, int position, View view);

}

2 个答案:

答案 0 :(得分:0)

我认为您可能希望使用Expandable Recycler View作为解决方案。这是一个链接

Expandable RecyclerView

如果您对可扩展回收站视图的实施有任何问题,请不要犹豫,我会帮忙 我希望这会有所帮助;)

答案 1 :(得分:0)

首先,在托管两个recyclerviews的活动中,您应该添加以下内容:

<FrameLayout android:id="@+id/fragment_change" 
android:layout_width="match_parent" android:fitsSystemWindows="true" 
android:layout_height="match_parent"/>

由于你的两个片段都只是RecyclerViews,你可以将它作为主要容器,在活动中(确保它是一个AppCompatActivity),你可以这样称呼:

SupportFragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
FolderListFragment fragment = new FolderListFragment(); 
fragment.setArguments(bundle);
fragmentTransaction.replace(R.id.fragment_change, fragment);
fragmentTransaction.commit();

从foldersAdapter你应该使用它,而不是调用活动:

SupportFragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
FilesListFragment fragment = new FilesListFragment();
Bundle bundle = new Bundle();
bundle.putInt("parentPath",name.getFolderPath());
fragment.setArguments(bundle);
fragmentTransaction.replace(R.id.fragment_change, fragment);
fragmentTransaction.commit();

使用文件保存RecyclerView的活动必须扩展Fragment。

片段的OnCreate:

Bundle bundle = this.getArguments();
if (bundle != null) {
        int myInt = bundle.getString("parentPath", defaultValue);
}