RecycleViewCardviewExam.zip


필요 라이브러리 프로젝트 3가지 

{ANDROID_SDK}/extras/android/support/v7/appcompat

{ANDROID_SDK}/extras/android/support/v7/recyclerview

{ANDROID_SDK}/extras/android/support/v7/card


MainActivity.java
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.Window;

public class MainActivity extends AppCompatActivity {

	private Context						mContext;

	private RecyclerView				mRecyclerView;

	private RecyclerView.Adapter		mAdapter;
	private RecyclerView.LayoutManager	mLayoutManager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// Request window feature action bar
		requestWindowFeature(Window.FEATURE_ACTION_BAR);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		// Get the application context
		mContext = getApplicationContext();

		// Change the action bar color
		getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.RED));

		// Get the widgets reference from XML layout
		mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);

		// Initialize a new String array
		String[] colors = { "Red", "Green", "Blue", "Yellow", "Magenta", "Cyan", "Orange", "Aqua", "Azure", "Beige", "Bisque", "Brown", "Coral", "Crimson"};

		mLayoutManager = new StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.VERTICAL);
		mRecyclerView.setLayoutManager(mLayoutManager);

		// Initialize a new instance of RecyclerView Adapter instance
		mAdapter = new ColorAdapter(mContext, colors);

		// Set the adapter for RecyclerView
		mRecyclerView.setAdapter(mAdapter);
	}
}
ColorAdapter.java
import java.util.Random;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


public class ColorAdapter extends RecyclerView.Adapter{
    private String[] mDataSet;
    private Context mContext;
    private Random mRandom = new Random();

    public ColorAdapter(Context context,String[] DataSet){
        mDataSet = DataSet;
        mContext = context;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{
        public TextView mTextView;
        public ViewHolder(View v){
            super(v);
            mTextView = (TextView)v.findViewById(R.id.tv);
        }
    }

    @Override
    public ColorAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
        // Create a new View
        View v = LayoutInflater.from(mContext).inflate(R.layout.custom_view,parent,false);
        ViewHolder vh = new ViewHolder(v);
        return vh;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position){
        holder.mTextView.setText(mDataSet[position]);
        // Set a random height for TextView
        holder.mTextView.getLayoutParams().height = getRandomIntInRange(250,75);
        // Set a random color for TextView background
        holder.mTextView.setBackgroundColor(getRandomHSVColor());
    }

    @Override
    public int getItemCount(){
        return mDataSet.length;
    }

    // Custom method to get a random number between a range
    protected int getRandomIntInRange(int max, int min){
        return mRandom.nextInt((max-min)+min)+min;
    }

    // Custom method to generate random HSV color
    protected int getRandomHSVColor(){
        // Generate a random hue value between 0 to 360
        int hue = mRandom.nextInt(361);
        // We make the color depth full
        float saturation = 1.0f;
        // We make a full bright color
        float value = 1.0f;
        // We avoid color transparency
        int alpha = 255;
        // Finally, generate the color
        int color = Color.HSVToColor(alpha, new float[]{hue, saturation, value});
        // Return the color
        return color;
    }
}
activity_main.xml



    
    


custom_view.xml


    

AndroidManifest.xml



    
        

    
        
            
                

                
            
        
    





Posted by MR 손
,