这是我的主要档案。 Departments.java
public class Departments extends Activity {
private List<Departments_Item> facultati;
private RecyclerView rv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.departments);
rv=(RecyclerView)findViewById(R.id.rv);
LinearLayoutManager llm = new LinearLayoutManager(this);
rv.setLayoutManager(llm);
rv.setHasFixedSize(true);
initializeData();
initializeAdapter();
}
private void initializeData(){
facultati = new ArrayList<>();
facultati.add(new Departments_Item("Trasnporturi", "Baieti destepti", R.drawable.faculate_1));
facultati.add(new Departments_Item("Automatica si Claculatoare", "Hackeri", R.drawable.faculatate_2));
facultati.add(new Departments_Item("IMST","Nu am habar", R.drawable.facultate_3));
facultati.add(new Departments_Item("FAIMA", "Fete cool", R.drawable.facultate_4));
}
private void initializeAdapter(){
DepartmentsAdapter adapter = new DepartmentsAdapter(facultati);
rv.setAdapter(adapter);
}
}
我有一个Departments_Items.java
class Departments_Item {
String facultate;
String info1;
int photoId;
Departments_Item(String facultate, String info1, int photoId) {
this.facultate = facultate;
this.info1= info1;
this.photoId = photoId;
}
}
我有DepartmentsAdapter.java
public class DepartmentsAdapter extends RecyclerView.Adapter<DepartmentsAdapter.PersonViewHolder> {
public static class PersonViewHolder extends RecyclerView.ViewHolder {
CardView cv;
TextView facultate;
TextView info1;
ImageView personPhoto;
PersonViewHolder(View itemView) {
super(itemView);
cv = (CardView)itemView.findViewById(R.id.cv);
facultate = (TextView)itemView.findViewById(R.id.facultate);
info1 = (TextView)itemView.findViewById(R.id.facultate_info1);
personPhoto = (ImageView)itemView.findViewById(R.id.facultate_photo);
}
}
List<Departments_Item> facultati;
DepartmentsAdapter(List<Departments_Item> facultati){
this.facultati = facultati;
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
@Override
public PersonViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.departments_item, viewGroup, false);
PersonViewHolder pvh = new PersonViewHolder(v);
return pvh;
}
@Override
public void onBindViewHolder(PersonViewHolder personViewHolder, int i) {
personViewHolder.facultate.setText(facultati.get(i).
facultate);
personViewHolder.info1.setText(facultati.get(i).info1);
personViewHolder.personPhoto.setImageResource(facultati.get(i).photoId);
}
@Override
public int getItemCount() {
return facultati.size();
}
}
如何使用onItemClick开启新活动,这是一个带有cardview的回收者视图,我只想点击卡片视图并打开一个活动。 我是Android Studio和Java的新手,如果你可以帮助我,我会帮助它。