im试图在RecyClerview上单击和项目时,它将打开新的活动,并使用单击项目的详细信息。
我的apapter.java代码是:
public class AdapterUsersData extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
private LayoutInflater inflater;
List<UsersData> data= Collections.emptyList();
UsersData current;
int currentPos=0;
// create constructor to innitilize context and data sent from MainActivity
public AdapterUsersData(Context context, List<UsersData> data){
this.context=context;
inflater= LayoutInflater.from(context);
this.data=data;
}
// Inflate the layout when viewholder created
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view=inflater.inflate(R.layout.container_users, parent,false);
MyHolder holder=new MyHolder(view);
return holder;
}
// Bind data
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
// Get current position of item in recyclerview to bind data and assign values from list
final MyHolder myHolder= (MyHolder) holder;
final UsersData current=data.get(position);
myHolder.textFirstName.setText("First Name: " + current.fname);
myHolder.textPassword.setText("Pass: " + current.password);
myHolder.textLastName.setText("Last Name: " + current.lname);
myHolder.textEmail.setText("Email. " + current.email);
}
// return total item from List
@Override
public int getItemCount() {
return data.size();
}
class MyHolder extends RecyclerView.ViewHolder{
TextView textFirstName;
TextView textPassword;
TextView textLastName;
TextView textEmail;
// create constructor to get widget reference
public MyHolder(View itemView) {
super(itemView);
textFirstName= (TextView) itemView.findViewById(R.id.textFirstName);
textPassword = (TextView) itemView.findViewById(R.id.textPassword);
textLastName = (TextView) itemView.findViewById(R.id.textLastName);
textEmail = (TextView) itemView.findViewById(R.id.textEmail);
}
}
}
例如,我想将" curremt.fname ......"解析到另一个活动中,以向用户显示单击项目的完整详细信息。
recyclerviews提供了一个框的功能。您需要自己提供开机功能。长时间紧迫。
理想情况下,您的视图持有人实现了视图。inclickListener界面以及视图持有人的构造函数内部,您将其注册为ItemView的单击"侦听器"。每当调用onclick方法时,您都可以通过调用getAdapterPostion()来获得该Viewholder实例的位置。它将返回指示相对于适配器数据集的视图位置的int。您可以从这里调用recisster自定义回调。我在应用程序中所做的工作是创建一个使用一种方法来触发并接收到INT的位置和单击的视图的方法。我已经修改了您的代码,为您提供了可以做什么的示例。
public class AdapterUsersData extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
private LayoutInflater inflater;
List<UsersData> data= Collections.emptyList();
UsersData current;
int currentPos=0;
// create constructor to innitilize context and data sent from MainActivity
public AdapterUsersData(Context context, List<UsersData> data){
this.context=context;
inflater= LayoutInflater.from(context);
this.data=data;
}
// Inflate the layout when viewholder created
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view=inflater.inflate(R.layout.container_users, parent,false);
MyHolder holder=new MyHolder(view);
return holder;
}
// Bind data
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
// Get current position of item in recyclerview to bind data and assign values from list
final MyHolder myHolder= (MyHolder) holder;
final UsersData current=data.get(position);
myHolder.textFirstName.setText("First Name: " + current.fname);
myHolder.textPassword.setText("Pass: " + current.password);
myHolder.textLastName.setText("Last Name: " + current.lname);
myHolder.textEmail.setText("Email. " + current.email);
}
// return total item from List
@Override
public int getItemCount() {
return data.size();
}
public interface OnItemClickListener {
void onItemClicked(View v, int position);
}
private OnItemClickListener listener;
class MyHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView textFirstName;
TextView textPassword;
TextView textLastName;
TextView textEmail;
// create constructor to get widget reference
public MyHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
textFirstName= (TextView) itemView.findViewById(R.id.textFirstName);
textPassword = (TextView) itemView.findViewById(R.id.textPassword);
textLastName = (TextView) itemView.findViewById(R.id.textLastName);
textEmail = (TextView) itemView.findViewById(R.id.textEmail);
}
public void onClick(View v) {
if(listener != null) {
listener.onItemClicked(v, getAdapterPosition());
}
}
}
您可以尝试..
class dapterUsersData extends RecyclerView.Adapter<dapterUsersData.ViewHolder> {
private Context context;
private LayoutInflater inflater;
List<UsersData> data = Collections.emptyList();
ArrayList<UsersData> dataSet;
public dapterUsersData(ArrayList<UsersData> data) {
this.data = data;
}
@Override
public dapterUsersData.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View itemLayoutView = LayoutInflater.from(viewGroup.getContext()).inflate(
R.layout.container_users, null);
itemLayoutView.setLayoutParams(new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.WRAP_CONTENT));
dapterUsersData.ViewHolder viewHolder = new dapterUsersData.ViewHolder(itemLayoutView);
return viewHolder;
}
@Override
public void onBindViewHolder(dapterUsersData.ViewHolder viewHolder, int i) {
UsersData current = data.get(i);
viewHolder.textFirstName.setText("First Name: " + current.getFname());
viewHolder.textPassword.setText("Pass: " + current.getPassword());
viewHolder.textLastName.setText("Last Name: " + current.getLname());
viewHolder.textEmail.setText("Email. " + current.getEmail());
viewHolder.menu = current;
}
@Override
public int getItemCount() {
return dataSet.size();
}
public void updateList(List<UsersData> temp) {
dataSet = (ArrayList<UsersData>) temp;
notifyDataSetChanged();
}
// inner class to hold a reference to each item of RecyclerView
public class ViewHolder extends RecyclerView.ViewHolder {
TextView textFirstName;
TextView textPassword;
TextView textLastName;
TextView textEmail;
public UsersData menu;
public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
textFirstName= (TextView) itemLayoutView.findViewById(R.id.textFirstName);
textPassword = (TextView) itemLayoutView.findViewById(R.id.textPassword);
textLastName = (TextView) itemLayoutView.findViewById(R.id.textLastName);
textEmail = (TextView) itemLayoutView.findViewById(R.id.textEmail);
itemLayoutView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// OnClick
String firstName = textFirstName.getText().toString();
}
});
}
}
}
//end Adapter
谢谢你们的帮助!
无论如何我只是添加了:
myHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context,current.fname.toString(),Toast.LENGTH_LONG).show();
}
});
to:public void onBindViewHolder(recyClerview.iweviewholder Holder,int位置)方法,它起作用。