build.gradle (Module: app)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
apply plugin: 'com.android.application' android { compileSdkVersion 26 defaultConfig { applicationId "com.phaisarn.myapplication" minSdkVersion 21 targetSdkVersion 26 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:26.1.0' implementation 'com.android.support.constraint:constraint-layout:1.1.3' implementation 'com.android.support:design:26.1.0' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' implementation 'com.amulyakhare:com.amulyakhare.textdrawable:1.0.1' implementation 'com.yqritc:recyclerview-flexibledivider:1.4.0' } |
layout/custom_layout.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<?xml version="1.0" encoding="utf-8"?> <GridLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dp" android:layout_marginTop="10dp" android:columnCount="2"> <ImageView android:id="@+id/imageView" android:layout_width="50dp" android:layout_height="50dp" android:layout_marginRight="15dp" android:layout_rowSpan="3" android:src="@mipmap/ic_launcher" /> <TextView android:id="@+id/textView_name" android:layout_columnWeight="1" android:layout_marginBottom="5dp" android:textColor="#00b" android:textSize="20sp" /> <TextView android:id="@+id/textView_email" android:layout_columnWeight="1" android:layout_marginBottom="5dp" android:textColor="#050" android:textSize="18sp" /> <!-- <TextView android:layout_width="match_parent" android:layout_height="1dp" android:layout_columnWeight="1" android:background="#999" /> --> </GridLayout> |
content_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/content_main" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context=".MainActivity" tools:showIn="@layout/activity_main"> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v7.widget.RecyclerView> </LinearLayout> |
MyViewHolder.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com.phaisarn.myapplication; import android.support.v7.widget.RecyclerView; import android.view.View; import android.widget.ImageView; import android.widget.TextView; public class MyViewHolder extends RecyclerView.ViewHolder { public ImageView imageView; public TextView textViewName; public TextView textViewEmail; public MyViewHolder(View view) { super(view); imageView = (ImageView)view.findViewById(R.id.imageView); textViewName = (TextView)view.findViewById(R.id.textView_name); textViewEmail = (TextView)view.findViewById(R.id.textView_email); } } |
CustomAdapter.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
package com.phaisarn.myapplication; import android.content.Context; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.amulyakhare.textdrawable.TextDrawable; import com.amulyakhare.textdrawable.util.ColorGenerator; import java.util.ArrayList; public class CustomAdapter extends RecyclerView.Adapter<MyViewHolder> { private Context mContext; private ArrayList<CustomItem> mItems; public CustomAdapter(Context context, ArrayList<CustomItem> items) { mContext = context; mItems = items; } @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int position) { LayoutInflater inflater = LayoutInflater.from(mContext); View view = inflater.inflate(R.layout.custom_layout, parent, false); return new MyViewHolder(view); } @Override public void onBindViewHolder(MyViewHolder vHolder, int pos) { CustomItem item = mItems.get(pos); String letter = String.valueOf(item.name.charAt(0)); ColorGenerator generator = ColorGenerator.MATERIAL; TextDrawable drawable = TextDrawable.builder() .buildRound(letter, generator.getRandomColor()); //.buildRect(letter, generator.getRandomColor()); //.buildRoundRect(letter, generator.getRandomColor(), 50); vHolder.imageView.setImageDrawable(drawable); vHolder.textViewName.setText(item.name); vHolder.textViewEmail.setText(item.email); } @Override public int getItemCount() { return mItems.size(); } } |
CustomItem.java
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package com.phaisarn.myapplication; public class CustomItem { public int imgId; public String name; public String email; public CustomItem(int imgId, String name, String email) { this.imgId = imgId; this.name = name; this.email = email; } } |
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
package com.phaisarn.myapplication; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.View; import android.view.Menu; import android.view.MenuItem; import android.graphics.Color; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration; import java.util.ArrayList; import java.util.Arrays; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); ArrayList<CustomItem> items = new ArrayList<>(); int i = R.mipmap.ic_launcher; items.addAll(Arrays.asList( new CustomItem(i, "มานี", "manee@example.com"), new CustomItem(i, "มานะ", "mana@test.org"), new CustomItem(i, "ปิติ", "piti@example.co.xx"), new CustomItem(i, "วีระ", "weera@test.com.org"), new CustomItem(i, "ชูใจ", "chujai@demo.biz.xxx"), new CustomItem(i, "ครูไพลิน", "pailin@p1234.com") )); RecyclerView rcv = (RecyclerView) findViewById(R.id.recyclerView); CustomAdapter adapter = new CustomAdapter(this, items); rcv.setAdapter(adapter); rcv.setLayoutManager(new LinearLayoutManager(this)); /* RecyclerView.ItemDecoration itemDecor = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL); rcv.addItemDecoration(itemDecor); */ rcv.addItemDecoration( new HorizontalDividerItemDecoration.Builder(this) .color(Color.LTGRAY) .size(4) .margin(200, 0) //left, right .build()); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } } |