apply plugin: 'com.android.application'
android {
compileSdkVersion 27
buildToolsVersion "27.+"
defaultConfig {
applicationId "com.your.appname"
minSdkVersion 14
targetSdkVersion 27
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation ('android.arch.core:runtime:+') {
force = true
}
//noinspection GradleCompatible
implementation 'com.android.support:appcompat-v7:19.+'
implementation fileTree(dir: 'libs', include: ['*.jar'])
}
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private RecyclerView.LayoutManager layoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<RecyclerViewItem> recyclerViewItems = new ArrayList<>();
recyclerViewItems.add(new RecyclerViewItem(R.drawable.ic_mood_bad_black_24dp, "Happy", "Live is fan" ));
recyclerViewItems.add(new RecyclerViewItem(R.drawable.ic_mood_black_24dp, "Happy not", "Live is fan" ));
recyclerView = findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
adapter = new RecyclerViewAdapter(recyclerViewItems);
layoutManager = new LinearLayoutManager(this);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(layoutManager);
}
}
implementation 'androidx.recyclerview:recyclerview:1.1.0'
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/constraintLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
import androidx.recyclerview.widget.RecyclerView
val recyclerView: RecyclerView = view.findViewById(R.id.recyclerView1)
recyclerView.layoutManager = LinearLayoutManager(activity)
recyclerView.adapter = MyAdapter(list)
class MyAdapter(private val values: MutableList<String>) : RecyclerView.Adapter<Adapter.ViewHolder>() {
override fun getItemCount() = values.size
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val itemView = LayoutInflater.from(parent.context).inflate(R.layout.list_two, parent, false)
return ViewHolder(itemView)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.textView?.text = values[position].toString()
holder.textView?.setOnClickListener {
Toast.makeText(it.context, values[position], Toast.LENGTH_LONG).show()
}
}
class ViewHolder(itemView: View?) : RecyclerView.ViewHolder(itemView!!) {
var textView: TextView? = null
init {
textView = itemView?.findViewById(R.id.textView4)
}
}
}
<?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"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/imageView2"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_weight="1"
android:maxWidth="40dp"
android:maxHeight="40dp"
android:minWidth="40dp"
android:minHeight="40dp"
app:srcCompat="@drawable/temporary" />
<TextView
android:id="@+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="1dp"
android:layout_marginRight="1dp"
android:layout_weight="100"
android:text="TextView"
android:textColor="#000000"
android:textSize="16sp" />
</LinearLayout>