Search in sources :

Example 16 with Adapter

use of android.support.v7.widget.RecyclerView.Adapter in project FastAdapter by mikepenz.

the class RealmActivity method onCreate.

@Override
protected void onCreate(final Bundle savedInstanceState) {
    findViewById(android.R.id.content).setSystemUiVisibility(findViewById(android.R.id.content).getSystemUiVisibility() | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // Handle Toolbar
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setTitle(R.string.sample_realm_list);
    //style our ui
    new MaterializeBuilder().withActivity(this).build();
    //create our FastAdapter which will manage everything
    mFastItemAdapter = new FastItemAdapter<>();
    //configure our fastAdapter
    mFastItemAdapter.withOnClickListener(new FastAdapter.OnClickListener<RealmSampleUserItem>() {

        @Override
        public boolean onClick(View v, IAdapter<RealmSampleUserItem> adapter, RealmSampleUserItem item, int position) {
            Toast.makeText(v.getContext(), item.getName(), Toast.LENGTH_SHORT).show();
            return false;
        }
    });
    //get our recyclerView and do basic setup
    RecyclerView rv = (RecyclerView) findViewById(R.id.rv);
    rv.setLayoutManager(new LinearLayoutManager(this));
    rv.setItemAnimator(new AlphaInAnimator());
    rv.setAdapter(mFastItemAdapter);
    //Get a realm instance for this activity
    mRealm = Realm.getDefaultInstance();
    //Add a realm on change listener (donĀ“t forget to close this realm instance before adding this listener again)
    mRealm.where(RealmSampleUserItem.class).findAllAsync().addChangeListener(new RealmChangeListener<RealmResults<RealmSampleUserItem>>() {

        @Override
        public void onChange(RealmResults<RealmSampleUserItem> userItems) {
            //This will call twice
            //1.) from findAllAsync()
            //2.) from createData()
            mFastItemAdapter.setNewList(userItems);
        }
    });
    //fill with some sample data
    createData();
    //set the back arrow in the toolbar
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(false);
    //restore selections (this has to be done after the items were added
    mFastItemAdapter.withSavedInstanceState(savedInstanceState);
}
Also used : LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) MaterializeBuilder(com.mikepenz.materialize.MaterializeBuilder) RecyclerView(android.support.v7.widget.RecyclerView) View(android.view.View) AlphaInAnimator(com.mikepenz.itemanimators.AlphaInAnimator) RealmSampleUserItem(com.mikepenz.fastadapter.app.items.RealmSampleUserItem) RecyclerView(android.support.v7.widget.RecyclerView) FastAdapter(com.mikepenz.fastadapter.FastAdapter) Toolbar(android.support.v7.widget.Toolbar) RealmResults(io.realm.RealmResults)

Example 17 with Adapter

use of android.support.v7.widget.RecyclerView.Adapter in project FastAdapter by mikepenz.

the class SimpleItemListActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    findViewById(android.R.id.content).setSystemUiVisibility(findViewById(android.R.id.content).getSystemUiVisibility() | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sample);
    // Handle Toolbar
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    //style our ui
    new MaterializeBuilder().withActivity(this).build();
    //create our FastAdapter which will manage everything
    fastItemAdapter = new FastItemAdapter<>();
    fastItemAdapter.withSelectable(true);
    fastItemAdapter.withPositionBasedStateManagement(false);
    final FastScrollIndicatorAdapter<SimpleItem> fastScrollIndicatorAdapter = new FastScrollIndicatorAdapter<>();
    //configure our fastAdapter
    fastItemAdapter.withOnClickListener(new FastAdapter.OnClickListener<SimpleItem>() {

        @Override
        public boolean onClick(View v, IAdapter<SimpleItem> adapter, SimpleItem item, int position) {
            Toast.makeText(v.getContext(), (item).name.getText(v.getContext()), Toast.LENGTH_LONG).show();
            return false;
        }
    });
    //configure the itemAdapter
    fastItemAdapter.withFilterPredicate(new IItemAdapter.Predicate<SimpleItem>() {

        @Override
        public boolean filter(SimpleItem item, CharSequence constraint) {
            //return false to keep it
            return !item.name.getText().toLowerCase().contains(constraint.toString().toLowerCase());
        }
    });
    fastItemAdapter.getItemAdapter().withItemFilterListener(this);
    //get our recyclerView and do basic setup
    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rv);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setAdapter(fastScrollIndicatorAdapter.wrap(fastItemAdapter));
    //add a FastScrollBar (Showcase compatibility)
    //DragScrollBar materialScrollBar = new DragScrollBar(this, recyclerView, true);
    //materialScrollBar.setHandleColour(ContextCompat.getColor(this, R.color.accent));
    //materialScrollBar.addIndicator(new AlphabetIndicator(this), true);
    //fill with some sample data
    int x = 0;
    List<SimpleItem> items = new ArrayList<>();
    for (String s : ALPHABET) {
        int count = new Random().nextInt(20);
        for (int i = 1; i <= count; i++) {
            SimpleItem item = new SimpleItem().withName(s + " Test " + x).withIdentifier(100 + x);
            items.add(item);
            x++;
        }
    }
    fastItemAdapter.add(items);
    //add drag and drop for item
    touchCallback = new SimpleDragCallback(this);
    // Create ItemTouchHelper and pass with parameter the SimpleDragCallback
    touchHelper = new ItemTouchHelper(touchCallback);
    // Attach ItemTouchHelper to RecyclerView
    touchHelper.attachToRecyclerView(recyclerView);
    //restore selections (this has to be done after the items were added
    fastItemAdapter.withSavedInstanceState(savedInstanceState);
    //set the back arrow in the toolbar
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(false);
}
Also used : FastScrollIndicatorAdapter(com.mikepenz.fastadapter.app.adapters.FastScrollIndicatorAdapter) SimpleDragCallback(com.mikepenz.fastadapter_extensions.drag.SimpleDragCallback) ArrayList(java.util.ArrayList) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) MaterializeBuilder(com.mikepenz.materialize.MaterializeBuilder) SearchView(android.support.v7.widget.SearchView) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView) DefaultItemAnimator(android.support.v7.widget.DefaultItemAnimator) ItemTouchHelper(android.support.v7.widget.helper.ItemTouchHelper) Random(java.util.Random) IItemAdapter(com.mikepenz.fastadapter.IItemAdapter) RecyclerView(android.support.v7.widget.RecyclerView) FastAdapter(com.mikepenz.fastadapter.FastAdapter) SimpleItem(com.mikepenz.fastadapter.app.items.SimpleItem) Toolbar(android.support.v7.widget.Toolbar)

Example 18 with Adapter

use of android.support.v7.widget.RecyclerView.Adapter in project FastAdapter by mikepenz.

the class AdvancedSampleActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    findViewById(android.R.id.content).setSystemUiVisibility(findViewById(android.R.id.content).getSystemUiVisibility() | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
    //as we use an icon from Android-Iconics via xml we add the IconicsLayoutInflater
    //https://github.com/mikepenz/Android-Iconics
    LayoutInflaterCompat.setFactory(getLayoutInflater(), new IconicsLayoutInflater(getDelegate()));
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sample);
    // Handle Toolbar
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setTitle(R.string.sample_advanced);
    //style our ui
    new MaterializeBuilder().withActivity(this).build();
    //create our FastAdapter
    mFastAdapter = new FastAdapter<>();
    //we init our ActionModeHelper
    mActionModeHelper = new ActionModeHelper(mFastAdapter, R.menu.cab, new ActionBarCallBack());
    mActionModeHelper.withSupportSubItems(true);
    //create our adapters
    final StickyHeaderAdapter stickyHeaderAdapter = new StickyHeaderAdapter();
    mItemAdapter = new ItemAdapter<>();
    mHeaderAdapter = new HeaderAdapter<>();
    //configure our mFastAdapter
    //as we provide id's for the items we want the hasStableIds enabled to speed up things
    mFastAdapter.withSelectable(true);
    mFastAdapter.withMultiSelect(true);
    mFastAdapter.withSelectOnLongClick(true);
    mFastAdapter.withPositionBasedStateManagement(false);
    mFastAdapter.withOnPreClickListener(new FastAdapter.OnClickListener<IItem>() {

        @Override
        public boolean onClick(View v, IAdapter adapter, IItem item, int position) {
            //we handle the default onClick behavior for the actionMode. This will return null if it didn't do anything and you can handle a normal onClick
            Boolean res = mActionModeHelper.onClick(item);
            return res != null ? res : false;
        }
    });
    mFastAdapter.withOnPreLongClickListener(new FastAdapter.OnLongClickListener<IItem>() {

        @Override
        public boolean onLongClick(View v, IAdapter adapter, IItem item, int position) {
            //we do not want expandable items to be selected
            if (item instanceof IExpandable) {
                if (((IExpandable) item).getSubItems() != null) {
                    return true;
                }
            }
            //handle the longclick actions
            ActionMode actionMode = mActionModeHelper.onLongClick(AdvancedSampleActivity.this, position);
            if (actionMode != null) {
                //we want color our CAB
                findViewById(R.id.action_mode_bar).setBackgroundColor(UIUtils.getThemeColorFromAttrOrRes(AdvancedSampleActivity.this, R.attr.colorPrimary, R.color.material_drawer_primary));
            }
            //if we have no actionMode we do not consume the event
            return actionMode != null;
        }
    });
    //get our recyclerView and do basic setup
    RecyclerView rv = (RecyclerView) findViewById(R.id.rv);
    rv.setLayoutManager(new LinearLayoutManager(this));
    rv.setItemAnimator(new DefaultItemAnimator());
    rv.setAdapter(stickyHeaderAdapter.wrap(mItemAdapter.wrap(mHeaderAdapter.wrap(mFastAdapter))));
    final StickyRecyclerHeadersDecoration decoration = new StickyRecyclerHeadersDecoration(stickyHeaderAdapter);
    rv.addItemDecoration(decoration);
    //so the headers are aware of changes
    stickyHeaderAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {

        @Override
        public void onChanged() {
            decoration.invalidateHeaders();
        }
    });
    //init cache with the added items, this is useful for shorter lists with many many different view types (at least 4 or more
    //new RecyclerViewCacheUtil().withCacheSize(2).apply(rv, items);
    //set the back arrow in the toolbar
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(false);
    //we define the items
    setItems();
    //restore selections (this has to be done after the items were added
    mFastAdapter.withSavedInstanceState(savedInstanceState);
}
Also used : LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) DefaultItemAnimator(android.support.v7.widget.DefaultItemAnimator) StickyHeaderAdapter(com.mikepenz.fastadapter.app.adapters.StickyHeaderAdapter) IItem(com.mikepenz.fastadapter.IItem) IAdapter(com.mikepenz.fastadapter.IAdapter) Toolbar(android.support.v7.widget.Toolbar) IExpandable(com.mikepenz.fastadapter.IExpandable) MaterializeBuilder(com.mikepenz.materialize.MaterializeBuilder) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView) StickyRecyclerHeadersDecoration(com.timehop.stickyheadersrecyclerview.StickyRecyclerHeadersDecoration) ActionMode(android.support.v7.view.ActionMode) RecyclerView(android.support.v7.widget.RecyclerView) FastAdapter(com.mikepenz.fastadapter.FastAdapter) IconicsLayoutInflater(com.mikepenz.iconics.context.IconicsLayoutInflater) ActionModeHelper(com.mikepenz.fastadapter_extensions.ActionModeHelper)

Example 19 with Adapter

use of android.support.v7.widget.RecyclerView.Adapter in project FastAdapter by mikepenz.

the class CheckBoxSampleActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    findViewById(android.R.id.content).setSystemUiVisibility(findViewById(android.R.id.content).getSystemUiVisibility() | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sample);
    // Handle Toolbar
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    //style our ui
    new MaterializeBuilder().withActivity(this).build();
    //create our FastAdapter which will manage everything
    fastItemAdapter = new FastItemAdapter<>();
    fastItemAdapter.withSelectable(true);
    //configure our fastAdapter
    fastItemAdapter.withOnClickListener(new FastAdapter.OnClickListener<CheckBoxSampleItem>() {

        @Override
        public boolean onClick(View v, IAdapter<CheckBoxSampleItem> adapter, CheckBoxSampleItem item, int position) {
            Toast.makeText(v.getContext(), (item).name.getText(v.getContext()), Toast.LENGTH_LONG).show();
            return false;
        }
    });
    //init the ClickListenerHelper which simplifies custom click listeners on views of the Adapter
    mClickListenerHelper = new ClickListenerHelper<>(fastItemAdapter);
    fastItemAdapter.withOnPreClickListener(new FastAdapter.OnClickListener<CheckBoxSampleItem>() {

        @Override
        public boolean onClick(View v, IAdapter<CheckBoxSampleItem> adapter, CheckBoxSampleItem item, int position) {
            // consume otherwise radio/checkbox will be deselected
            return true;
        }
    });
    fastItemAdapter.withItemEvent(new CheckBoxSampleItem.CheckBoxClickEvent());
    //get our recyclerView and do basic setup
    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rv);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setAdapter(fastItemAdapter);
    //fill with some sample data
    int x = 0;
    List<CheckBoxSampleItem> items = new ArrayList<>();
    for (String s : ALPHABET) {
        int count = new Random().nextInt(20);
        for (int i = 1; i <= count; i++) {
            items.add(new CheckBoxSampleItem().withName(s + " Test " + x).withIdentifier(100 + x));
            x++;
        }
    }
    fastItemAdapter.add(items);
    //restore selections (this has to be done after the items were added
    fastItemAdapter.withSavedInstanceState(savedInstanceState);
    //set the back arrow in the toolbar
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(false);
}
Also used : CheckBoxSampleItem(com.mikepenz.fastadapter.app.items.CheckBoxSampleItem) ArrayList(java.util.ArrayList) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) MaterializeBuilder(com.mikepenz.materialize.MaterializeBuilder) RecyclerView(android.support.v7.widget.RecyclerView) View(android.view.View) DefaultItemAnimator(android.support.v7.widget.DefaultItemAnimator) Random(java.util.Random) RecyclerView(android.support.v7.widget.RecyclerView) FastAdapter(com.mikepenz.fastadapter.FastAdapter) Toolbar(android.support.v7.widget.Toolbar)

Example 20 with Adapter

use of android.support.v7.widget.RecyclerView.Adapter in project Android-Iconics by mikepenz.

the class IconsFragment method configAdapter.

private void configAdapter() {
    //our popup on touch
    mAdapter.withOnTouchListener(new FastAdapter.OnTouchListener<IconItem>() {

        @Override
        public boolean onTouch(View v, MotionEvent motionEvent, IAdapter<IconItem> adapter, IconItem item, int position) {
            int a = motionEvent.getAction();
            if (a == MotionEvent.ACTION_DOWN) {
                if (popup != null && popup.isShowing()) {
                    popup.dismiss();
                }
                IconicsDrawable icon = new IconicsDrawable(v.getContext()).icon(item.getIcon()).sizeDp(144).paddingDp(8).backgroundColor(Color.parseColor("#DDFFFFFF")).roundedCornersDp(12);
                ImageView imageView = new ImageView(v.getContext());
                imageView.setImageDrawable(icon);
                int size = (int) UIUtils.convertDpToPixel(144, v.getContext());
                popup = new PopupWindow(imageView, size, size);
                popup.showAsDropDown(v);
                //copy to clipboard
                if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
                    android.text.ClipboardManager clipboard = (android.text.ClipboardManager) v.getContext().getSystemService(Context.CLIPBOARD_SERVICE);
                    clipboard.setText(icon.getIcon().getFormattedName());
                } else {
                    android.content.ClipboardManager clipboard = (android.content.ClipboardManager) v.getContext().getSystemService(Context.CLIPBOARD_SERVICE);
                    android.content.ClipData clip = android.content.ClipData.newPlainText("Android-Iconics icon", icon.getIcon().getFormattedName());
                    clipboard.setPrimaryClip(clip);
                }
            } else if (a == MotionEvent.ACTION_UP || a == MotionEvent.ACTION_CANCEL || a == MotionEvent.ACTION_OUTSIDE) {
                if (popup != null && popup.isShowing()) {
                    popup.dismiss();
                }
            }
            return false;
        }
    });
    mAdapter.withOnBindViewHolderListener(new FastAdapter.OnBindViewHolderListener() {

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position, List payloads) {
            IconItem.ViewHolder holder = (IconItem.ViewHolder) viewHolder;
            //as we overwrite the default listener
            mAdapter.getItem(position).bindView(holder, payloads);
            if (randomize) {
                holder.image.setColorRes(getRandomColor(position));
                holder.image.setPaddingDp(random.nextInt(12));
                holder.image.setContourWidthDp(random.nextInt(2));
                holder.image.setContourColor(getRandomColor(position - 2));
                int y = random.nextInt(10);
                if (y % 4 == 0) {
                    holder.image.setBackgroundColorRes(getRandomColor(position - 4));
                    holder.image.setRoundedCornersDp(2 + random.nextInt(10));
                }
            }
        }

        @Override
        public void unBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
            IconItem item = mAdapter.getItem(position);
            if (item != null) {
                item.unbindView((IconItem.ViewHolder) viewHolder);
            }
        }
    });
}
Also used : PopupWindow(android.widget.PopupWindow) ImageView(android.widget.ImageView) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView) MotionEvent(android.view.MotionEvent) IconItem(com.mikepenz.iconics.sample.item.IconItem) RecyclerView(android.support.v7.widget.RecyclerView) AbstractList(java.util.AbstractList) ArrayList(java.util.ArrayList) List(java.util.List) FastAdapter(com.mikepenz.fastadapter.FastAdapter) ImageView(android.widget.ImageView) Context(android.content.Context) IconicsDrawable(com.mikepenz.iconics.IconicsDrawable)

Aggregations

RecyclerView (android.support.v7.widget.RecyclerView)688 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)612 View (android.view.View)593 TextView (android.widget.TextView)245 ArrayList (java.util.ArrayList)179 Intent (android.content.Intent)148 ImageView (android.widget.ImageView)132 Toolbar (android.support.v7.widget.Toolbar)118 GridLayoutManager (android.support.v7.widget.GridLayoutManager)111 AdapterView (android.widget.AdapterView)109 ViewGroup (android.view.ViewGroup)97 AlertDialog (android.support.v7.app.AlertDialog)91 Bundle (android.os.Bundle)85 ListView (android.widget.ListView)85 BindView (butterknife.BindView)85 Nullable (android.support.annotation.Nullable)78 DialogInterface (android.content.DialogInterface)71 Context (android.content.Context)65 ArrayAdapter (android.widget.ArrayAdapter)65 LayoutInflater (android.view.LayoutInflater)64