在RecyclerView中创建部分

时间:2015-03-12 18:12:37

标签: android android-recyclerview

我已经与listviews合作了一段时间,当我开始理解它们时,他们推出了recyclerview。我有一个包含所有项目的简单工作示例。我想使用相同的布局在recyclerview中创建部分。例如,每个部分都按数据(早餐,午餐,晚餐)进行过滤。我如何在recyclerview中创建部分?如果您需要更多信息,或者认为这是一个糟糕的问题,我会更新我的问题或提供更多代码。

我如何通过早餐,午餐和晚餐过滤适配器:

FoodAdapter mAdapterBreakfast = new FoodAdapter(LogSet.filterBy("Breakfast"), R.layout.row, this);
FoodAdapter mAdapterLunch = new FoodAdapter(LogSet.filterBy("Lunch"), R.layout.row, this);
FoodAdapter mAdapterDinner = new FoodAdapter(LogSet.filterBy("Dinner"), R.layout.row, this);

我如何获得适配器中的所有项目:

FoodAdapter mAdapter = new FoodAdapter(LogSet.all(), R.layout.row, this);

以下是所有人的工作示例:

适配器

public class FoodAdapter extends RecyclerView.Adapter<FoodAdapter.ViewHolder> {
    private List<LogSet> meals;
    private int rowLayout;
    private Context mContext;
    View v;

    public FoodAdapter(List<LogSet> mMeals, int rowLayout, Context context) {
        this.meals = mMeals;
        this.rowLayout = rowLayout;
        this.mContext = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        v = LayoutInflater.from(viewGroup.getContext()).inflate(rowLayout, viewGroup, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int i) {
        LogSet meal = meals.get(i);
        viewHolder.countryName.setText(meal.getLiftName());
    }

    @Override
    public int getItemCount() {
        return meals == null ? 0 : meals.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView countryName;

        public ViewHolder(View itemView) {
            super(itemView);
            countryName = (TextView) itemView.findViewById(R.id.foodName);
        }
    }
}

MainActivity

public class MainActivity extends ActionBarActivity {
    RecyclerView mRecyclerView;
    Date mDate = new Date();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_testing_rec);
        mRecyclerView = (RecyclerView) findViewById(R.id.list);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        FoodAdapter mAdapter = new FoodAdapter(LogSet.all(), R.layout.row, this);
        mRecyclerView.setAdapter(mAdapter);
        /*
        FoodAdapter mAdapterBreakfast = new FoodAdapter(LogSet.filterBy("Breakfast"), R.layout.row, this);
        FoodAdapter mAdapterLunch = new FoodAdapter(LogSet.filterBy("Lunch"), R.layout.row, this);
        FoodAdapter mAdapterDinner = new FoodAdapter(LogSet.filterBy("Dinner"), R.layout.row, this);
        */
    }
}

以下是我将每个List值发送到适配器的方法:

适配器

public class FoodAdapter extends RecyclerView.Adapter<FoodAdapter.ViewHolder> {
    private List<LogSet> meals;
    private List<LogSet> breakfast;
    private List<LogSet> lunch;
    private List<LogSet> dinner;
    private int rowLayout;
    private Context mContext;
    View v;

    public FoodAdapter(List<LogSet> mMeals, List<LogSet> mBreakfast, List<LogSet> mLunch, List<LogSet> mDinner, int rowLayout, Context context) {
        this.meals = mMeals;
        this.breakfast = mBreakfast;
        this.lunch = mLunch;
        this.dinner = mDinner;
        this.rowLayout = rowLayout;
        this.mContext = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        v = LayoutInflater.from(viewGroup.getContext()).inflate(rowLayout, viewGroup, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int i) {
        LogSet meal = meals.get(i);
        viewHolder.countryName.setText(meal.getLiftName());
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView countryName;

        public ViewHolder(View itemView) {
            super(itemView);
            countryName = (TextView) itemView.findViewById(R.id.foodName);
        }
    }

    @Override
    public int getItemCount() {
        return meals == null ? 0 : meals.size();
    }
}

MainActivity 减去所有()

public class MainActivity extends ActionBarActivity {
    RecyclerView mRecyclerView;
    Date mDate = new Date();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_testing_rec);
        mRecyclerView = (RecyclerView) findViewById(R.id.list);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        FoodAdapter mAdapter = new FoodAdapter(LogSet.all(), LogSet.filterBy("Breakfast"), LogSet.filterBy("Lunch"), LogSet.filterBy("Dinner"), R.layout.row, this);
        mRecyclerView.setAdapter(mAdapter);
        /*
        FoodAdapter mAdapterBreakfast = new FoodAdapter(LogSet.filterBy("Breakfast"), R.layout.row, this);
        FoodAdapter mAdapterLunch = new FoodAdapter(LogSet.filterBy("Lunch"), R.layout.row, this);
        FoodAdapter mAdapterDinner = new FoodAdapter(LogSet.filterBy("Dinner"), R.layout.row, this);
        */
    }
}

同样,如何在RecyclerView中创建包含早餐,午餐和晚餐的部分?

1 个答案:

答案 0 :(得分:3)

您只需使用此库中的一个适配器即可实现此目的:SectionedRecyclerViewAdapter

首先创建一个Section类来对项目进行分组:

class MySection extends StatelessSection {

    String title;
    List<LogSet> list;

    public MySection(String title, List<LogSet> list) {
        // call constructor with layout resources for this Section header, footer and items 
        super(R.layout.section_header, R.layout.section_item);

        this.title = title;
        this.list = list;
    }

    @Override
    public int getContentItemsTotal() {
        return list.size(); // number of items of this section
    }

    @Override
    public RecyclerView.ViewHolder getItemViewHolder(View view) {
        // return a custom instance of ViewHolder for the items of this section
        return new MyItemViewHolder(view);
    }

    @Override
    public void onBindItemViewHolder(RecyclerView.ViewHolder holder, int position) {
        MyItemViewHolder itemHolder = (MyItemViewHolder) holder;

        // bind your view here
        itemHolder.tvItem.setText(list.get(position).getLiftName());
    }

    @Override
    public RecyclerView.ViewHolder getHeaderViewHolder(View view) {
        return new SimpleHeaderViewHolder(view);
    }

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder) {
        MyHeaderViewHolder headerHolder = (MyHeaderViewHolder) holder;

        // bind your header view here
        headerHolder.tvItem.setText(title);
    }
}

然后设置RecyclerView,其中包含3个分类类实例(早餐,午餐和晚餐):

// Create an instance of SectionedRecyclerViewAdapter 
SectionedRecyclerViewAdapter sectionAdapter = new SectionedRecyclerViewAdapter();

// Create your sections with the list of data
MySection breakfastSection = new MySection("Breakfast", LogSet.filterBy("Breakfast"));
MySection lunchSection = new MySection("Lunch", LogSet.filterBy("Lunch"));
MySection dinnerSection = new MySection("Dinner", LogSet.filterBy("Dinner"));

// Add your Sections to the adapter
sectionAdapter.addSection(breakfastSection);
sectionAdapter.addSection(lunchSection);
sectionAdapter.addSection(dinnerSection);

// Set up your RecyclerView with the SectionedRecyclerViewAdapter
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(sectionAdapter);