Android RecyclerView如何通过单击项目打开新活动

时间:2018-10-06 02:22:34

标签: android android-recyclerview

大家好,有人可以帮我完成这个简单的项目吗? 我不知道如何配置要打开新活动的项目的点击。

RecycleViewAdapter.java

import java.util.List;

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

private List<ModelData> users;
private Context context;

public RecyclerViewAdapter(List<ModelData> users, Context context) {
    this.users = users;
    this.context = context;
}

@Override
public ViewHolder onCreateViewHolder( ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(context.getApplicationContext()).inflate(R.layout.layout_row, parent, false);
    return new ViewHolder(v);
}

@Override
public void onBindViewHolder( ViewHolder holder, int position) {
    ModelData user = users.get(position);
    holder.nomeUser.setText(user.getName());
    holder.userImage.setImageResource(user.getUserImage());


}

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

public class ViewHolder extends RecyclerView.ViewHolder {

    private TextView nomeUser;
    private ImageView userImage;


    public ViewHolder( View itemView) {
        super(itemView);
        nomeUser = itemView.findViewById(R.id.nomeText);
        userImage = itemView.findViewById(R.id.imageView);
        context = itemView.getContext();

    }


}

ModelData.java

package com.example.aicopolilattico.splashscreen;

import java.security.PublicKey;

public class ModelData {

private String name;
private int userImage;

public ModelData() {}

public ModelData(String name, int userImage ) {

    this.name = name;
    this.userImage = userImage;

}

public String getName() {
    return name;
}

public int getUserImage() {
    return userImage;
}

ActivityMain.java

package com.example.aicopolilattico.splashscreen;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;


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

public class ActivityMan extends AppCompatActivity {

private RecyclerView recycleView;
private List<ModelData> users;

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

    recycleView = findViewById(R.id.recycleView);
    users = new ArrayList<>();

    users.add(new ModelData("Jack and Jones", R.drawable.jackcjones111));
    users.add(new ModelData("Marlboro", R.drawable.marlb));

    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
    recycleView.setLayoutManager(linearLayoutManager);

    recycleView.setAdapter(new RecyclerViewAdapter(users, this));
    recycleView.setHasFixedSize(true);


}

人真的很重要。 预先感谢您有耐心和时间给我。 :)

[! ! !更新 ! ! ! ]

我尝试编写下面的代码,但是当我单击该项目时,它会带我进入应用程序的第一个活动。忠告?非常感谢

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


private List<ModelData> users;
private Context context;


public RecyclerViewAdapter(List<ModelData> users, Context context)
{
    this.users = users;
    this.context = context;

}

@Override
public ViewHolder onCreateViewHolder( ViewGroup parent, int viewType)
{
    View v = LayoutInflater.from(context.getApplicationContext()).inflate(R.layout.layout_row, parent, false);
    return new ViewHolder(v);
}

@Override
public void onBindViewHolder( ViewHolder holder, int position)
{
    final ModelData user = users.get(position);
    holder.nomeUser.setText(user.getName());
    holder.userImage.setImageResource(user.getUserImage());
    holder.nomeUser.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(context,Photo_Man.class);

            context.startActivity(intent);
        }
    });


}


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




public class ViewHolder extends RecyclerView.ViewHolder  {

    private TextView nomeUser;
    private ImageView userImage;


    public ViewHolder( View itemView)
    {
        super(itemView);
        nomeUser = itemView.findViewById(R.id.nomeText);
        userImage = itemView.findViewById(R.id.imageView);
        context = itemView.getContext();



    }

}

}

4 个答案:

答案 0 :(得分:0)

实现这一目标的最佳和最简便的方法是将事件的侦听器从活动发送到recyclerview。

像这样...

  1. 创建用于监听点击事件的界面:

    public interface OnItemClickListener {
        void onItemClick();
    }
    
  2. 修改适配器以接收侦听器:

    private OnItemClickListener listener;
    
    public RecyclerViewAdapter(List<ModelData> users, Context context, OnItemClickListener listener ) {
        this.users = users;
        this.context = context;
        this.listener = listener;
    }
    
  3. 从活动中创建侦听器(从此处可以打开新活动):

    OnItemClickListener listener = new OnItemClickListener() {
        @Override public void onItemClick() {
            Intent intent = new Intent(ActivityMain.this, NewActivity.class);
            startActivity(intent);
        }
    };
    
  4. 将该接口发送到适配器:

    recycleView.setAdapter(new RecyclerViewAdapter(users, this, listener));
    
  5. 将此侦听器设置为您要观察事件的视图:

    @Override
    public void onBindViewHolder( ViewHolder holder, int position) {
        ModelData user = users.get(position);
        holder.nomeUser.setText(user.getName());
        holder.userImage.setImageResource(user.getUserImage());
        holder.userImage.setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View v) {
                listener.onItemClick();
            }
        });
    }
    

答案 1 :(得分:0)

首先请使用recyclerview适配器的onBindViewHolder方法。

然后只需添加以下代码。这里itemView是recyclerview的其中一项

holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override public void onClick(View v) {
            // here you go for second activity using intent
            Intent intent = new Intent(context, SecondActivity.class); // here you need context from constructor, SecondActivity is another activity
            context.startActivity(intent); // this will call the SecondActivity intent
            ((Activity)context).finish(); // this is for just finish current activity (OPTIONAL, as per requirement)
        }
    });

答案 2 :(得分:0)

如果您想参加活动

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


          Intent intent = new Intent(context,SomeActivity.class); 

        context.startActivity(intent); 

            }
        });

如果要转到片段

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

                FragmentManager manager = ((Activity) context).getFragmentManager();
                manager.beginTransaction().replace(R.id.transaction,get_posts).addToBackStack(null).commit();

            }
        });

答案 3 :(得分:0)

您能否像下面的代码一样替换Click侦听器

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

context.startActivity(new Intent(context, Photo_Man.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));

        }
    });