package com.trydroid.demo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;

public class RecylcerViewDemoActivity extends AppCompatActivity implements View.OnClickListener {

    private RecyclerViewAdapter mAdapter;

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

        Button loadButton = (Button) findViewById(R.id.loading_btn);
        Button addButton = (Button) findViewById(R.id.add_btn);
        Button clearButton = (Button) findViewById(R.id.clear_btn);
        Button errorButton = (Button) findViewById(R.id.error_btn);

        loadButton.setOnClickListener(this);
        addButton.setOnClickListener(this);
        clearButton.setOnClickListener(this);
        errorButton.setOnClickListener(this);

        setupRecylcerView();
    }

    private void setupRecylcerView() {
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);

        // use this setting to improve performance if you know that changes
        // in content do not change the layout size of the RecyclerView
        recyclerView.setHasFixedSize(true);

        // use a linear layout manager
        RecyclerView.LayoutManager layoutManager = new GridLayoutManager(this, 3);
        recyclerView.setLayoutManager(layoutManager);

        // specify an adapter (see also next example)
        mAdapter = new RecyclerViewAdapter(this);
        recyclerView.setAdapter(mAdapter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.loading_btn:
                mAdapter.load();
                break;

            case R.id.add_btn:
                mAdapter.addAll(Utils.buildDataSet());
                break;

            case R.id.clear_btn:
                mAdapter.clear();
                break;

            case R.id.error_btn:
                mAdapter.error();
                break;
        }
    }
}

@@ -21,16 +21,48 @@
 .demo;%0A%0A
+import android.content.Context;%0A
 import a
@@ -79,16 +79,16 @@
 Bundle;%0A
-
 import a
@@ -1458,21 +1458,20 @@
     
-RecyclerView.
+AutoSpanGrid
 Layo
@@ -1500,16 +1500,24 @@
 r = new 
+AutoSpan
 GridLayo
@@ -1536,9 +1536,9 @@
 is, 
-3
+2
 );%0A 
@@ -2178,24 +2178,24 @@
 er.error();%0A
-
             
@@ -2220,11 +2220,857 @@
  %7D%0A    %7D
+%0A%0A    public static class AutoSpanGridLayoutManager extends GridLayoutManager %7B%0A%0A        private static final int DEFAULT_SPAN_SIZE = 1;%0A        private int mSpanCount;%0A%0A        public AutoSpanGridLayoutManager(Context context, int spanCount) %7B%0A            super(context, DEFAULT_SPAN_SIZE);%0A            mSpanCount = spanCount;%0A        %7D%0A%0A        public AutoSpanGridLayoutManager(Context context, int spanCount, int orientation, boolean reverseLayout) %7B%0A            super(context, DEFAULT_SPAN_SIZE, orientation, reverseLayout);%0A            mSpanCount = spanCount;%0A        %7D%0A%0A        @Override%0A        public void onItemsChanged(RecyclerView recyclerView) %7B%0A            int span = getItemCount() == DEFAULT_SPAN_SIZE ? DEFAULT_SPAN_SIZE : mSpanCount;%0A            setSpanCount(span);%0A            super.onItemsChanged(recyclerView);%0A        %7D%0A    %7D
 %0A%7D%0A

add AutoSpanGridLayoutManager to support all State for RecyclerView