当前位置: 首页 > 知识库问答 >
问题:

这是我的密码。我想从回收器视图打开一个新活动。请帮我提前从这个感谢开启一个新的活动

燕寒
2023-03-14

单击Recolycer视图中的项目时,我希望它打开一个新的活动。这是主要活动

package com.example.myclassroom;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.myclassroom.helperclass.HomeAdapter.ItemClickListener;
//import com.example.myclassroom.helperclass.HomeAdapter.OnUGListener;
import com.example.myclassroom.helperclass.HomeAdapter.PGadapter;
import com.example.myclassroom.helperclass.HomeAdapter.PGhelperclass;
import com.example.myclassroom.helperclass.HomeAdapter.UGadapter;
import com.example.myclassroom.helperclass.HomeAdapter.UGhelperclass;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationView;
import com.google.firebase.auth.FirebaseAuth;

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

public class firstpage extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, BottomNavigationView.OnNavigationItemSelectedListener {
    //navigation
    static final float END_SCALE = 0.7f;


    private DrawerLayout drawer;
    private NavigationView navigation;

    public List<UGhelperclass> uglocation;


    LinearLayout contentView;

    //cards
    RecyclerView ugrecycler, pgrecycler;
    RecyclerView.Adapter adapter;
    ImageView menuIcon;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_firstpage);

        //hooks
        ugrecycler = findViewById(R.id.ug_recycler);
        pgrecycler = findViewById(R.id.pg_recycler);
        contentView = findViewById(R.id.content_id);

        //Menu hooks

        navigation = findViewById(R.id.nav_view);
        drawer = findViewById(R.id.drawer_layout);
        menuIcon = findViewById(R.id.menu_icon);


        ugrecycler();
        pgrecycler();
        navigationDrawer();


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.search, menu);

        MenuItem.OnActionExpandListener onActionExpandListener = new MenuItem.OnActionExpandListener() {
            @Override
            public boolean onMenuItemActionExpand(MenuItem menuItem) {
                return false;
            }

            @Override
            public boolean onMenuItemActionCollapse(MenuItem menuItem) {
                return false;
            }
        };

        return true;
    }

    private void navigationDrawer() {
        //navigation drawer


        navigation.bringToFront();
        navigation.setNavigationItemSelectedListener(this);
        navigation.setCheckedItem(R.id.nav_home);
        menuIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (drawer.isDrawerVisible(GravityCompat.START))
                    drawer.closeDrawer(GravityCompat.START);
                else drawer.openDrawer(GravityCompat.START);
            }


        });

        animateNavigationDrawer();
    }

    private void animateNavigationDrawer() {
        drawer.setScrimColor(getResources().getColor(R.color.bg));
        drawer.addDrawerListener(new DrawerLayout.SimpleDrawerListener() {
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                //scale the view based on current slide effect
                final float diffScaledOffset = slideOffset * (1 - END_SCALE);
                final float offSetScale = 1 - diffScaledOffset;
                contentView.setScaleX(offSetScale);
                contentView.setScaleY(offSetScale);

                //transilate the view
                final float xoffset = drawerView.getWidth() * slideOffset;
                final float xoffsetDiff = contentView.getWidth() * diffScaledOffset / 2;
                final float xTranslation = xoffset - xoffsetDiff;
                contentView.setTranslationX(xTranslation);

            }
        });
    }


    @Override
    public void onBackPressed() {
        if (drawer.isDrawerVisible(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {

        switch (menuItem.getItemId()) {

            case R.id.nav_ebook:
                Intent intent = new Intent(firstpage.this, fragment_ebooks.class);
                startActivity(intent);

                finish();
                break;
            case R.id.nav_logout:
                FirebaseAuth.getInstance().signOut();
                intent = new Intent(firstpage.this, loginpage.class);
                startActivity(intent);
                finish();
            case R.id.nav_share:
                break;
        }
        drawer.closeDrawer(GravityCompat.START);

        return true;
    }

    private void pgrecycler() {
        pgrecycler.setHasFixedSize(true);
        pgrecycler.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));

        ArrayList<PGhelperclass> pglocation = new ArrayList<>();
        pglocation.add(new PGhelperclass(R.drawable.computer_main_screen, "Computer Science", "Notes for Bsc and BCA"));
        pglocation.add(new PGhelperclass(R.drawable.microbiology, "Microbiology", "Notes for micro"));
        pglocation.add(new PGhelperclass(R.drawable.chemistry, "Chemistry", "Notes for chemistry"));

        PGadapter adapter = new PGadapter(pglocation);
        pgrecycler.setAdapter(adapter);
    }

    private void ugrecycler() {
        ugrecycler.setHasFixedSize(true);
        ugrecycler.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));

        final ArrayList<UGhelperclass> uglocation = new ArrayList<>();

        uglocation.add(
                new UGhelperclass(
                        1,
                        R.drawable.computer_main_screen,
                        "Nandhini",
                        "https://firebasestorage.googleapis.com/v0/b/my-classroom-50f12.appspot.com/o/11.pdf?alt=media&token=48bb603d-b7b9-4588-9539-33381077afd2",

                        6000
                ));


        UGadapter adapter = new UGadapter(this, uglocation);
        ugrecycler.setAdapter(adapter);


    }


}

这是适配器

package com.example.myclassroom.helperclass.HomeAdapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myclassroom.R;
import com.google.firebase.database.core.Context;

import java.util.ArrayList;

public class PGadapter extends RecyclerView.Adapter<PGadapter.PGviewholder> {

//1
    private final ArrayList<PGhelperclass> pglocation;


    //2

    public PGadapter(ArrayList<PGhelperclass> pglocation) {
        this.pglocation = pglocation;

    }




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

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.pg_card_design,parent,false);
        PGviewholder pgviewholder = new PGviewholder(view);
        return pgviewholder;
    }

    @Override
    public void onBindViewHolder(@NonNull PGadapter.PGviewholder holder, int position) {
        PGhelperclass pghelperclass = pglocation.get(position);

        holder.image.setImageResource(pghelperclass.getImage());
        holder.title.setText(pghelperclass.getTitle());
        holder.desc.setText(pghelperclass.getDescription());


    }

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

    //3
    public static class PGviewholder extends RecyclerView.ViewHolder {

        ImageView image;
        TextView title,desc;

        public PGviewholder(@NonNull View itemView) {
            super(itemView);

            //hooks

            image = itemView.findViewById(R.id.pg_img);
            title = itemView.findViewById(R.id.pg_course_title);
            desc = itemView.findViewById(R.id.pg_desc);

        }
    }
}
package com.example.myclassroom.helperclass.HomeAdapter;

public class PGhelperclass {

    int image;
    String title, description;

    public PGhelperclass(int image, String title, String description) {
        this.image = image;
        this.title = title;
        this.description = description;


    }

    public int getImage() {
        return image;
    }

    public String getTitle() {
        return title;
    }

    public String getDescription() {
        return description;
    }
}

...................................................

共有1个答案

江光明
2023-03-14

在OnBindViewWholder类中,

    holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              if(position==1){
 Intent intent = new Intent(itemView.getContext(), SecondActivity.class);
}else if(position==2){
 Intent intent = new Intent(itemView.getContext(), SomeOtherActivity.class);
}
               
                itemView.getContext().startActivity(intent);
            }
        });
 类似资料: