Android Open Source - RecyclerViewLib Name Adapter






From Project

Back to project page RecyclerViewLib.

License

The source code is released under:

Apache License

If you think the Android project RecyclerViewLib listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.twotoasters.recycled;
/*  w w  w. ja v a2  s  .  c o  m*/

import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;

import com.twotoasters.android.support.v7.widget.RecyclerView;

import java.util.List;
import java.util.Random;

public class NameAdapter extends RecyclerView.Adapter<NameViewHolder> {
    private List<Item> mNames;

    public NameAdapter(List<Item> names) {
        this.mNames = names;
    }

    /**
     * @param name to be added to the list in a random position.
     */
    public void addToList(Item name) {
        int position = 0;
        if (mNames.size() > 1) {
            // Put the new name in a random place.
            position = new Random().nextInt(mNames.size() - 1);
        }
        mNames.add(position, name);
        notifyItemInserted(position);
    }

    public void removeItemFromList(Item name) {
        int position = mNames.indexOf(name);
        mNames.remove(position);
        notifyItemRemoved(position);
    }


    @Override
    public NameViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.item, parent, false);
        return new NameViewHolder(view);
    }

    @Override
    public void onBindViewHolder(NameViewHolder viewHolder, final int position) {
        final Item name = mNames.get(position);
        viewHolder.textView.setText(name.toString());
        viewHolder.textView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                removeItemFromList(name);
            }
        });
    }

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




Java Source Code List

com.twotoasters.android.support.v7.widget.CardViewDelegate.java
com.twotoasters.android.support.v7.widget.CardViewEclairMr1.java
com.twotoasters.android.support.v7.widget.CardViewImpl.java
com.twotoasters.android.support.v7.widget.CardViewJellybeanMr1.java
com.twotoasters.android.support.v7.widget.CardView.java
com.twotoasters.android.support.v7.widget.DefaultItemAnimator.java
com.twotoasters.android.support.v7.widget.LinearLayoutManager.java
com.twotoasters.android.support.v7.widget.LinearSmoothScroller.java
com.twotoasters.android.support.v7.widget.PositionMap.java
com.twotoasters.android.support.v7.widget.RecyclerView.java
com.twotoasters.android.support.v7.widget.RoundRectDrawableWithShadow.java
com.twotoasters.anim.FlipDownItemAnimator.java
com.twotoasters.anim.FromTopItemAnimator.java
com.twotoasters.anim.GarageDoorItemAnimator.java
com.twotoasters.anim.PendingItemAnimator.java
com.twotoasters.anim.SlideItemAnimator.java
com.twotoasters.layoutmanager.BaseLayoutManager.java
com.twotoasters.layoutmanager.GridLayoutManager.java
com.twotoasters.recycled.ApplicationTest.java
com.twotoasters.recycled.Item.java
com.twotoasters.recycled.NameAdapter.java
com.twotoasters.recycled.NameViewHolder.java
com.twotoasters.recycled.RecycleActivity.java
com.twotoasters.recycled.factory.ItemAnimationFactory.java
com.twotoasters.recycled.factory.NameFactory.java
com.twotoasters.utils.DisplayUtils.java