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 |
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.android.support:cardview-v7:26.1.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 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 90 91 92 93 94 95 96 97 98 99 |
<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="15dp" app:cardBackgroundColor="#fafafa" app:cardCornerRadius="15dp" app:cardElevation="3dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginBottom="10dp" android:background="#aea" android:orientation="horizontal"> <TextView android:id="@+id/text_title" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="5dp" android:padding="7dp" android:text="Title" android:textColor="#00b" android:textSize="18sp" /> <ImageView android:id="@+id/img_3dots" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right|center_vertical" android:layout_margin="5dp" android:src="@drawable/ic_3dots" android:tint="#999" /> </FrameLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <ImageView android:id="@+id/img_content" android:layout_width="120dp" android:layout_height="80dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:src="@drawable/nougat" /> <TextView android:id="@+id/text_content" android:layout_width="match_parent" android:layout_height="match_parent" android:lineSpacingExtra="5dp" android:lines="3" android:maxLines="3" android:paddingRight="5dp" android:text="บรรทัด 1\nบรรทัด 2\nบรรทัด 3..." android:textColor="#000" android:textSize="16sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <RatingBar android:id="@+id/rating_bar" style="?android:attr/ratingBarStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="10dp" android:isIndicator="false" android:numStars="5" android:rating="4" android:stepSize="1" /> <TextView android:id="@+id/text_readMore" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_columnWeight="1" android:layout_margin="10dp" android:gravity="right" android:text="Read More..." android:textColor="#00b" android:textSize="14sp" android:textStyle="italic" /> </LinearLayout> </LinearLayout> </android.support.v7.widget.CardView> |
content_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?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" android:background="#777" 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> |
menu/popup.xml
1 2 3 4 5 6 7 8 9 |
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/menu1" android:title="ซ่อน" /> <item android:id="@+id/menu2" android:title="แจ้งเตือน" /> </menu> |
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 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 90 91 92 93 94 95 |
package com.phaisarn.myapplication; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.support.v4.graphics.drawable.RoundedBitmapDrawable; import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory; import android.support.v7.widget.PopupMenu; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.Toast; import java.util.ArrayList; public class CustomAdapter extends RecyclerView.Adapter<CustomHolder> { private Context mContext; private ArrayList<CustomItem> mItems; private Bitmap mBitmap; private RoundedBitmapDrawable mRoundedBitmap; public CustomAdapter(Context context, ArrayList<CustomItem> items) { mContext = context; mItems = items; } public interface OnItemClickListener { void onItemClick(View item, int position); } private OnItemClickListener mListener; public void setOnClickListener(OnItemClickListener listener) { mListener = listener; } @Override public CustomHolder onCreateViewHolder(ViewGroup parent, final int viewType) { final LayoutInflater inflater = LayoutInflater.from(mContext); final View v = inflater.inflate(R.layout.custom_layout, parent, false); final CustomHolder vHolder = new CustomHolder(v); return vHolder; } @Override public void onBindViewHolder(final CustomHolder vHolder, int position) { CustomItem item = mItems.get(position); vHolder.textViewTitle.setText(item.title); mBitmap = BitmapFactory.decodeResource(mContext.getResources(), item.imgId); mRoundedBitmap = RoundedBitmapDrawableFactory.create(mContext.getResources(), mBitmap); mRoundedBitmap.setCornerRadius(150.0f); //mRoundedBitmap.setCircular(true); mRoundedBitmap.setAntiAlias(true); vHolder.imgContent.setImageDrawable(mRoundedBitmap); vHolder.textViewContent.setText(item.content); vHolder.img3dots.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int pos = vHolder.getAdapterPosition(); if (pos != RecyclerView.NO_POSITION) { /* Context wrapper = new ContextThemeWrapper(mContext, R.style.popupMenuStyle); PopupMenu popup = new PopupMenu(wrapper, v); */ PopupMenu popup = new PopupMenu(mContext, v); MenuInflater menuInflater = popup.getMenuInflater(); menuInflater.inflate(R.menu.popup, popup.getMenu()); popup.show(); popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { int id = item.getItemId(); String str = item.getTitle().toString(); Toast.makeText(mContext, str, Toast.LENGTH_SHORT).show(); if (id == R.id.menu1) { //... } return true; } }); } } }); } @Override public int getItemCount() { return mItems.size(); } } |
CustomHolder.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package com.phaisarn.myapplication; import android.support.v7.widget.RecyclerView; import android.view.View; import android.widget.ImageView; import android.widget.TextView; public class CustomHolder extends RecyclerView.ViewHolder { public TextView textViewTitle; public ImageView img3dots; public ImageView imgContent; public TextView textViewContent; public CustomHolder(View view) { super(view); textViewTitle = (TextView) view.findViewById(R.id.text_title); img3dots = (ImageView) view.findViewById(R.id.img_3dots); imgContent = (ImageView) view.findViewById(R.id.img_content); textViewContent = (TextView) view.findViewById(R.id.text_content); } } |
CustomItem.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package com.phaisarn.myapplication; public class CustomItem { public String title; public int imgId; public String content; public CustomItem(String title, int imgId, String content) { this.title = title; this.imgId = imgId; this.content = content; } } |
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 |
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.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; 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(); } }); fab.hide(); ArrayList items = new ArrayList(); items.addAll(Arrays.asList( new CustomItem("Jellybean (v4.1 - 4.3)", R.drawable.jellybean, "Debuted in June 2012: Key additions: Advanced natural language voice command capabilities akin to Apple's Siri, enhanced interface and overall responsiveness via \"Project Butter,\" Google Now support, an improved Web browser, enhanced file management capabilities and more."), new CustomItem("Kitkat (v4.4)", R.drawable.kitkat, "Debuted in November 2013. Key additions: Full-screen immersive mode, new transitions framework, and \"Project Svelte,\" a project initiated to reduce the memory needs of the Android OS. Originally internally referred to as Key Lime Pie, Google announced in early September 2013 that it would be using the iconic candy bar as the codename for the 4.4 Android release."), new CustomItem("Lollipop (v5.0)", R.drawable.lollipop, "Debuted in November 2014. Key additions: Enhanced Material Design user interface, improved continuity across Android devices, multiple user support, a guest user account option, a new notification system, support for 64-bit CPUs, and more."), new CustomItem("Marshmallow (v6.0)", R.drawable.marshmallow, "Debuted in November 2015. Key additions: Now on Tap functionality and other Google Now enhancements, native fingerprint authentication support, Android Pay integration, USB Type-C support, improved battery life, better app management and more."), new CustomItem("Nougat (v7.0)", R.drawable.nougat, "Debuted in September 2016. Key additions: Split Screen for true multitasking, Doze on the Go for conserving battery power, Notification Direct Reply and Bundled Notifications, new security features, a Data Saver mode and more.") )); CustomAdapter adapter = new CustomAdapter(this, items); RecyclerView rcv = (RecyclerView) findViewById(R.id.recyclerView); rcv.setAdapter(adapter); rcv.setLayoutManager(new LinearLayoutManager(this)); } @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); } } |
รูปที่ใช้