RecyclerView与不同的项目(组和子项目)

时间:2015-05-10 03:46:23

标签: android android-layout android-gridlayout android-recyclerview gridlayoutmanager

我正在做[]rails new testapp create create README.rdoc create Rakefile create config.ru create .gitignore create Gemfile create app create app/assets/javascripts/application.js create app/assets/stylesheets/application.css create app/controllers/application_controller.rb create app/helpers/application_helper.rb create app/views/layouts/application.html.erb create app/assets/images/.keep create app/mailers/.keep create app/models/.keep create app/controllers/concerns/.keep create app/models/concerns/.keep create bin create bin/bundle create bin/rails create bin/rake create bin/setup create config create config/routes.rb create config/application.rb create config/environment.rb create config/secrets.yml create config/environments create config/environments/development.rb create config/environments/production.rb create config/environments/test.rb create config/initializers create config/initializers/assets.rb create config/initializers/backtrace_silencers.rb create config/initializers/cookies_serializer.rb create config/initializers/filter_parameter_logging.rb create config/initializers/inflections.rb create config/initializers/mime_types.rb create config/initializers/session_store.rb create config/initializers/wrap_parameters.rb create config/locales create config/locales/en.yml create config/boot.rb create config/database.yml create db create db/seeds.rb create lib create lib/tasks create lib/tasks/.keep create lib/assets create lib/assets/.keep create log create log/.keep create public create public/404.html create public/422.html create public/500.html create public/favicon.ico create public/robots.txt create test/fixtures create test/fixtures/.keep create test/controllers create test/controllers/.keep create test/mailers create test/mailers/.keep create test/models create test/models/.keep create test/helpers create test/helpers/.keep create test/integration create test/integration/.keep create test/test_helper.rb create tmp/cache create tmp/cache/assets create vendor/assets/javascripts create vendor/assets/javascripts/.keep create vendor/assets/stylesheets create vendor/assets/stylesheets/.keep run bundle install Fetching gem metadata from https://rubygems.org/............ Fetching version metadata from https://rubygems.org/... Fetching dependency metadata from https://rubygems.org/.. Resolving dependencies... Using rake 10.4.2 Using i18n 0.7.0 Using json 1.8.2 Installing minitest 5.6.1 Using thread_safe 0.3.5 Using tzinfo 1.2.2 Using activesupport 4.2.1 Using builder 3.2.2 Using erubis 2.7.0 Using mini_portile 0.6.2 Using nokogiri 1.6.6.2 Using rails-deprecated_sanitizer 1.0.3 Using rails-dom-testing 1.0.6 Using loofah 2.0.2 Using rails-html-sanitizer 1.0.2 Using actionview 4.2.1 Using rack 1.6.1 Using rack-test 0.6.3 Using actionpack 4.2.1 Using globalid 0.3.5 Using activejob 4.2.1 Using mime-types 2.5 Using mail 2.6.3 Using actionmailer 4.2.1 Using activemodel 4.2.1 Using arel 6.0.0 Using activerecord 4.2.1 Errno::EACCES: Permission denied @ dir_s_mkdir - /Users/jeremy/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/extensions/x86_64-darwin-14/2.2.0-static/debug_inspector-0.0.2 An error occurred while installing debug_inspector (0.0.2), and Bundler cannot continue. Make sure that `gem install debug_inspector -v '0.0.2'` succeeds before bundling. run bundle exec spring binstub --all rbenv: spring: command not found The `spring' command exists in these Ruby versions: 2.2.2这将有两个不同的项目,但我不知道我该怎么做。

首先:我有RecyclerView个对象,我不知道如何将与日期相关的时间放在ArrayList中。

  • 日期 - 小组项目。

  • 次数 - 子项目。

    RecyclerView

第二:当不期望项目的位置时,如何根据适配器中的项目更改spanSize? ( Object { Date date; ArrayList times; }

2 个答案:

答案 0 :(得分:4)

要回答第二部分,要根据项目类型(组或子项)更改跨度大小,请在GridLayoutManager上设置跨度大小查找。在适配器中,实现getItemViewType()并根据位置返回视图类型。例如。 0表示组,1表示小孩,在这种情况下MyAdapter.VIEW_TYPE_GROUP将为0。

RecyclerView list = (RecyclerView) view.findViewById(
        R.id.my_fragment_my_recycler_view);
GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), 5);
layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
    @Override
    public int getSpanSize(int position) {
        if (list.getAdapter().getItemViewType(position) == MyAdapter.VIEW_TYPE_GROUP) {
            // return the number of columns so the group header takes a whole row
            return 5
        }
        // normal child item takes up 1 cell
        return 1;
    }
});

答案 1 :(得分:1)

您可以使用库SectionedRecyclerViewAdapter将项目分组,并为每个部分添加标题:

class MySection extends StatelessSection {

    String date;
    List<String> list;

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

        this.date = date;
        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));
    }

    @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(date);
    }
}

然后使用您的部分设置RecyclerView并使用GridLayoutManager更改标题的spanSize:

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

// Create your sections with the list of data
MySection date1Section = new MySection("Date 1", date1List);
MySection date2Section = new MySection("Date 2", date2List);

// Add your Sections to the adapter
sectionAdapter.addSection(date1Section);
sectionAdapter.addSection(date2Section);

// set up a gridlayoutmanager to change the spansize of the header
GridLayoutManager glm = new GridLayoutManager(getContext(), 2);
glm.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
    @Override
    public int getSpanSize(int position) {
        switch(sectionAdapter.getSectionItemViewType(position)) {
            case SectionedRecyclerViewAdapter.VIEW_TYPE_HEADER:
                return 2;
            default:
                return 1;
        }
    }
});

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