我想添加switch语句以打开多个活动(目的)

时间:2019-03-24 10:26:22

标签: java android android-intent

我想添加switch语句以打开多个活动(意图)。 如何使用此代码打开新的Intent?

CategoryAdapter:

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

    private List<CategoryModal> categoryModalList;

    public CategoryAdapter(List<CategoryModal> categoryModalList) {
        this.categoryModalList = categoryModalList;
    }

    @Override
    public CategoryAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.category_item,viewGroup,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(CategoryAdapter.ViewHolder viewHolder, int position) {
        String icon = categoryModalList.get(position).getCategoryIconLink();
        String name = categoryModalList.get(position).getCategoryName();
        viewHolder.setCategory(name,position);

    }

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

    public class ViewHolder extends RecyclerView.ViewHolder{

        private ImageView categoryIcon;
        private TextView categoryName;

        public ViewHolder(View itemView) {
            super(itemView);
            categoryIcon = itemView.findViewById(R.id.category_icon);
            categoryName = itemView.findViewById(R.id.category_name);
        }
        private void setCategoryIcon(){


        }
        private void setCategory(final String name, final int position){
            categoryName.setText(name);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (position !=0){
                    Intent categotyIntent = new Intent(itemView.getContext(),CategoryActivity.class);
                    categotyIntent.putExtra("CategoryName",name);
                    itemView.getContext().startActivity(categotyIntent);
                }
                }
            });
        }
    }
}

CategoryModel:

public class CategoryModal {

    private String CategoryIconLink;
    private String CategoryName;

    public CategoryModal(String categoryIconLink, String categoryName) {
        CategoryIconLink = categoryIconLink;
        CategoryName = categoryName;
    }

    public String getCategoryIconLink() {
        return CategoryIconLink;
    }

    public void setCategoryIconLink(String categoryIconLink) {
        CategoryIconLink = categoryIconLink;
    }

    public String getCategoryName() {
        return CategoryName;
    }

    public void setCategoryName(String categoryName) {
        CategoryName = categoryName;
    }
}

HomePageFragment:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_home_fragment1, container, false);

    categoryRecycleView = view.findViewById(R.id.category_recyclerview);
    LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
    layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
    categoryRecycleView.setLayoutManager(layoutManager);

    final List<CategoryModal> categoryModalList = new ArrayList<CategoryModal>();
    categoryModalList.add(new CategoryModal("link","Home"));
    categoryModalList.add(new CategoryModal("link","Home1"));
    categoryModalList.add(new CategoryModal("link","Home2"));
    categoryModalList.add(new CategoryModal("link","Home3"));
    categoryModalList.add(new CategoryModal("link","Home4"));
    categoryModalList.add(new CategoryModal("link","Home5"));
    categoryModalList.add(new CategoryModal("link","Home6"));
    categoryModalList.add(new CategoryModal("link","Home7"));
    categoryModalList.add(new CategoryModal("link","Home8"));
    categoryModalList.add(new CategoryModal("link","Home9"));

    categoryAdapter = new CategoryAdapter(categoryModalList);
    categoryRecycleView.setAdapter(categoryAdapter);
    categoryAdapter.notifyDataSetChanged();

0 个答案:

没有答案