如何在同一个 recyclerView 中显示两个不同数组列表的项目?

How to display items of two different arraylists in same recyclerView?

我正在构建一个音频播放器,并且有一个片段 "My Files",其中查询 phone 的内部存储以查找包含音频文件并显示的所有文件夹。我所做的是在 "My Files" 片段中显示文件夹(在 recyclerView 中)。单击此 recyclerView 的任何项目(文件夹)时,另一个 activity 启动(MyFilesSong.java),显示单击文件夹的音频文件。

我想要的 - 我希望片段 "My Files" 显示文件夹(因为它已经显示)并且当我单击任何项​​目(文件夹)时,音频文件应该加载到同一个片段中(我不想要另一个 activity)。我希望我已经清楚了。

PS:使用了两个recyclerview,它们都有不同类型的arraylist——"MyFiles"使用foldermodel而"Songs.java"使用songinfomodel

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

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:(activity 列出音频文件):

 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);

}

我认为您可能希望使用可扩展回收器视图作为解决方案。这是一个link

Expandable RecyclerView

如果您对 Expandable Recycler View 的实施有任何疑问,请随时询问我会提供帮助 希望对您有所帮助 ;)

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

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

由于您的两个片段都只是 RecyclerViews,因此您将其作为主要容器,在 activity(确保这是一个 AppCompatActivity)中,您称之为:

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

你应该从文件夹适配器中使用它,而不是调用 activity:

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 和文件的 activity 必须扩展 Fragment。

在片段的 OnCreate 上:

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