티스토리 뷰
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:design:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/darker_gray"
android:padding="4dp"
android:scrollbars="vertical"
android:layout_marginBottom="100dp"/>
<EditText
android:id="@+id/editext_insert"
android:inputType="number"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginStart="15dp"
android:layout_marginBottom="25dp"
android:layout_width="40dp"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button_insert"
android:text="insert"
android:layout_alignParentBottom="true"
android:layout_toEndOf="@+id/editext_insert"
android:layout_marginBottom="25dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/editext_remove"
android:inputType="number"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="@+id/button_remove"
android:layout_marginStart="15dp"
android:layout_marginBottom="25dp"
android:layout_width="40dp"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button_remove"
android:text="remove"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_marginBottom="25dp"
android:layout_marginRight="50dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_margin="4dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView"
android:padding="2dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_toEndOf="@id/imageView"
android:text="Line 1"
android:textColor="@android:color/black"
android:textSize="20sp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textView2"
android:layout_below="@id/textView"
android:layout_toEndOf="@id/imageView"
android:layout_marginStart="8dp"
android:text="Line 2"
android:textSize="15sp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/image_delete"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:src="@drawable/ic_delete_black_24dp" />
</RelativeLayout>
</android.support.v7.widget.CardView>
package com.example.devat.myapplication;
public class ExampleItem {
private int mImageResource;
private String mText1;
private String mText2;
public ExampleItem(int mImageResource, String mText1, String mText2) {
this.mImageResource = mImageResource;
this.mText1 = mText1;
this.mText2 = mText2;
}
public int getmImageResource() {
return mImageResource;
}
//
public void changeTExt1(String text ) {
mText1 = text;
}
public String getmText1() {
return mText1;
}
public String getmText2() {
return mText2;
}
}
public class MainActivity extends AppCompatActivity {
private ArrayList<ExampleItem> exampleItems = new ArrayList<>();
private RecyclerView recyclerView;
private ExampleAdapter adapter;
private RecyclerView.LayoutManager layoutManager;
private Button buttonInsert;
private Button buttonRemove;
private EditText editTextInsert;
private EditText editTextRemove;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createExampleList();
buildRecyclerView();
setButtons();
}
private void setButtons() {
buttonInsert = findViewById(R.id.button_insert);
buttonRemove = findViewById(R.id.button_remove);
editTextInsert = findViewById(R.id.editext_insert);
editTextRemove = findViewById(R.id.editext_remove);
buttonInsert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int position = Integer.parseInt(editTextInsert.getText().toString().trim());
insertItem(position);
}
});
buttonRemove.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int position = Integer.parseInt(editTextRemove.getText().toString().trim());
removeItem(position);
}
});
}
private void insertItem(int position) {
//Todo modify insert division , out of index check
exampleItems.add(new ExampleItem(R.drawable.ic_aspect_ratio_black_24dp, "new Item At Position " + position, "Line " + position));
adapter.notifyItemInserted(position);
}
private void removeItem(int position) {
// Todo out of index chceck
exampleItems.remove(position);
adapter.notifyItemRemoved(position);
}
public void changeItem(int position, String text) {
exampleItems.get(position).changeTExt1(text);
adapter.notifyItemChanged(position);
}
private void createExampleList() {
exampleItems = new ArrayList<>();
exampleItems.add(new ExampleItem(R.drawable.ic_android_black_24dp, ":Line 1", "Lind 2"));
exampleItems.add(new ExampleItem(R.drawable.ic_adb_black_24dp, ":Line 3", "Lind 4"));
exampleItems.add(new ExampleItem(R.drawable.ic_aspect_ratio_black_24dp, ":Line 5", "Lind 6"));
exampleItems.add(new ExampleItem(R.drawable.ic_aspect_ratio_black_24dp, ":Line 7", "Lind 8"));
exampleItems.add(new ExampleItem(R.drawable.ic_aspect_ratio_black_24dp, ":Line 9", "Lind 10"));
}
private void buildRecyclerView() {
recyclerView = findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
adapter = new ExampleAdapter(exampleItems);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
adapter.setOnItemClickListener(new ExampleAdapter.OnItemClickListener() {
@Override
public void onItemClick(int position) {
changeItem(position, "Clicked");
}
@Override
public void onDeleteClick(int position) {
removeItem(position);
}
});
}
}
public class ExampleAdapter extends RecyclerView.Adapter<ExampleAdapter.ExampleViewHolder> {
private ArrayList<ExampleItem> mExampleList;
public OnItemClickListener onItemClickListener;
public interface OnItemClickListener {
void onItemClick(int position);
void onDeleteClick(int position);
}
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}
public static class ExampleViewHolder extends RecyclerView.ViewHolder {
public ImageView imageView;
public ImageView imageDelete;
public TextView textView1;
public TextView textView2;
public ExampleViewHolder(@NonNull View itemView, final OnItemClickListener onItemClickListener) {
super(itemView);
imageView = itemView.findViewById(R.id.imageView);
textView1 = itemView.findViewById(R.id.textView);
textView2 = itemView.findViewById(R.id.textView2);
imageDelete = itemView.findViewById(R.id.image_delete);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (onItemClickListener != null) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
onItemClickListener.onItemClick(position);
}
}
}
});
imageDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (onItemClickListener != null) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
onItemClickListener.onDeleteClick(position);
}
}
}
});
}
}
public ExampleAdapter(ArrayList<ExampleItem> mExampleList) {
this.mExampleList = mExampleList;
}
@NonNull
@Override
public ExampleViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_layout, viewGroup, false);
ExampleViewHolder viewHolder = new ExampleViewHolder(v, onItemClickListener);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull ExampleViewHolder holder, int i) {
ExampleItem currentItem = mExampleList.get(i);
holder.imageView.setImageResource(currentItem.getmImageResource());
holder.textView1.setText(currentItem.getmText1());
holder.textView2.setText(currentItem.getmText2());
}
@Override
public int getItemCount() {
return mExampleList.size();
}
}
'android' 카테고리의 다른 글
EasyPermissions (0) | 2019.01.05 |
---|---|
imeOptions 사용 (0) | 2018.10.25 |
HashMap 사용방법 (0) | 2018.10.25 |
Custom AutoCompleteTextView (0) | 2018.10.23 |
spannable string (0) | 2018.10.23 |