我在垂直再循环视图中有水平循环视图。
我在水平循环浏览中添加了列表 cardview 。
点击主要活动上的项目时如何获取项目信息?
谢谢
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private AlbumsVerticalAdapter adapter;
private List<Album> albumList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
initCollapsingToolbar();
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
albumList = new ArrayList<>();
adapter = new AlbumsVerticalAdapter(this, albumList);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
recyclerView.setAdapter(adapter);
}
private void initCollapsingToolbar() {}
private void prepareAlbums() {}
}
public class AlbumsVerticalAdapter extends RecyclerView.Adapter<AlbumsVerticalAdapter.MyViewHolder> {
private Context mContext;
private List<Album> albumList;
//.............
@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
Album album = albumList.get(position);
holder.title.setText(album.getName());
AlbumsHorizontalAdapter itemListDataAdapter = new AlbumsHorizontalAdapter(mContext, albumList);
holder.recycler_view_list.setHasFixedSize(true);
holder.recycler_view_list.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
holder.recycler_view_list.setAdapter(itemListDataAdapter);
}
@Override
public int getItemCount() {
return albumList.size();
}
}
答案 0 :(得分:7)
最好在OnClickListener
课程上实施ViewHolder
,并将听众添加到您想要的观看次数。
只需在onBindViewHolder(...)
添加setOnClickListener(...)
,就像这样:
@Override
public void onBindViewHolder(WordViewHolder holder, final int position) {
//...
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), position + "", Toast.LENGTH_SHORT).show();
}
});
}
答案 1 :(得分:1)
查看您的代码,VerticalRecyclerViewAdapter有一个List<Albums>
,此列表中的每个相册都需要一个HorizontalRecyclerViewAdapter才能容纳另一个List<Albums>
。
在您的VerticalRecylerViewAdapter中创建VerticalClickListener
,这将由您的活动实施。在HorizontalRecyclerViewAdapter里面创建HorizontalClickListener
,它将由您的VerticalRecyclerViewAdapter为每个相册实现。为每个相册创建HorizontalRecyclerViewAdapter时,也要传递垂直位置。然后,当单击cardview时,HorizontalClickListener
可以返回适配器的垂直位置以及cardview的水平位置。 VerticalRecyclerViewAdapter将返回您活动的垂直和水平位置。
答案 2 :(得分:0)
首先,您创建一个类似的界面:
public interface OnItemClickListener{
void onItemClick(View view, int position)
}
在全球宣布:
private OnItemClickListener mOnItemClickListener;
接下来,您需要一个像:
这样的构造函数public MyAdapter(Context context, List data, OnItemClickListener mOnItemClickListener){
//...
this.mOnItemClickListener= mOnItemClickListener;
}
在onBindViewHolder
@Override
public void onBindViewHolder(final FolderViewHolder holder, final int position) {
//....
holder.image.setOnClickListener(new View.OnItemClickListener){
@Override
public void onClick(View v) {
mOnItemClickListener.onItemClick(v,position);
}
});
}
最后,您可以在类使用适配器中实现Callback,如:
public class A extends AppCombatActivity implements MyAdapter.OnItemClickListener{
//exist code
@Override
onItemClick(View view, int position){
// Do something with view + position
}
}
答案 3 :(得分:0)
/Just try to implement the below code Snippet/
First make an Interface Class "RecyclerView_ItemClickListener"
public interface RecyclerView_ItemClickListener
{
void onClick(View view, int position);
void onItemClick(View view, int position);
}
Then use this class in your Activity/Fragment and inside Adapter.
How to Implement in Adapter class:
private RecyclerView_ItemClickListener clickListener;
public void setClickListener(RecyclerView_ItemClickListener itemClickListener)
{
this.clickListener = itemClickListener;
}
public void SetOnItemClickListener(final RecyclerView_ItemClickListener mItemClickListener)
{
this.clickListener = mItemClickListener;
}
Implement implements View.OnClickListener inside MyViewHolder Method
of Adapter
Example like this: class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
itemView.setOnClickListener(this);
Then put the below code
@Override
public void onClick(View view)
{
if (clickListener != null)
{
clickListener.onClick(view, getAdapterPosition());
clickListener.onItemClick(view,getAdapterPosition());
}
}
After that implements RecyclerView_ItemClickListener inside your
Activity/Fragment
in OnCreate:
your_Adapter.setClickListener(this);
your_Adapter.SetOnItemClickListener(this);
@Override
public void onClick(View view, int position)
{}
@Override
public void onItemClick(View view, int position)
{
}
答案 4 :(得分:0)
创建一个接口
public interface OnItemClickListener{
void onItemClick(View view, int position)
}
在适配器中添加参数化构造器 AlbumsVerticalAdapter
private OnItemClickListener mOnviewItemClickListener;
public AlbumsVerticalAdapter(Context context, List data, OnItemClickListener mOItemClickListener){
//...
this.mOnItemClickListener= mOnItemClickListener;
现在在适配器类
内的OnBinderViewHolder方法中添加此行@Override
public void onBindViewHolder(final FolderViewHolder holder, final int position) {
//....
mholder.mimageview.setOnClickListener(new View.OnItemClickListener){
@Override
public void onClick(View v) {
mOnItemClickListener.onItemClick(v,position);
}
});
}
在您的Activity中实现此界面并使用项目的onClick
public class MainActivity extends AppCombatActivity implements AlbumsVerticalAdapter.OnItemClickListener{
@Override
onItemClick(View view, int position){
// write your code
}
}