Как мне добиться этого в android студии. точно так же, как когда вы нажимаете на изображение в Instagram и оно прокручивается до того же изображения в другом упражнении.
введите описание изображения здесь
это мой код Первый адаптер:
publi c Класс DisplayDuetAdapter расширяет RecyclerView.Adapter {
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());
}
}
}
Второе действие:
publi c Класс DuetDisplayActivity расширяет 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.scrollToPosition(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) {
}
});
}
}
Второй адаптер для второго действия:
publi c Класс DuetShowAdapter расширяет RecyclerView.Adapter {
private Context mContext;
private List<DisplayDuet> mDisplayDuet;
private FirebaseUser firebaseUser;
public DuetShowAdapter(Context context, List<DisplayDuet> displayDuet) {
this.mContext = context;
mDisplayDuet = displayDuet;
}
@NonNull
@Override
public DuetShowAdapter.ImageViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
View threadItemLayoutView = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.duet_show_item, viewGroup, false);
return new DuetShowAdapter.ImageViewHolder(threadItemLayoutView);
}
@Override
public void onBindViewHolder(@NonNull final DuetShowAdapter.ImageViewHolder holder, int position) {
firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
final DisplayDuet postThread = mDisplayDuet.get(position);
if (postThread.getDescription() != null && postThread.getDescription().equals("")) {
holder.duet_show_description.setVisibility(View.GONE);
} else {
holder.duet_show_description.setVisibility(View.VISIBLE);
holder.duet_show_description.setText(postThread.getDescription());
}
}
@Override
public int getItemCount() {
return mDisplayDuet.size();
}
public class ImageViewHolder extends RecyclerView.ViewHolder {
..................................
public RecyclerView duet_show_recycler_view_worth;
public LinearLayout duet_show_l_worth;
public ImageViewHolder(@NonNull View itemView) {
super(itemView);
duet_show_image_profile = itemView.findViewById(R.id.duet_show_image_profile);
duet_show_username = itemView.findViewById(R.id.duet_show_username);
duet_show_recycler_view_worth = itemView.findViewById(R.id.duet_show_recycler_view_worth);
duet_show_like = itemView.findViewById(R.id.duet_show_like);
duet_show_first_frame = itemView.findViewById(R.id.duet_show_first_frame);
duet_show_second_frame = itemView.findViewById(R.id.duet_show_second_frame);
.................................
}
}
. ..........}