xyz.valentin.marineoh.adapters.LineRecyclerViewAdapter.java Source code

Java tutorial

Introduction

Here is the source code for xyz.valentin.marineoh.adapters.LineRecyclerViewAdapter.java

Source

/*
 * MIT License
 *
 * Copyright (c) 2016 - Valentin Polo
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package xyz.valentin.marineoh.adapters;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.Build;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import butterknife.Bind;
import butterknife.ButterKnife;
import xyz.valentin.marineoh.R;
import xyz.valentin.marineoh.fragments.LineFragment.OnListFragmentInteractionListener;
import xyz.valentin.marineoh.models.MarineohWebservice.Line;

import java.util.List;

public class LineRecyclerViewAdapter extends RecyclerView.Adapter<LineRecyclerViewAdapter.ViewHolder> {
    private final List<Line> mLines;
    private final OnListFragmentInteractionListener mListener;
    private Context mContext;

    public LineRecyclerViewAdapter(List<Line> items, OnListFragmentInteractionListener listener) {
        mLines = items;
        mListener = listener;
    }

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

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        holder.mLine = mLines.get(position);

        holder.mLineNameButton.setText(mLines.get(position).getName());
        holder.mLineNameButton
                .setBackgroundTintList(ColorStateList.valueOf(Color.parseColor(mLines.get(position).getColor())));
        holder.mLineNameButton.setTextColor(Color.parseColor(mLines.get(position).getTextColor()));

        holder.mLineNameButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != mListener) {
                    mListener.onListFragmentInteraction(holder.mLine);
                }
            }
        });
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {
        public final View mView;
        public Line mLine;

        @Bind(R.id.linearLayoutContainerLineFragment)
        public LinearLayout mLinearLayoutContainer;

        @Bind(R.id.lineNameButtonLineFragment)
        public Button mLineNameButton;

        public ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            mView = view;
        }

        @Override
        public String toString() {
            return super.toString();
        }
    }

}