如何从这个RecyclerView开始活动?

时间:2015-09-07 03:26:48

标签: android android-intent android-activity android-recyclerview

我有一个充满元素的RecyclerView,在这种情况下是CardViews,我也有一个开关,根据状态对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);
    }
}

我将非常感谢有关如何执行此操作的任何说明。 万分感谢。

3 个答案:

答案 0 :(得分:2)

RecyclerView项目点击集开始新的活动,onClick监听器在onCreateViewHolder中查看行的根视图:

@Override
  public CategoriesViewHolder onCreateViewHolder(ViewGroup viewGroup,int i){
         View itemView =....;
         itemView.setClickable(true);
         itemView.setOnClickListener(new View.OnClickListener()
          {
           @Override
            public void onClick(View view)
            {
              // starte new Activity here
            }
         });
        return new CategoriesViewHolder(itemView);
    }

如果category_row布局中的子视图可调焦,则为每个视图设置android:duplicateParentState="true"

答案 1 :(得分:1)

设置ViewHolder以实现View.OnClickListener。在onClick()方法中,您可以调用ViewHolder.getAdapterPosition()并检查该位置的项目应该启动哪个活动。

class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    ViewHolder(View itemView) {
        super(itemView);
        itemView.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int position = getAdapterPosition();

        // Logic to start correct activity class based on position
    }
}

这样做可确保您只在ViewHolder创建时创建新的侦听器。如果在onBindView()中创建新的侦听器,则每次滚动时都会创建新的对象。

另一种选择是使用不同的View类型。设置适配器以在getItemViewType()中返回不同的整数,具体取决于onClick中的行为。然后,在onCreateViewHolder()中,您可以添加OnClickListener并使用该整数(作为viewType参数传递)来确定其行为。但是,如果您的所有列表项都启动了不同的活动,那么这意味着您需要为每个项目单独的视图类型,然后它们不会非常有效地回收。

答案 2 :(得分:0)

在onBindViewHolder中执行。

Fragment