ไลบรารีมีปัญหา แค่ใส่ implement ใน build.gradle (Module: app) แล้ว sync ก็ error ละ
error: duplicate value for resource 'attr/tint' with config ''.
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.yqritc:recyclerview-flexibledivider:1.4.0' implementation 'com.github.akashandroid90:imageletter:1.8@aar' } |
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" android:rowCount="3"> <com.github.akashandroid90.imageletter.MaterialLetterIcon android:id="@+id/letter_icon" android:layout_width="50dp" android:layout_height="50dp" android:layout_marginRight="15dp" android:layout_rowSpan="3" /> <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 20 |
<?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:layout_width="match_parent" android:layout_height="match_parent" android:padding="16dp" 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> |
ColorGenerator.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 |
package com.phaisarn.myapplication; import java.util.Random; public class ColorGenerator { private int[] mColors = { /* Material Colors */ 0xffe57373, 0xfff06292, 0xffba68c8, 0xff9575cd, 0xff7986cb, 0xff64b5f6, 0xff4fc3f7, 0xff4dd0e1, 0xff4db6ac, 0xff81c784, 0xffaed581, 0xffff8a65, 0xffd4e157, 0xffffd54f, 0xffffb74d, 0xffa1887f, 0xff90a4ae /* Default Colors */ /* 0xfff16364, 0xfff58559, 0xfff9a43e, 0xffe4c62e, 0xff67bf74, 0xff59a2be, 0xff2093cd, 0xffad62a7, 0xff805781 */ }; private Random mRandom; public ColorGenerator() { mRandom = new Random(System.currentTimeMillis()); } public int getRandomColor() { return mColors[mRandom.nextInt(mColors.length)]; } } |
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 50 51 52 53 54 55 56 57 58 |
package com.phaisarn.myapplication; import android.content.Context; import android.graphics.Color; import android.support.v4.content.ContextCompat; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.github.akashandroid90.imageletter.MaterialLetterIcon; import java.util.ArrayList; public class CustomAdapter extends RecyclerView.Adapter<CustomViewHolder> { private Context mContext; private ArrayList<CustomItem> mItems; public CustomAdapter(Context context, ArrayList<CustomItem> items) { mContext = context; mItems = items; } @Override public CustomViewHolder onCreateViewHolder(ViewGroup parent, int position) { LayoutInflater inflater = LayoutInflater.from(mContext); View view = inflater.inflate(R.layout.custom_layout, parent, false); return new CustomViewHolder(view); } @Override public void onBindViewHolder(CustomViewHolder vHolder, int pos) { CustomItem item = mItems.get(pos); ColorGenerator colorGen = new ColorGenerator(); if (item.imgId == 0) { String letter = String.valueOf(item.name.charAt(0)); vHolder.icon.setLetter(letter); } else { vHolder.icon.setImageDrawable(ContextCompat.getDrawable(mContext, item.imgId)); } vHolder.icon.setShapeColor(colorGen.getRandomColor()); vHolder.icon.setShapeType(MaterialLetterIcon.SHAPE_CIRCLE); vHolder.icon.setLetterColor(Color.WHITE); vHolder.icon.setLetterSize(24); vHolder.icon.setLettersNumber(1); vHolder.icon.setInitials(false); 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; } } |
CustomViewHolder.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package com.phaisarn.myapplication; import android.support.v7.widget.RecyclerView; import android.view.View; import android.widget.TextView; import com.github.akashandroid90.imageletter.MaterialLetterIcon; public class CustomViewHolder extends RecyclerView.ViewHolder { public MaterialLetterIcon icon; public TextView textViewName; public TextView textViewEmail; public CustomViewHolder(View view) { super(view); icon = (MaterialLetterIcon) view.findViewById(R.id.letter_icon); textViewName = (TextView) view.findViewById(R.id.textView_name); textViewEmail = (TextView) view.findViewById(R.id.textView_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 |
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<>(); items.addAll(Arrays.asList( new CustomItem(R.drawable.icon1, "มานี", "manee@example.com"), new CustomItem(0, "มานะ", "mana@test.org"), new CustomItem(R.drawable.icon2, "ปิติ", "piti@example.co.xx"), new CustomItem(0, "วีระ", "weera@test.com.org"), new CustomItem(0, "ชูใจ", "chujai@demo.biz.xxx"), new CustomItem(R.drawable.icon3, "ครูไพลิน", "pailin@p1234.com") )); RecyclerView rcv = (RecyclerView) findViewById(R.id.recyclerView); CustomAdapter adapter = new CustomAdapter(this, items); rcv.setAdapter(adapter); rcv.setLayoutManager(new LinearLayoutManager(this)); 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); } } |
รูปที่ใช้