티스토리 뷰
Android/Java
[Java] RecyclerView + ItemClickListener + ItemLongClickListener
혀가 길지 않은 개발자 2020. 7. 21. 16:56RecyclerView + ItemClickListener + ItemLongClickListener
build.gradle (Module: app)
dependencies {
// RecyclerView
implementation 'androidx.recyclerview:recyclerview:1.1.0'
// CardView
implementation 'androidx.cardview:cardview:1.0.0'
}
cardview_item.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_margin="10dp"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:background="#BBBBBB"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent">
<TextView
android:text="혀가 길지 않은 개발자"
android:textSize="26dp"
android:textStyle="bold|italic"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tvPosition"
android:text="1"
android:textSize="22dp"
android:textStyle="bold|italic"
android:layout_marginTop="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:listitem="@layout/cardview_item" />
</LinearLayout>
MyAdapter.java
package com.jwsoft.javaproject;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
List<Integer> mData;
public interface ItemClickInterface {
void onClick(View view, int position);
}
public interface ItemLongClickInterface {
void onLongClick(View view, int position);
}
public ItemClickInterface itemClickInterface;
public ItemLongClickInterface itemLongClickInterface;
public MyAdapter(ArrayList<Integer> data,
ItemClickInterface clickInterface,
ItemLongClickInterface longClickInterface
) {
this.mData = data;
this.itemClickInterface = clickInterface;
this.itemLongClickInterface = longClickInterface;
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView tvPosition;
View itemView;
public MyViewHolder(View itemView) {
super(itemView);
this.itemView = itemView;
tvPosition = itemView.findViewById(R.id.tvPosition);
}
public void bind(final int position) {
tvPosition.setText(Integer.toString(position));
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
itemClickInterface.onClick(itemView, position);
}
});
itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
itemLongClickInterface.onLongClick(itemView, position);
return true;
}
});
}
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.cardview_item, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
holder.bind(position);
}
@Override
public int getItemCount() {
return mData.size();
}
}
MainActivity.java
package com.jwsoft.javaproject;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recyclerView);
MyAdapter.ItemClickInterface itemClickInterface = new MyAdapter.ItemClickInterface() {
@Override
public void onClick(View view, int position) {
Toast.makeText(getApplicationContext(), "Short Click " + position, Toast.LENGTH_SHORT).show();
}
};
MyAdapter.ItemLongClickInterface itemLongClickInterface = new MyAdapter.ItemLongClickInterface() {
@Override
public void onLongClick(View view, int position) {
Toast.makeText(getApplicationContext(), "Long Click " + position, Toast.LENGTH_SHORT).show();
}
};
ArrayList<Integer> data = new ArrayList<>();
for (int i=0; i<10; i++) {
data.add(i);
}
MyAdapter adapter = new MyAdapter(data, itemClickInterface, itemLongClickInterface);
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
}
}
'Android > Java' 카테고리의 다른 글
[Java] Generics (0) | 2020.07.24 |
---|---|
[Java] MVVM (0) | 2020.07.23 |
[Java] AlertDialog (0) | 2020.07.21 |
[Java] Room (0) | 2020.07.20 |
[Java] SQLite + Singleton Pattern (0) | 2020.07.18 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- Vue.js #Vue.js + javascript
- Android
- DataBinding
- View
- handler
- activity
- Kotlin
- Livedata
- 안드로이드 #코틀린 #Android #Kotlin
- CoordinatorLayout
- Architecture Pattern
- ViewPager2
- 자바
- 혀가 길지 않은 개발자
- MVVM
- java
- recyclerview
- JSONObject
- 코틀린
- 안드로이드
- ViewModel
- James Kim
- ArrayList
- XML
- fragment
- TabLayout
- Intent
- Design Pattern
- coroutine
- JSONArray
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함