从不同的活动中打开RecycleAdapter中的特定卡片项目。

问题描述 投票:0回答:1

我将尽可能的具体化。我创建了一个使用RecyclerAdapter的页面来在网格中显示卡片项目,并且我使用OnclickListner为每张卡片设置了一个默认活动,所以当你点击任何卡片项目时,它将带你到那个默认活动。喜欢这张图片.而我的一个看起来像这样 点击这里.我的问题是,我怎样才能在Recycler Adapter中从不同的活动中打开一张特定的卡,在我的应用程序中,我希望当我按下弹出的Go按钮时,它将切换lvl2或下一张卡来打开游戏活动。

回收器适配器的Java文件

package com.example.apptuzzle;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {

    private Context mcontext;
    private List<InGameContent> mData;


    public RecyclerViewAdapter(Context mcontext, List<InGameContent> mData){
        this.mcontext = mcontext;
        this.mData = mData;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view;
        LayoutInflater mInflater = LayoutInflater.from(mcontext);
        view = mInflater.inflate(R.layout.cardview_items1,parent,false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
         holder.Title.setText(mData.get(position).getTitle());
         holder.Title.setTag(position);

        holder.cardview.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 int  pos = (int) getItemId(position) + 1;
                 Intent intent = new Intent(mcontext,InGame_wrkfunction.class);
                 intent.putExtra("pos", pos);
                 //passing data to the InGame_wrkfunction.class
                 intent.putExtra("Answrr",mData.get(position).getAnswrr());
                 intent.putExtra("Thumbnail",mData.get(position).getThumbnail());
                 intent.putExtra("Description",mData.get(position).getDescription());
                 // start the activity
                 mcontext.startActivity(intent);
             }

         });

    }

    @Override
    public int getItemCount() {
        return mData.size();
    }




  public static class MyViewHolder extends RecyclerView.ViewHolder {

        TextView Title;
        CardView cardview;

        public MyViewHolder(View itemView) {
            super(itemView);
            Title = (TextView) itemView.findViewById(R.id.LvL_id1);
            cardview = (CardView) itemView.findViewById(R.id.cardview_id);


        }

    }
}
默认游戏活动

package com.example.apptuzzle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import java.util.ArrayList;
import java.util.List;

public class InGame_wrkfunction extends AppCompatActivity  {
    private Button btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn0, btndot;                //Buttons Declaration
    private Button btnclr, btnsubm, popup_g_button1;
    private TextView txtv1, txtv2, popup_g_msg1, popup_g_des1,highscore1;
    int scores = 0;


    private LinearLayout Popup_layout11;
    private Animation pop_animation;


    private ImageView img,popup_g_img1,popup_b_img;                                                      //Question Declaration
    private TextView txt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_in_game_wrkfunction);


        btn0 = (Button) findViewById(R.id.Btn0);
        btn1 = (Button) findViewById(R.id.Btn1);
        btn2 = (Button) findViewById(R.id.Btn2);
        btn3 = (Button) findViewById(R.id.Btn3);
        btn4 = (Button) findViewById(R.id.Btn4);
        btn5 = (Button) findViewById(R.id.Btn5);
        btn6 = (Button) findViewById(R.id.Btn6);
        btn7 = (Button) findViewById(R.id.Btn7);
        btn8 = (Button) findViewById(R.id.Btn8);
        btn9 = (Button) findViewById(R.id.Btn9);
        btndot = (Button) findViewById(R.id.Btndot);
        btnclr = (Button) findViewById(R.id.BtnClr);
        btnsubm = (Button) findViewById(R.id.BtnSubm);
        img = (ImageView) findViewById(R.id.gamethumbnail);
        txtv1 = (TextView) findViewById(R.id.TxtV1);
        txt = (TextView) findViewById(R.id.TxtV2);
        Popup_layout11 = findViewById(R.id.Popup_layout11);
        txt = (TextView) findViewById(R.id.TxtV2);
        popup_g_button1 = findViewById(R.id.Popup_G_button1);
        popup_g_msg1 = findViewById(R.id.Popup_G_msg1);
        popup_g_des1 = findViewById(R.id.Popup_G_des1);
        popup_g_img1 = findViewById(R.id.Popup_G_Img12);
        highscore1 = findViewById(R.id.ScoreCounter1);



        pop_animation = AnimationUtils.loadAnimation(this, R.anim.pop_up);

        btn0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                txtv1.setBackgroundResource(R.drawable.input_bttn_colorc);
                txtv1.setText(txtv1.getText() + "0");
            }
        });
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                txtv1.setBackgroundResource(R.drawable.input_bttn_colorc);
                txtv1.setText(txtv1.getText() + "1");
            }
        });
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                txtv1.setBackgroundResource(R.drawable.input_bttn_colorc);
                txtv1.setText(txtv1.getText() + "2");
            }
        });
        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                txtv1.setBackgroundResource(R.drawable.input_bttn_colorc);
                txtv1.setText(txtv1.getText() + "3");
            }
        });
        btn4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                txtv1.setBackgroundResource(R.drawable.input_bttn_colorc);
                txtv1.setText(txtv1.getText() + "4");
            }
        });
        btn5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                txtv1.setBackgroundResource(R.drawable.input_bttn_colorc);
                txtv1.setText(txtv1.getText() + "5");
            }
        });
        btn6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                txtv1.setBackgroundResource(R.drawable.input_bttn_colorc);
                txtv1.setText(txtv1.getText() + "6");
            }
        });
        btn7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                txtv1.setBackgroundResource(R.drawable.input_bttn_colorc);
                txtv1.setText(txtv1.getText() + "7");
            }
        });
        btn8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                txtv1.setBackgroundResource(R.drawable.input_bttn_colorc);
                txtv1.setText(txtv1.getText() + "8");
            }
        });
        btn9.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                txtv1.setBackgroundResource(R.drawable.input_bttn_colorc);
                txtv1.setText(txtv1.getText() + "9");
            }
        });
        btndot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                txtv1.setBackgroundResource(R.drawable.input_bttn_colorc);
                txtv1.setText(txtv1.getText() + ".");
            }
        });
        btnclr.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                txtv1.setBackground(null);
                txtv1.setText(null);
            }
        });


        // Receive data

        Intent intent = getIntent();
        String Description = intent.getExtras().getString("Description");               //It ll fetch data from Description()
        int image = intent.getExtras().getInt("Thumbnail");
        int pos = intent.getExtras().getInt("pos");
        final String answrr1 = intent.getExtras().getString("Answrr");

        //Setting Values
        img.setImageResource(image);                                                         ////It ll replace the image that was fetched previously
        txt.setText(Description);

        //Load Scores
        SharedPreferences myscores = this.getSharedPreferences("MyAwesomeScores", Context.MODE_PRIVATE);
        scores = myscores.getInt("scores", 0);  //0 tha phehele
        highscore1.setText("Scores:" + scores);


        btnsubm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String a = txtv1.getText().toString();
                if (a.equals(answrr1)) {
                    scores += 30;
                    //Save scores
                    SharedPreferences myscores = getSharedPreferences("MyAwesomeScores", Context.MODE_PRIVATE);
                    SharedPreferences.Editor editor = myscores.edit();
                    editor.putInt("scores", scores);
                    editor.commit();
                    v.setOnClickListener(null);//Remove setOnClickListener

                    highscore1.setText("Scores:" + scores);


                    Popup_layout11.setVisibility(LinearLayout.VISIBLE);
                    Popup_layout11.setAnimation(pop_animation);
                    Popup_layout11.animate();
                    pop_animation.start();
                } else {
                    scores -= 30;

                    //Save scores
                    SharedPreferences myscores = getSharedPreferences("MyAwesomeScores", Context.MODE_PRIVATE);
                    SharedPreferences.Editor editor = myscores.edit();
                    editor.putInt("scores", scores);
                    editor.commit();
                    v.setOnClickListener(null);//Remove setOnClickListener

                    highscore1.setText("Scores:" + scores);


                    popup_g_img1.setImageResource(R.drawable.sadface11);
                    popup_g_msg1.setText("Ohh Snap!");
                    popup_g_des1.setText("Don't worry Kid you just tap the button to move on to the next lesson. REMEMBER 'never waste time'.");
                    Popup_layout11.setVisibility(LinearLayout.VISIBLE);
                    Popup_layout11.setAnimation(pop_animation);
                    Popup_layout11.animate();
                    pop_animation.start();
                }
            }
        });

    }

}





/*Two Ways of Putting One Animation In an activity
* 1st One
*        Anim1 = AnimationUtils.loadAnimation(this, R.anim.new_animfile1);

      //  pop_img1.setVisibility(View.GONE);
        Btn1.setOnClickListener(new View.OnClickListener() {                <-----In this type you can put animation inside On Click Listener
            @Override
            public void onClick(View v) {

                Layout1.setVisibility(LinearLayout.VISIBLE);
                Anim1.setDuration(500);
                Layout1.setAnimation(Anim1);
                Layout1.animate();
                Anim1.start();

            }
        });
*
*
*  2nd One
* public class MyActivity extends Activity{

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);                                            <-----In this type you can set another class outside the OnCreate Bundle
 }

public void animate(View view){
    LinearLayout dialog   = (LinearLayout)findViewById(R.id.dialog);
    dialog.setVisibility(LinearLayout.VISIBLE);
    Animation animation   =    AnimationUtils.loadAnimation(this, R.anim.anim);
    animation.setDuration(500);
    dialog.setAnimation(animation);
    dialog.animate();
    animation.start();
 }

}
* */
android android-studio android-recyclerview recycler-adapter
1个回答
2
投票

我的理解是,你想在每张卡片上打开不同的活动,如果正确的话。

使用 switch 在您的onClick内打开您的活动,并在特定项目上点击

@Override
public void onClick(View v) {          

    final Intent intent;
    switch (getAdapterPostion()){
        case 0:
           intent =  new Intent(context, FirstActivity.class);
           break;

        case 1:
            intent =  new Intent(context, SecondActivity.class);
            break;
           ...
        default:
           intent =  new Intent(context, DefaultActivity.class);
           break;
     }
    context.startActivity(intent);
}

或使用:-

@Override
public void onClick(View v) {          

    final Intent intent;
    if (getAdapterPosition() == sth){
       intent =  new Intent(context, OneActivity.class);
    } else if (getPosition() == sth2){
       intent =  new Intent(context, SecondActivity.class);
    } else {
       intent =  new Intent(context, DifferentActivity.class);
    }
    context.startActivity(intent);
}
© www.soinside.com 2019 - 2024. All rights reserved.