从适配器中单击时,如何在 RecyclerView 中获取选定位置?
How do I get the selected position in a RecyclerView when clicked from from an adapter?
我正在开发一个应用程序,我希望它滚动到我从一个适配器单击到另一个的特定位置 activity 就像 Instagram 检查配置文件片段中的较小图片并滚动到在另一个 activity 中的确切位置,即从小图到大图。
我的适配器代码
public class DisplayDuetAdapter extends RecyclerView.Adapter<DisplayDuetAdapter.ImageViewHolder> {
private Context mContext;
private List<DisplayDuet> mDisplayDuet;
public int position;
public int getPosition() {
return position;
}
public void setPosition(int position) {
this.position = position;
}
public DisplayDuetAdapter(Context context, List<DisplayDuet> displayDuet){
mContext = context;
mDisplayDuet = displayDuet;
}
@NonNull
@Override
public DisplayDuetAdapter.ImageViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(mContext).inflate(R.layout.duet_display_item, viewGroup, false);
return new DisplayDuetAdapter.ImageViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final DisplayDuetAdapter.ImageViewHolder imageViewHolder, int i) {
final DisplayDuet displayDuet = mDisplayDuet.get(i);
// Glide.with(mContext).load(displayDuet.getOriginalpics()).into(imageViewHolder.original_pic);
Picasso.get().load(displayDuet.getOriginalpics()).networkPolicy(NetworkPolicy.OFFLINE).placeholder(R.drawable.placeholder).into(imageViewHolder.original_pic, new Callback() {
@Override
public void onSuccess() {
}
@Override
public void onError(Exception e) {
Picasso.get().load(displayDuet.getOriginalpics()).placeholder(R.drawable.placeholder).into(imageViewHolder.original_pic);
}
});
// Glide.with(mContext).load(displayDuet.getPostimage()).into(imageViewHolder.post_pic);
Picasso.get().load(displayDuet.getPostimage()).networkPolicy(NetworkPolicy.OFFLINE).placeholder(R.drawable.placeholder).into(imageViewHolder.post_pic, new Callback() {
@Override
public void onSuccess() {
}
@Override
public void onError(Exception e) {
Picasso.get().load(displayDuet.getPostimage()).placeholder(R.drawable.placeholder).into(imageViewHolder.post_pic);
}
});
imageViewHolder.setiItemClickListener(new IItemClickListener() {
@Override
public void onItemClickListener(View view, int position) {
Intent intent = new Intent(mContext, DuetDisplayActivity.class);
intent.putExtra("postid", displayDuet.getPostid());
intent.putExtra("publisherid", displayDuet.getPublisher());
intent.putExtra("postduetid", displayDuet.getPostduetid());
mContext.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return mDisplayDuet.size();
}
public class ImageViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public ImageView post_pic, original_pic;
IItemClickListener iItemClickListener;
public void setiItemClickListener(IItemClickListener iItemClickListener) {
this.iItemClickListener = iItemClickListener;
}
public ImageViewHolder(@NonNull View itemView) {
super(itemView);
post_pic = itemView.findViewById(R.id.duet_pic_one);
original_pic = itemView.findViewById(R.id.duet_pic_two);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
iItemClickListener.onItemClickListener(view, getAdapterPosition());
}
}
}
另一个activity代码:
public class DuetDisplayActivity extends AppCompatActivity {
private String postid, publisher, postduetid;
private ImageView duet_close;
int position = 2;
private RecyclerView duet_show_recyclerView;
private DuetShowAdapter postAdapter;
private List<DisplayDuet> postList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_duet_display);
Intent intent = getIntent();
postid = intent.getStringExtra("postid");
publisher = intent.getStringExtra("publisherid");
postduetid = intent.getStringExtra("postduetid");
duet_close = findViewById(R.id.close);
duet_show_recyclerView = findViewById(R.id.duet_show_recyclerView);
duet_show_recyclerView.setHasFixedSize(true);
LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
duet_show_recyclerView.setLayoutManager(mLayoutManager);
postList = new ArrayList<>();
postAdapter = new DuetShowAdapter(this, postList);
duet_show_recyclerView.findViewHolderForAdapterPosition(position);
duet_show_recyclerView.setAdapter(postAdapter);
SnapHelper startSnapHelper = new PagerSnapHelper();
duet_show_recyclerView.setOnFlingListener(null);
startSnapHelper.attachToRecyclerView(duet_show_recyclerView);
duet_close.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
readPost();
}
private void readPost(){
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("DuetPics").child(postid);
reference.keepSynced(true);
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
postList.clear();
for (DataSnapshot getsnapshot : dataSnapshot.getChildren()) {
for (DataSnapshot snapshot : getsnapshot.getChildren()) {
DisplayDuet displayDuet = snapshot.getValue(DisplayDuet.class);
postList.add(displayDuet);
}
Collections.reverse(postList);
postAdapter.notifyDataSetChanged();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
我从你的问题中了解到,你想将点击位置从 DisplayDuetAdapter 传递到你的 DuetDisplayActivity。
如果是这种情况,那么您可以按意图通过它。
所以像这样在 DisplayDuetAdapter 中传递按下的项目位置:
imageViewHolder.setiItemClickListener(new IItemClickListener() {
@Override
public void onItemClickListener(View view, int position) {
Intent intent = new Intent(mContext, DuetDisplayActivity.class);
//pass position like this
intent.putExtra("position", i);
intent.putExtra("postid", displayDuet.getPostid());
intent.putExtra("publisherid", displayDuet.getPublisher());
intent.putExtra("postduetid", displayDuet.getPostduetid());
mContext.startActivity(intent);
}
});
并像这样获取 DuetDisplayActivity 中的位置:
private int position;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_duet_display);
Intent intent = getIntent();
postid = intent.getStringExtra("postid");
publisher = intent.getStringExtra("publisherid");
postduetid = intent.getStringExtra("postduetid");
//get the passed position
position = intent.getIntExtra("position");
............
............
更新:
这样做:
//get the passed position
position = intent.getIntExtra("position",0);
我正在开发一个应用程序,我希望它滚动到我从一个适配器单击到另一个的特定位置 activity 就像 Instagram 检查配置文件片段中的较小图片并滚动到在另一个 activity 中的确切位置,即从小图到大图。
我的适配器代码
public class DisplayDuetAdapter extends RecyclerView.Adapter<DisplayDuetAdapter.ImageViewHolder> {
private Context mContext;
private List<DisplayDuet> mDisplayDuet;
public int position;
public int getPosition() {
return position;
}
public void setPosition(int position) {
this.position = position;
}
public DisplayDuetAdapter(Context context, List<DisplayDuet> displayDuet){
mContext = context;
mDisplayDuet = displayDuet;
}
@NonNull
@Override
public DisplayDuetAdapter.ImageViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(mContext).inflate(R.layout.duet_display_item, viewGroup, false);
return new DisplayDuetAdapter.ImageViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final DisplayDuetAdapter.ImageViewHolder imageViewHolder, int i) {
final DisplayDuet displayDuet = mDisplayDuet.get(i);
// Glide.with(mContext).load(displayDuet.getOriginalpics()).into(imageViewHolder.original_pic);
Picasso.get().load(displayDuet.getOriginalpics()).networkPolicy(NetworkPolicy.OFFLINE).placeholder(R.drawable.placeholder).into(imageViewHolder.original_pic, new Callback() {
@Override
public void onSuccess() {
}
@Override
public void onError(Exception e) {
Picasso.get().load(displayDuet.getOriginalpics()).placeholder(R.drawable.placeholder).into(imageViewHolder.original_pic);
}
});
// Glide.with(mContext).load(displayDuet.getPostimage()).into(imageViewHolder.post_pic);
Picasso.get().load(displayDuet.getPostimage()).networkPolicy(NetworkPolicy.OFFLINE).placeholder(R.drawable.placeholder).into(imageViewHolder.post_pic, new Callback() {
@Override
public void onSuccess() {
}
@Override
public void onError(Exception e) {
Picasso.get().load(displayDuet.getPostimage()).placeholder(R.drawable.placeholder).into(imageViewHolder.post_pic);
}
});
imageViewHolder.setiItemClickListener(new IItemClickListener() {
@Override
public void onItemClickListener(View view, int position) {
Intent intent = new Intent(mContext, DuetDisplayActivity.class);
intent.putExtra("postid", displayDuet.getPostid());
intent.putExtra("publisherid", displayDuet.getPublisher());
intent.putExtra("postduetid", displayDuet.getPostduetid());
mContext.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return mDisplayDuet.size();
}
public class ImageViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public ImageView post_pic, original_pic;
IItemClickListener iItemClickListener;
public void setiItemClickListener(IItemClickListener iItemClickListener) {
this.iItemClickListener = iItemClickListener;
}
public ImageViewHolder(@NonNull View itemView) {
super(itemView);
post_pic = itemView.findViewById(R.id.duet_pic_one);
original_pic = itemView.findViewById(R.id.duet_pic_two);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
iItemClickListener.onItemClickListener(view, getAdapterPosition());
}
}
}
另一个activity代码:
public class DuetDisplayActivity extends AppCompatActivity {
private String postid, publisher, postduetid;
private ImageView duet_close;
int position = 2;
private RecyclerView duet_show_recyclerView;
private DuetShowAdapter postAdapter;
private List<DisplayDuet> postList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_duet_display);
Intent intent = getIntent();
postid = intent.getStringExtra("postid");
publisher = intent.getStringExtra("publisherid");
postduetid = intent.getStringExtra("postduetid");
duet_close = findViewById(R.id.close);
duet_show_recyclerView = findViewById(R.id.duet_show_recyclerView);
duet_show_recyclerView.setHasFixedSize(true);
LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
duet_show_recyclerView.setLayoutManager(mLayoutManager);
postList = new ArrayList<>();
postAdapter = new DuetShowAdapter(this, postList);
duet_show_recyclerView.findViewHolderForAdapterPosition(position);
duet_show_recyclerView.setAdapter(postAdapter);
SnapHelper startSnapHelper = new PagerSnapHelper();
duet_show_recyclerView.setOnFlingListener(null);
startSnapHelper.attachToRecyclerView(duet_show_recyclerView);
duet_close.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
readPost();
}
private void readPost(){
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("DuetPics").child(postid);
reference.keepSynced(true);
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
postList.clear();
for (DataSnapshot getsnapshot : dataSnapshot.getChildren()) {
for (DataSnapshot snapshot : getsnapshot.getChildren()) {
DisplayDuet displayDuet = snapshot.getValue(DisplayDuet.class);
postList.add(displayDuet);
}
Collections.reverse(postList);
postAdapter.notifyDataSetChanged();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
我从你的问题中了解到,你想将点击位置从 DisplayDuetAdapter 传递到你的 DuetDisplayActivity。
如果是这种情况,那么您可以按意图通过它。
所以像这样在 DisplayDuetAdapter 中传递按下的项目位置:
imageViewHolder.setiItemClickListener(new IItemClickListener() {
@Override
public void onItemClickListener(View view, int position) {
Intent intent = new Intent(mContext, DuetDisplayActivity.class);
//pass position like this
intent.putExtra("position", i);
intent.putExtra("postid", displayDuet.getPostid());
intent.putExtra("publisherid", displayDuet.getPublisher());
intent.putExtra("postduetid", displayDuet.getPostduetid());
mContext.startActivity(intent);
}
});
并像这样获取 DuetDisplayActivity 中的位置:
private int position;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_duet_display);
Intent intent = getIntent();
postid = intent.getStringExtra("postid");
publisher = intent.getStringExtra("publisherid");
postduetid = intent.getStringExtra("postduetid");
//get the passed position
position = intent.getIntExtra("position");
............
............
更新:
这样做:
//get the passed position
position = intent.getIntExtra("position",0);