我想用RecyclerView创建一个电影应用。这个程序主要包括3个活动。在第一个活动电影类别(例如:惊悚片,戏剧..)和第二个活动电影图像和电影名称(例如:海报或屏幕截图)上,第三个活动的电影详细信息。我计划在此应用程序中包含100部电影。我想知道如何通过在第一个活动上单击相同的电影类别来打开电影的第二个/海报活动页面,以及如何通过在第二个活动中单击相同的电影图像来打开详细信息活动。请帮助我...
这是我的第一项活动
RecyclerView mRecyclerView;
FirebaseDatabase mFirebaseDatabase;
DatabaseReference mRef;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_moviesmain);
//Actionbar
Toolbar toolbar = findViewById(R.id.MoviesMain_Toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Movie");
//set title
//RecyclerView
mRecyclerView = findViewById(R.id.Movies_recyclerView);
mRecyclerView.setHasFixedSize(true);
//set layout as LinearLayout
mRecyclerView.setLayoutManager(new LinearLayoutManager(this) );
//send Query to FirebaseDatabase
mFirebaseDatabase = FirebaseDatabase.getInstance();
mRef = mFirebaseDatabase.getReference("Movie");
}
//load data into recycler view onStart
@Override
protected void onStart() {
super.onStart();
FirebaseRecyclerAdapter<MoviesModel, MoviesViewHolder> firebaseRecyclerAdapter =
new FirebaseRecyclerAdapter<MoviesModel, MoviesViewHolder>(
MoviesModel.class,
R.layout.moviesmainitems,
MoviesViewHolder.class,
mRef
) {
@Override
protected void populateViewHolder(MoviesViewHolder viewHolder, MoviesModel model, int position) {
viewHolder.setDetails(getApplicationContext(), model.getTitle(), model.getImage());
}
};
//set adapter to recyclerview
mRecyclerView.setAdapter(firebaseRecyclerAdapter);
}
这是我的第一个模特
String image, title;
//constructor
public MoviesModel(){}
//getter and setters press Alt+Insert
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
这是我的第一个ViewHolder
View mView;
public MoviesViewHolder(View itemView) {
super(itemView);
mView = itemView;
//item click
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mClickListener.onItemClick(view, getAdapterPosition());
}
});
//item long click
itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
mClickListener.onItemLongClick(view, getAdapterPosition());
return true;
}
});
}
//set details to recycler view moviesmainitems
public void setDetails(Context ctx, String title, String image){
//Views
TextView mDetailTv = mView.findViewById(R.id.rTitleTv);
ImageView mImageIv = mView.findViewById(R.id.rImageView);
//set data to views
mDetailTv.setText(title);
Picasso.get().load(image).into(mImageIv);
}
private MoviesViewHolder.ClickListener mClickListener;
//interface to send callbacks
public interface ClickListener{
void onItemClick(View view, int position);
void onItemLongClick(View view, int position);
}
public void setOnClickListener(MoviesViewHolder.ClickListener clickListener){
mClickListener = clickListener;
}
这是我的第二项活动
RecyclerView mRecyclerView;
FirebaseDatabase mFirebaseDatabase;
DatabaseReference mRef;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_moviessecond);
//Actionbar
Toolbar toolbar = findViewById(R.id.MoviesSecond_Toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Movie");
//set title
//RecyclerView
mRecyclerView = findViewById(R.id.MoviesSecond_recyclerView);
mRecyclerView.setHasFixedSize(true);
//set layout as LinearLayout
mRecyclerView.setLayoutManager(new LinearLayoutManager(this) );
//send Query to FirebaseDatabase
mFirebaseDatabase = FirebaseDatabase.getInstance();
mRef = mFirebaseDatabase.getReference("Movie");
}
//load data into recycler view onStart
@Override
protected void onStart() {
super.onStart();
FirebaseRecyclerAdapter<MoviesSecondModel, MoviesSecondViewHolder> firebaseRecyclerAdapter =
new FirebaseRecyclerAdapter<MoviesSecondModel, MoviesSecondViewHolder>(
MoviesModel.class,
R.layout.moviesseconditems,
MoviesSecondViewHolder.class,
mRef
) {
@Override
protected void populateViewHolder(MoviesSecondViewHolder viewHolder, MoviesSecondModel model, int position) {
viewHolder.setDetails(getApplicationContext(), model.getTitle(), model.getImage());
}
};
//set adapter to recyclerview
mRecyclerView.setAdapter(firebaseRecyclerAdapter);
}
答案 0 :(得分:0)
如果您希望保持3个活动的结构,最好的方法是在Listener
中添加Adapter
,如下所示:
public class MoveiAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
private OnInteractionListener mListener;
public MoveiAdapter(OnInteractionListener listener) {
mListener = listener;
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
holder.itemView.setOnClickListener(v -> {
if (mListener != null) {
mListener.onFooterClick();
}
});
}
}
public interface OnInteractionListener {
void onItemClick(int position);
}
}
您Activity
将实现此接口OnInteractionListener
,并且在调用onItemClick()
时,您可以打开下一个Activity
仅是一个建议,您只能使用一个Activity
并使用Fragments