我的活动被调用,因此用户应该选择一些歌曲,他想要添加到播放列表。如何选择RecyclerView(背景颜色应该是强调色),如何从RecyclerView接收所选歌曲的列表或数组?
我的SelectSongsActivity:
public class SelectSongsActivity extends AppCompatActivity {
private Song[] sSongs;
private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
private SelectSongRecyclerViewAdapter adapter;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getIntent().getExtras();
sSongs = (Song[]) bundle.get(Constants.IntentExtra.SONGS);
Initialize();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_selectsongs, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.mi_Add) {
Intent i = getIntent();
i.putExtra(Constants.IntentExtra.SONGS, adapter.getSelectedSongs());
setResult(RESULT_OK, i);
finish();
}
return super.onOptionsItemSelected(item);
}
@Override
public void onBackPressed() {
setResult(RESULT_CANCELED);
super.onBackPressed();
}
@Override
public boolean onSupportNavigateUp() {
setResult(RESULT_CANCELED);
return super.onSupportNavigateUp();
}
private void Initialize() {
setContentView(R.layout.activity_selectsongs);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.addItemDecoration(new RecyclerViewDivider(this));
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
adapter = new SelectSongRecyclerViewAdapter(sSongs);
recyclerView.setAdapter(adapter);
}
}
我的RecyclerViewAdapter:
public class SelectSongRecyclerViewAdapter extends RecyclerView.Adapter<SelectSongRecyclerViewAdapter.Holder> {
private Song[] sSongs;
private List<Song> selectedSongs;
public SelectSongRecyclerViewAdapter(Song[] songs) {
sSongs = songs;
selectedSongs = new ArrayList<>();
}
@Override
public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_songview, parent, false);
Holder holder = new Holder(view);
return holder;
}
@Override
public void onBindViewHolder(Holder holder, int position) {
//holder.imvSong.setImageResource(R.drawable.standardartwork);
holder.txvSongTitle.setText(sSongs[position].getTitle());
holder.txvSongInfo.setText(sSongs[position].getArtists());
}
@Override
public int getItemCount() {
return sSongs != null ? sSongs.length : 0;
}
public Song[] getSelectedSongs() {
Song[] songs = new Song[selectedSongs.size()];
return selectedSongs.toArray(songs);
}
public class Holder extends RecyclerView.ViewHolder {
LinearLayout linearLayout;
ImageView imvSong;
TextView txvSongTitle;
TextView txvSongInfo;
public Holder(View layout) {
super(layout);
linearLayout = (LinearLayout) layout;
imvSong = (ImageView) layout.findViewById(R.id.imvSong);
txvSongTitle = (TextView) layout.findViewById(R.id.adap_txvSongtitle);
txvSongInfo = (TextView) layout.findViewById(R.id.txvSongInfo);
}
}
}
谢谢!
答案 0 :(得分:0)
你的意思是当你点击线性布局? 你可以做的是在onbindviewholder -
holder.linearlayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectedSongs.add(sSongs[position]);
}
});
以不同颜色显示已选择的歌曲 -
if (selectedSongs.contains(sSongs[position])) {
holder.linearlayout.setBackgroundColor(R.color.color_accent);
} else {
//else as viewholders are reused
holder.linearlayout.setBackgroundColor(R.color.default_color);
}