我有 RecyclerCards。单击卡片时,应打开一个新的 activity
I have RecyclerCards. When you click on the card, a new activity should open
回收器适配器。
这里我用CardView注册了RecyclerView的Adapter。主要问题是他最近才开始做移动开发,知识还不够。
class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder>{
private static final String TAG ="RecyclerView";
private Context mContext;
private ArrayList<Game> gamesList;
public RecyclerAdapter(Context mContext, ArrayList<Game> gamesList){
this.mContext=mContext;
this.gamesList=gamesList;
}
@NonNull
@Override
public RecyclerAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view=LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_games_items, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull RecyclerAdapter.ViewHolder holder, int position) {
holder.textView.setText(gamesList.get(position).getTitle());
holder.textView1.setText(gamesList.get(position).getTag());
holder.textView2.setText(gamesList.get(position).getPrice());
Glide.with(mContext).load(gamesList.get(position).getImage()).into(holder.imageView);
}
@Override
public int getItemCount() {
return gamesList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
ImageView imageView;
TextView textView, textView1, textView2;
public ViewHolder(@NonNull View itemView) {
super(itemView);
imageView=itemView.findViewById(R.id.game_img_id);
textView=itemView.findViewById(R.id.game_title_id);
textView1=itemView.findViewById(R.id.game_tags_id);
textView2=itemView.findViewById(R.id.game_price_id);
}
}
}
游戏。
应该单击并转换到包含游戏说明的新 activity。信息取自 FireBase 数据库。
public class Games extends AppCompatActivity implements View.OnClickListener{
RecyclerView recyclerView;
private DatabaseReference myRef;
private ArrayList<Game> gamesList;
private RecyclerAdapter recyclerAdapter;
private Context mContext;
@Override
protected void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_games);
ImageButton Home = (ImageButton) findViewById(R.id.home);
Home.setOnClickListener(this);
ImageButton Lupa = (ImageButton) findViewById(R.id.lupa);
Lupa.setOnClickListener(this);
ImageButton Calendar = (ImageButton) findViewById(R.id.calendar);
Calendar.setOnClickListener(this);
ImageButton Kubik = (ImageButton) findViewById(R.id.kubik);
Kubik.setOnClickListener(this);
ImageButton Profile = (ImageButton) findViewById(R.id.profile);
Profile.setOnClickListener(this);
recyclerView = findViewById(R.id.recyclerview_id);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(new GridLayoutManager(this,2));
recyclerView.setHasFixedSize(true);
myRef=FirebaseDatabase.getInstance().getReference();
gamesList= new ArrayList<>();
GetDataFromFirebase();
}
private void GetDataFromFirebase(){
Query query_adventures = myRef.child("Parsing/Adventures");
Query query_indi= myRef.child("Parsing/Indi");
Query query_casuals= myRef.child("Parsing/Casuals");
Query query_freegames= myRef.child("Parsing/Freegames");
Query query_mmos= myRef.child("Parsing/Mmos");
Query query_races= myRef.child("Parsing/Races");
Query query_RP = myRef.child("Parsing/RP");
Query query_simulator = myRef.child("Parsing/Simulator");
query_adventures.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()){
Game games = new Game();
games.setImage(snapshot.child("IMG").getValue().toString());
games.setTitle(snapshot.child("TITLE").getValue().toString());
games.setTag(snapshot.child("TAG").getValue().toString());
games.setPrice(snapshot.child("PRICE").getValue().toString());
gamesList.add(games);
}
recyclerAdapter = new RecyclerAdapter(getApplicationContext(), gamesList);
recyclerView.setAdapter(recyclerAdapter);
recyclerAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
query_indi.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()){
Game games = new Game();
games.setImage(snapshot.child("IMG").getValue().toString());
games.setTitle(snapshot.child("TITLE").getValue().toString());
games.setTag(snapshot.child("TAG").getValue().toString());
games.setPrice(snapshot.child("PRICE").getValue().toString());
gamesList.add(games);
}
recyclerAdapter = new RecyclerAdapter(getApplicationContext(), gamesList);
recyclerView.setAdapter(recyclerAdapter);
recyclerAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
query_casuals.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()){
Game games = new Game();
games.setImage(snapshot.child("IMG").getValue().toString());
games.setTitle(snapshot.child("TITLE").getValue().toString());
games.setTag(snapshot.child("TAG").getValue().toString());
games.setPrice(snapshot.child("PRICE").getValue().toString());
gamesList.add(games);
}
recyclerAdapter = new RecyclerAdapter(getApplicationContext(), gamesList);
recyclerView.setAdapter(recyclerAdapter);
recyclerAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
query_freegames.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()){
Game games = new Game();
games.setImage(snapshot.child("IMG").getValue().toString());
games.setTitle(snapshot.child("TITLE").getValue().toString());
games.setTag(snapshot.child("TAG").getValue().toString());
games.setPrice(snapshot.child("PRICE").getValue().toString());
gamesList.add(games);
}
recyclerAdapter = new RecyclerAdapter(getApplicationContext(), gamesList);
recyclerView.setAdapter(recyclerAdapter);
recyclerAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
query_mmos.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()){
Game games = new Game();
games.setImage(snapshot.child("IMG").getValue().toString());
games.setTitle(snapshot.child("TITLE").getValue().toString());
games.setTag(snapshot.child("TAG").getValue().toString());
games.setPrice(snapshot.child("PRICE").getValue().toString());
gamesList.add(games);
}
recyclerAdapter = new RecyclerAdapter(getApplicationContext(), gamesList);
recyclerView.setAdapter(recyclerAdapter);
recyclerAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
query_races.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()){
Game games = new Game();
games.setImage(snapshot.child("IMG").getValue().toString());
games.setTitle(snapshot.child("TITLE").getValue().toString());
games.setTag(snapshot.child("TAG").getValue().toString());
games.setPrice(snapshot.child("PRICE").getValue().toString());
gamesList.add(games);
}
recyclerAdapter = new RecyclerAdapter(getApplicationContext(), gamesList);
recyclerView.setAdapter(recyclerAdapter);
recyclerAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
query_RP.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()){
Game games = new Game();
games.setImage(snapshot.child("IMG").getValue().toString());
games.setTitle(snapshot.child("TITLE").getValue().toString());
games.setTag(snapshot.child("TAG").getValue().toString());
games.setPrice(snapshot.child("PRICE").getValue().toString());
gamesList.add(games);
}
recyclerAdapter = new RecyclerAdapter(getApplicationContext(), gamesList);
recyclerView.setAdapter(recyclerAdapter);
recyclerAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
query_simulator.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()){
Game games = new Game();
games.setImage(snapshot.child("IMG").getValue().toString());
games.setTitle(snapshot.child("TITLE").getValue().toString());
games.setTag(snapshot.child("TAG").getValue().toString());
games.setPrice(snapshot.child("PRICE").getValue().toString());
gamesList.add(games);
}
recyclerAdapter = new RecyclerAdapter(getApplicationContext(), gamesList);
recyclerView.setAdapter(recyclerAdapter);
recyclerAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
private void ClearAll(){
if(gamesList!=null){
gamesList.clear();
if(recyclerAdapter!=null)
recyclerAdapter.notifyDataSetChanged();
}
gamesList= new ArrayList<>();
}
public class ViewHolder extends RecyclerView.ViewHolder{
ImageView imageView;
TextView textView, textView1, textView2;
CardView cardView;
public ViewHolder(@NonNull View itemView) {
super(itemView);
imageView=itemView.findViewById(R.id.game_img_id);
textView=itemView.findViewById(R.id.game_title_id);
textView1=itemView.findViewById(R.id.game_tags_id);
textView2=itemView.findViewById(R.id.game_price_id);
}
}
@Override
public void onClick(View v) {
Intent i;
switch (v.getId()){
case R.id.home:
startActivity(new Intent(this, News.class));
break;
case R.id.lupa:
startActivity(new Intent(this, Games.class));
break;
case R.id.calendar:
startActivity(new Intent(this, Calendar.class));
break;
case R.id.kubik:
startActivity(new Intent(this, Random_game.class));
break;
case R.id.profile:
startActivity(new Intent(this, Profile.class));
break;
}
}
}
您可以使用 ViewHolder
转到另一个 activity
public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
itemLayoutView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
// perform your action here
}
});
}
}
使用 Intent putExtra() 传递您的数据,并且可以使用 getExtraString() 在游戏 Class 中获取您的数据。
回收器适配器。
这里我用CardView注册了RecyclerView的Adapter。主要问题是他最近才开始做移动开发,知识还不够。
class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder>{
private static final String TAG ="RecyclerView";
private Context mContext;
private ArrayList<Game> gamesList;
public RecyclerAdapter(Context mContext, ArrayList<Game> gamesList){
this.mContext=mContext;
this.gamesList=gamesList;
}
@NonNull
@Override
public RecyclerAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view=LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_games_items, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull RecyclerAdapter.ViewHolder holder, int position) {
holder.textView.setText(gamesList.get(position).getTitle());
holder.textView1.setText(gamesList.get(position).getTag());
holder.textView2.setText(gamesList.get(position).getPrice());
Glide.with(mContext).load(gamesList.get(position).getImage()).into(holder.imageView);
}
@Override
public int getItemCount() {
return gamesList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
ImageView imageView;
TextView textView, textView1, textView2;
public ViewHolder(@NonNull View itemView) {
super(itemView);
imageView=itemView.findViewById(R.id.game_img_id);
textView=itemView.findViewById(R.id.game_title_id);
textView1=itemView.findViewById(R.id.game_tags_id);
textView2=itemView.findViewById(R.id.game_price_id);
}
}
}
游戏。
应该单击并转换到包含游戏说明的新 activity。信息取自 FireBase 数据库。
public class Games extends AppCompatActivity implements View.OnClickListener{
RecyclerView recyclerView;
private DatabaseReference myRef;
private ArrayList<Game> gamesList;
private RecyclerAdapter recyclerAdapter;
private Context mContext;
@Override
protected void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_games);
ImageButton Home = (ImageButton) findViewById(R.id.home);
Home.setOnClickListener(this);
ImageButton Lupa = (ImageButton) findViewById(R.id.lupa);
Lupa.setOnClickListener(this);
ImageButton Calendar = (ImageButton) findViewById(R.id.calendar);
Calendar.setOnClickListener(this);
ImageButton Kubik = (ImageButton) findViewById(R.id.kubik);
Kubik.setOnClickListener(this);
ImageButton Profile = (ImageButton) findViewById(R.id.profile);
Profile.setOnClickListener(this);
recyclerView = findViewById(R.id.recyclerview_id);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(new GridLayoutManager(this,2));
recyclerView.setHasFixedSize(true);
myRef=FirebaseDatabase.getInstance().getReference();
gamesList= new ArrayList<>();
GetDataFromFirebase();
}
private void GetDataFromFirebase(){
Query query_adventures = myRef.child("Parsing/Adventures");
Query query_indi= myRef.child("Parsing/Indi");
Query query_casuals= myRef.child("Parsing/Casuals");
Query query_freegames= myRef.child("Parsing/Freegames");
Query query_mmos= myRef.child("Parsing/Mmos");
Query query_races= myRef.child("Parsing/Races");
Query query_RP = myRef.child("Parsing/RP");
Query query_simulator = myRef.child("Parsing/Simulator");
query_adventures.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()){
Game games = new Game();
games.setImage(snapshot.child("IMG").getValue().toString());
games.setTitle(snapshot.child("TITLE").getValue().toString());
games.setTag(snapshot.child("TAG").getValue().toString());
games.setPrice(snapshot.child("PRICE").getValue().toString());
gamesList.add(games);
}
recyclerAdapter = new RecyclerAdapter(getApplicationContext(), gamesList);
recyclerView.setAdapter(recyclerAdapter);
recyclerAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
query_indi.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()){
Game games = new Game();
games.setImage(snapshot.child("IMG").getValue().toString());
games.setTitle(snapshot.child("TITLE").getValue().toString());
games.setTag(snapshot.child("TAG").getValue().toString());
games.setPrice(snapshot.child("PRICE").getValue().toString());
gamesList.add(games);
}
recyclerAdapter = new RecyclerAdapter(getApplicationContext(), gamesList);
recyclerView.setAdapter(recyclerAdapter);
recyclerAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
query_casuals.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()){
Game games = new Game();
games.setImage(snapshot.child("IMG").getValue().toString());
games.setTitle(snapshot.child("TITLE").getValue().toString());
games.setTag(snapshot.child("TAG").getValue().toString());
games.setPrice(snapshot.child("PRICE").getValue().toString());
gamesList.add(games);
}
recyclerAdapter = new RecyclerAdapter(getApplicationContext(), gamesList);
recyclerView.setAdapter(recyclerAdapter);
recyclerAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
query_freegames.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()){
Game games = new Game();
games.setImage(snapshot.child("IMG").getValue().toString());
games.setTitle(snapshot.child("TITLE").getValue().toString());
games.setTag(snapshot.child("TAG").getValue().toString());
games.setPrice(snapshot.child("PRICE").getValue().toString());
gamesList.add(games);
}
recyclerAdapter = new RecyclerAdapter(getApplicationContext(), gamesList);
recyclerView.setAdapter(recyclerAdapter);
recyclerAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
query_mmos.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()){
Game games = new Game();
games.setImage(snapshot.child("IMG").getValue().toString());
games.setTitle(snapshot.child("TITLE").getValue().toString());
games.setTag(snapshot.child("TAG").getValue().toString());
games.setPrice(snapshot.child("PRICE").getValue().toString());
gamesList.add(games);
}
recyclerAdapter = new RecyclerAdapter(getApplicationContext(), gamesList);
recyclerView.setAdapter(recyclerAdapter);
recyclerAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
query_races.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()){
Game games = new Game();
games.setImage(snapshot.child("IMG").getValue().toString());
games.setTitle(snapshot.child("TITLE").getValue().toString());
games.setTag(snapshot.child("TAG").getValue().toString());
games.setPrice(snapshot.child("PRICE").getValue().toString());
gamesList.add(games);
}
recyclerAdapter = new RecyclerAdapter(getApplicationContext(), gamesList);
recyclerView.setAdapter(recyclerAdapter);
recyclerAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
query_RP.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()){
Game games = new Game();
games.setImage(snapshot.child("IMG").getValue().toString());
games.setTitle(snapshot.child("TITLE").getValue().toString());
games.setTag(snapshot.child("TAG").getValue().toString());
games.setPrice(snapshot.child("PRICE").getValue().toString());
gamesList.add(games);
}
recyclerAdapter = new RecyclerAdapter(getApplicationContext(), gamesList);
recyclerView.setAdapter(recyclerAdapter);
recyclerAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
query_simulator.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()){
Game games = new Game();
games.setImage(snapshot.child("IMG").getValue().toString());
games.setTitle(snapshot.child("TITLE").getValue().toString());
games.setTag(snapshot.child("TAG").getValue().toString());
games.setPrice(snapshot.child("PRICE").getValue().toString());
gamesList.add(games);
}
recyclerAdapter = new RecyclerAdapter(getApplicationContext(), gamesList);
recyclerView.setAdapter(recyclerAdapter);
recyclerAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
private void ClearAll(){
if(gamesList!=null){
gamesList.clear();
if(recyclerAdapter!=null)
recyclerAdapter.notifyDataSetChanged();
}
gamesList= new ArrayList<>();
}
public class ViewHolder extends RecyclerView.ViewHolder{
ImageView imageView;
TextView textView, textView1, textView2;
CardView cardView;
public ViewHolder(@NonNull View itemView) {
super(itemView);
imageView=itemView.findViewById(R.id.game_img_id);
textView=itemView.findViewById(R.id.game_title_id);
textView1=itemView.findViewById(R.id.game_tags_id);
textView2=itemView.findViewById(R.id.game_price_id);
}
}
@Override
public void onClick(View v) {
Intent i;
switch (v.getId()){
case R.id.home:
startActivity(new Intent(this, News.class));
break;
case R.id.lupa:
startActivity(new Intent(this, Games.class));
break;
case R.id.calendar:
startActivity(new Intent(this, Calendar.class));
break;
case R.id.kubik:
startActivity(new Intent(this, Random_game.class));
break;
case R.id.profile:
startActivity(new Intent(this, Profile.class));
break;
}
}
}
您可以使用 ViewHolder
转到另一个 activitypublic ViewHolder(View itemLayoutView) {
super(itemLayoutView);
itemLayoutView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
// perform your action here
}
});
}
}
使用 Intent putExtra() 传递您的数据,并且可以使用 getExtraString() 在游戏 Class 中获取您的数据。