适用于新活动的Android可扩展Recyclerview arraylist

时间:2017-01-09 20:43:07

标签: android arraylist android-recyclerview

我使用可扩展的recyclerview作为一种日历,当点击该项目时,它会打开一个标签布局

我一直在尝试使用不同的方法从onitemclick监听器打开一个活动,但一直没有成功。我是开发新手,我想我会尝试堆栈寻求帮助。

我会很乐意帮助你。

这是代码

    public class EventActivity extends AppCompatActivity implements   ItemClickListener {

    RecyclerView mRecyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_event);

    mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
    SectionedExpandableLayoutHelper sectionedExpandableLayoutHelper = new   SectionedExpandableLayoutHelper(this,
            mRecyclerView, this, 3);


    ArrayList<Item> arrayList = new ArrayList<>();
    arrayList.add(new Item("March 7", 0));
    arrayList.add(new Item("March 8", 1));
    arrayList.add(new Item("March 9", 2));
    arrayList.add(new Item("March 10", 3));
    arrayList.add(new Item("March 11", 4));
    arrayList.add(new Item("March 12", 5));
    sectionedExpandableLayoutHelper.addSection("Week One", arrayList);
    arrayList = new ArrayList<>();
    arrayList.add(new Item("March 13", 7));
    arrayList.add(new Item("March 14", 8));
    arrayList.add(new Item("March 15", 8));
    arrayList.add(new Item("March 16", 10));
    arrayList.add(new Item("March 17", 11));
    arrayList.add(new Item("March 18", 12));
    sectionedExpandableLayoutHelper.addSection("Week Two", arrayList);
    arrayList = new ArrayList<>();
    arrayList.add(new Item("March 19", 13));
    arrayList.add(new Item("March 20", 14));
    arrayList.add(new Item("March 21", 15));
    arrayList.add(new Item("March 22", 16));
    arrayList.add(new Item("March 23", 18));
    arrayList.add(new Item("March 25", 19));
    sectionedExpandableLayoutHelper.addSection("Week Three", arrayList);

    sectionedExpandableLayoutHelper.notifyDataSetChanged();




    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
     }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();


    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
    }

    @Override
    public void itemClicked(Item item) {


    }


    @Override
    public void itemClicked(Section section) {

    }
    }

    Here are the adapters

    ItemClickListener interface

    public interface ItemClickListener {
    void itemClicked(Item item);
    void itemClicked(Section section);
     }

    Section

    public class Section {

    private final String name;

    public boolean isExpanded;

    public Section(String name) {
    this.name = name;
    isExpanded = true;
    }

    public String getName() {
    return name;
    }
    }

    Sectioned Expandable Grid Adapter

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

    //data array
    private ArrayList<Object> mDataArrayList;

    //context
    private final Context mContext;

    //listeners
    private final ItemClickListener mItemClickListener;
    private final SectionStateChangeListener mSectionStateChangeListener;

    //view type
    private static final int VIEW_TYPE_SECTION = R.layout.layout_section;
    private static final int VIEW_TYPE_ITEM = R.layout.layout_item; 

    public SectionedExpandableGridAdapter(Context context, ArrayList<Object>
    dataArrayList,
                                      final GridLayoutManager
    gridLayoutManager, ItemClickListener itemClickListener,
                                      SectionStateChangeListener
    sectionStateChangeListener) {
    mContext = context;
    mItemClickListener = itemClickListener;
    mSectionStateChangeListener = sectionStateChangeListener;
    mDataArrayList = dataArrayList;

    gridLayoutManager.setSpanSizeLookup(new
    GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            return isSection(position)?gridLayoutManager.getSpanCount():1;
        }
      });
     }

    private boolean isSection(int position) {
    return mDataArrayList.get(position) instanceof Section;
     }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    return new ViewHolder(LayoutInflater.from(mContext).inflate(viewType,
    parent, false), viewType);
    }


     @Override
     public void onBindViewHolder(ViewHolder holder, int position) {
     switch (holder.viewType) {
        case VIEW_TYPE_ITEM :
            final Item item = (Item) mDataArrayList.get(position);
            holder.itemTextView.setText(item.getName());
            holder.view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mItemClickListener.itemClicked(item);
                }
            });
            break;
        case VIEW_TYPE_SECTION :
            final Section section = (Section) mDataArrayList.get(position);
            holder.sectionTextView.setText(section.getName());
            holder.sectionTextView.setOnClickListener(new
         View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mItemClickListener.itemClicked(section);
                }
            });
            holder.sectionToggleButton.setChecked(section.isExpanded);
            holder.sectionToggleButton.setOnCheckedChangeListener(new
            CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView,
             boolean isChecked) {


             mSectionStateChangeListener.onSectionStateChanged(section,
            isChecked);
                }
            });
            break;
            }
           }

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

      @Override
      public int getItemViewType(int position) {
      if (isSection(position))
        return VIEW_TYPE_SECTION;
      else return VIEW_TYPE_ITEM;
       }

       protected static class ViewHolder extends RecyclerView.ViewHolder {

       //common
       View view;
       int viewType;

       //for section
       TextView sectionTextView;
       ToggleButton sectionToggleButton;

       //for item
       TextView itemTextView;

       public ViewHolder(View view, int viewType) {
        super(view);
        this.viewType = viewType;
        this.view = view;
        if (viewType == VIEW_TYPE_ITEM) {
            itemTextView = (TextView) view.findViewById(R.id.text_item);
        } else {
            sectionTextView = (TextView)
        view.findViewById(R.id.text_section);
            sectionToggleButton = (ToggleButton) 
        view.findViewById(R.id.toggle_button_section);
        }
        }
        }
        }

      Sectioned Expandable Layout Helper

       public class SectionedExpandableLayoutHelper implements
      SectionStateChangeListener {

      //data list
      private LinkedHashMap<Section, ArrayList<Item>> mSectionDataMap = new
     LinkedHashMap<Section, ArrayList<Item>>();
     private ArrayList<Object> mDataArrayList = new ArrayList<Object>();

      //section map
      private HashMap<String, Section> mSectionMap = new HashMap<String,
      Section>();

      //adapter
     private SectionedExpandableGridAdapter mSectionedExpandableGridAdapter;

     //recycler view
     RecyclerView mRecyclerView;

     public SectionedExpandableLayoutHelper(Context context, RecyclerView 
     recyclerView, ItemClickListener itemClickListener,
                                       int gridSpanCount) {

     //setting the recycler view
     GridLayoutManager gridLayoutManager = new GridLayoutManager(context,
     gridSpanCount);
     recyclerView.setLayoutManager(gridLayoutManager);
     mSectionedExpandableGridAdapter = new 
     SectionedExpandableGridAdapter(context, mDataArrayList,
            gridLayoutManager, itemClickListener, this);
     recyclerView.setAdapter(mSectionedExpandableGridAdapter);

     mRecyclerView = recyclerView;
     }

    public void notifyDataSetChanged() {

    generateDataList();
    mSectionedExpandableGridAdapter.notifyDataSetChanged();
    }

    public void addSection(String section, ArrayList<Item> items) {
    Section newSection;
    mSectionMap.put(section, (newSection = new Section(section)));
    mSectionDataMap.put(newSection, items);
     }

    public void addItem(String section, Item item) {
    mSectionDataMap.get(mSectionMap.get(section)).add(item);
    }

    public void removeItem(String section, Item item) {
    mSectionDataMap.get(mSectionMap.get(section)).remove(item);
    }

    public void removeSection(String section) {
    mSectionDataMap.remove(mSectionMap.get(section));
    mSectionMap.remove(section);
     }

    private void generateDataList () {
    mDataArrayList.clear();
    for (Map.Entry<Section, ArrayList<Item>> entry : 
    mSectionDataMap.entrySet()) {
        Section key;
        mDataArrayList.add((key = entry.getKey()));
        if (key.isExpanded)
            mDataArrayList.addAll(entry.getValue());
     }
     }

     @Override
    public void onSectionStateChanged(Section section, boolean isOpen) {
    if (!mRecyclerView.isComputingLayout()) {
        section.isExpanded = isOpen;
        notifyDataSetChanged();
    }
    }
    }

     Section State Interface

     public interface SectionStateChangeListener {
     void onSectionStateChanged(Section section, boolean isOpen);
      }

     Models

      Item
     public class Item {

     private final String name;
     private final int id;

     public Item(String name, int id) {
     this.name = name;
     this.id = id;
     }

     public int getId() {
     return id;
     }

     public String getName() {
    return name;
     }
     }






    here is the xml 

    event layout 

    <RelativeLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="#E0F2F1"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/recycler_view"/>


    </RelativeLayout>

    Layout Item

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:elevation="5dp"
    android:layout_margin="@dimen/item_margin"
    android:layout_width="wrap_content"
    android:layout_height="@dimen/item_height">

    <TextView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:id="@+id/text_item"
    android:gravity="center"
    android:layout_gravity="center" />

    </android.support.v7.widget.CardView>

    layout section

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    app:elevation="5dp"
    android:layout_marginTop="7dp"
    android:layout_height="wrap_content">

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#26A69A"
    android:orientation="horizontal">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/section_padding"
        android:paddingBottom="@dimen/section_padding"
        android:paddingStart="@dimen/section_text_padding_left"
        android:background="@drawable/theme_color"
        android:textColor="#fff"
        android:textSize="@dimen/section_text_size"
        android:id="@+id/text_section"
        android:layout_weight="0.12"/>

    <ToggleButton
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/toggle_button_section"
       android:contentDescription="@string/image_button_content_description"
        android:background="@drawable/selector_section_toggle"
        android:padding="@dimen/section_padding"
        android:textOn=""
        android:textOff=""
        android:layout_weight="0.88"/>

       </LinearLayout>

       </android.support.v7.widget.CardView>

0 个答案:

没有答案