我有一個RecyclerView完整的元素,在這種情況下是CardViews,我也有一個開關,根據狀態排序RecyclerView的元素,所以根據這個元素改變它們的位置,我想去一個不同的活動從回收站視圖的每一個元素,但我有點卡住,因爲我還沒有找到任何來源,以我的方式初始化元素的數據。如何從此RecyclerView開始活動?
這裏是RECYCLERVIEW:
public class CategoriesRecyclerView extends Activity {
private List<Categories> categories;
private RecyclerView rv;
private Switch categoriesSortingSwitch;
private TextView switchStatus;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.categories_recycler_view);
rv = (RecyclerView) findViewById(R.id.cardList);
rv.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(this);
llm.setOrientation(LinearLayoutManager.VERTICAL);
rv.setLayoutManager(llm);
initializeData();
final CategoriesAdapter ca = new CategoriesAdapter(categories);
rv.setAdapter(ca);
//IMPLEMENTACIÓN DE EL SORTING POR MEDIO DE EL SWITCH
categoriesSortingSwitch = (Switch) findViewById(R.id.switchsortcategories);
switchStatus = (TextView) findViewById(R.id.testswitch);
//set the switch to OFF
categoriesSortingSwitch.setChecked(false);
//attach a listener to check for changes in state
categoriesSortingSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
switchStatus.setText("Sorting alphabetically");
Collections.sort(categories, new Comparator<Categories>() {
@Override
public int compare(Categories lhs, Categories rhs) {
return lhs.title.compareTo(rhs.title);
}
});
ca.notifyDataSetChanged();
}else{
switchStatus.setText("Sorting by popularity");
Collections.sort(categories, new Comparator<Categories>() {
@Override
public int compare(Categories rhs, Categories lhs) {
return lhs.title.compareTo(rhs.title);
}
});
ca.notifyDataSetChanged();
}
}
});
//check the current state before we display the screen
if(categoriesSortingSwitch.isChecked()){
switchStatus.setText("Sorting alphabetically");
Collections.sort(categories, new Comparator<Categories>() {
@Override
public int compare(Categories lhs, Categories rhs) {
return lhs.title.compareTo(rhs.title);
}
});
}
else {
switchStatus.setText("Sorting by popularity");
Collections.sort(categories, new Comparator<Categories>() {
@Override
public int compare(Categories rhs, Categories lhs) {
return lhs.title.compareTo(rhs.title);
}
});
}
}
private void initializeData() {
categories = new ArrayList<>();
categories.add(new Categories("CARS", "CARS"));
categories.add(new Categories("SPORTS", "SPORTS"));
categories.add(new Categories("GAMING", "GAMING"));
categories.add(new Categories("GAMBLING", "GAMBLING"));
categories.add(new Categories("TECH", "TECH"));
categories.add(new Categories("NATURE", "NATURE"));
categories.add(new Categories("RANDOM", "RANDOM"));
categories.add(new Categories("COUSINE", "COUSINE"));
categories.add(new Categories("HISTORY", "HISTORY"));
categories.add(new Categories("MUSIC", "MUSIC"));
categories.add(new Categories("STUDIES", "STUDIES"));
}
}
這裏是適配器:
public class CategoriesAdapter extends RecyclerView.Adapter<CategoriesAdapter.CategoriesViewHolder> {
private List<Categories> categoriesList;
public CategoriesAdapter(List<Categories> categoriesList) {
this.categoriesList = categoriesList;
}
public static class CategoriesViewHolder extends RecyclerView.ViewHolder {
CardView cv;
TextView title;
TextView category_title;
//ImageView category_img; CUANDO VALLA A REMPLAZAR LA FORO DE CATEGORIA
CategoriesViewHolder(View itemView) {
super(itemView);
cv = (CardView)itemView.findViewById(R.id.card_view);
title = (TextView)itemView.findViewById(R.id.title);
category_title = (TextView)itemView.findViewById(R.id.category_title);
//category_img = (ImageView)itemView.findViewById(R.id.category_picture);
}
}
@Override
public int getItemCount() {
return categoriesList.size();
}
@Override
public void onBindViewHolder(CategoriesViewHolder categoriesViewHolder, final int i) {
Categories ci = categoriesList.get(i);
categoriesViewHolder.title.setText(categoriesList.get(i).title);
categoriesViewHolder.category_title.setText(categoriesList.get(i).category_title);
//categoriesViewHolder.categori_img.setImageResource(categoriesList.get(i).photoId);
}
@Override
public CategoriesViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.category_row, viewGroup, false);
return new CategoriesViewHolder(itemView);
}
}
我會很感激如何做到這一點的任何指令。 非常感謝。
爲了使用上下文初始化新的活動,必須對我的代碼進行一些更改,它現在正在工作,非常感謝。 –
@Sebastian,你可以通過調用itemView.getContext()來獲取ViewHolder中的上下文,並且不需要改變其他的東西。 – Brucelet