Skip to content

Commit e0bf11c

Browse files
authored
Merge pull request #47 from LeoAndo/feature/horizontal_list
RecyclerViewを使ったサンプル
2 parents 3912619 + 11e3915 commit e0bf11c

7 files changed

Lines changed: 211 additions & 3 deletions

File tree

.idea/deploymentTargetDropDown.xml

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/gradle.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.template.androidbasicapp.data;
2+
3+
4+
import androidx.annotation.NonNull;
5+
6+
public final class Item {
7+
private String title;
8+
9+
public Item(@NonNull final String title) {
10+
this.title = title;
11+
}
12+
13+
public String getTitle() {
14+
return title;
15+
}
16+
17+
public void setTitle(String title) {
18+
this.title = title;
19+
}
20+
}

app/src/main/java/com/template/androidbasicapp/ui/fragment/UiDemoFragment.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,22 @@
66
import androidx.annotation.Nullable;
77
import androidx.fragment.app.Fragment;
88
import androidx.navigation.fragment.NavHostFragment;
9+
import androidx.recyclerview.widget.LinearLayoutManager;
910

11+
import android.os.SystemClock;
1012
import android.view.View;
1113

14+
import com.google.android.material.snackbar.Snackbar;
1215
import com.template.androidbasicapp.BuildConfig;
1316
import com.template.androidbasicapp.R;
17+
import com.template.androidbasicapp.data.Item;
1418
import com.template.androidbasicapp.databinding.FragmentUiDemoBinding;
19+
import com.template.androidbasicapp.ui.widget.MyListAdapter;
20+
21+
import java.util.ArrayList;
22+
import java.util.Collections;
23+
import java.util.List;
24+
import java.util.concurrent.TimeUnit;
1525

1626
public class UiDemoFragment extends Fragment {
1727
private FragmentUiDemoBinding binding;
@@ -39,6 +49,31 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
3949
} else {
4050
binding.map.setVisibility(View.INVISIBLE);
4151
}
52+
53+
// リストの処理 -START
54+
final List<Item> items = new ArrayList<>();
55+
for (int i = 1; i < 29; i++) {
56+
items.add(new Item("鉄人" + i + "号"));
57+
}
58+
final MyListAdapter adapter = new MyListAdapter(items);
59+
binding.listHorizontal.setAdapter(adapter);
60+
// 縦向きの場合 -> LinearLayoutManager.VERTICALを指定する
61+
binding.listHorizontal.setLayoutManager(new LinearLayoutManager(requireContext(), LinearLayoutManager.HORIZONTAL, false));
62+
adapter.setOnItemClickListener(item -> {
63+
Snackbar.make(binding.getRoot(), item.getTitle(), Snackbar.LENGTH_LONG).show();
64+
});
65+
66+
binding.insert.setOnClickListener(v -> {
67+
final String title = "鉄人" + SystemClock.elapsedRealtime() + "号";
68+
adapter.insertItem(0, new Item(title));
69+
});
70+
binding.update.setOnClickListener(v -> {
71+
adapter.updateItem(0);
72+
});
73+
binding.remove.setOnClickListener(v -> {
74+
adapter.removeItem(0);
75+
});
76+
// リストの処理 -END
4277
}
4378

4479
/**
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package com.template.androidbasicapp.ui.widget;
2+
3+
import static androidx.recyclerview.widget.RecyclerView.NO_POSITION;
4+
5+
import android.util.Log;
6+
import android.view.LayoutInflater;
7+
import android.view.View;
8+
import android.view.ViewGroup;
9+
import android.widget.TextView;
10+
11+
import androidx.annotation.NonNull;
12+
import androidx.annotation.Nullable;
13+
import androidx.recyclerview.widget.RecyclerView;
14+
15+
import com.template.androidbasicapp.R;
16+
import com.template.androidbasicapp.data.Item;
17+
18+
import java.util.ArrayList;
19+
import java.util.List;
20+
21+
public class MyListAdapter extends RecyclerView.Adapter<MyListAdapter.VH> {
22+
@NonNull
23+
public final List<Item> items;
24+
@Nullable
25+
private OnItemClickListener listener;
26+
27+
public MyListAdapter(@NonNull final List<Item> items) {
28+
this.items = items;
29+
}
30+
31+
@NonNull
32+
@Override
33+
public VH onCreateViewHolder(@NonNull ViewGroup parent, final int viewType) {
34+
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
35+
return new VH(itemView);
36+
}
37+
38+
@Override
39+
public void onBindViewHolder(@NonNull VH holder, final int position) {
40+
final Item currentItem = items.get(position);
41+
holder.textTitle.setText(currentItem.getTitle());
42+
}
43+
44+
@Override
45+
public int getItemCount() {
46+
return this.items.size();
47+
}
48+
49+
public void updateItem(final int position) {
50+
if (getItemCount() <= 0) return;
51+
52+
final Item updateItem = items.get(position);
53+
updateItem.setTitle("update: " + updateItem.getTitle());
54+
notifyItemChanged(position);
55+
}
56+
57+
public void insertItem(final int index, @NonNull final Item newItem) {
58+
items.add(index, newItem);
59+
notifyItemInserted(index); // 変更がある箇所だけ差分更新する.
60+
// notifyDataSetChanged(); // これだとリスト全体の更新が走る.
61+
}
62+
63+
public void removeItem(final int index) {
64+
if (getItemCount() <= 0) return;
65+
66+
items.remove(index);
67+
notifyItemRemoved(index);
68+
}
69+
70+
class VH extends RecyclerView.ViewHolder {
71+
private static final String TAG = "VH";
72+
private final TextView textTitle;
73+
74+
public VH(@NonNull final View itemView) {
75+
super(itemView);
76+
textTitle = itemView.findViewById(R.id.title);
77+
itemView.setOnClickListener(new View.OnClickListener() {
78+
@Override
79+
public void onClick(@NonNull final View v) {
80+
final int position = getAdapterPosition();
81+
Log.d(TAG, "onClick position-> " + position);
82+
if (position != NO_POSITION && listener != null) {
83+
listener.onItemClick(items.get(position));
84+
}
85+
}
86+
});
87+
}
88+
}
89+
90+
public interface OnItemClickListener {
91+
void onItemClick(@NonNull final Item item);
92+
}
93+
94+
public void setOnItemClickListener(@Nullable final OnItemClickListener l) {
95+
this.listener = l;
96+
}
97+
}

app/src/main/res/layout/fragment_ui_demo.xml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,46 @@
1313
android:gravity="center_horizontal"
1414
android:orientation="vertical">
1515

16+
<TextView
17+
style="?attr/textAppearanceHeadline6"
18+
android:layout_width="wrap_content"
19+
android:layout_height="wrap_content"
20+
android:text="RecyclerView - Horizontal" />
21+
22+
<LinearLayout
23+
android:layout_width="wrap_content"
24+
android:layout_height="wrap_content"
25+
android:orientation="horizontal">
26+
27+
<Button
28+
android:id="@+id/insert"
29+
android:layout_width="wrap_content"
30+
android:layout_height="wrap_content"
31+
android:text="insert" />
32+
33+
<Button
34+
android:id="@+id/update"
35+
android:layout_width="wrap_content"
36+
android:layout_height="wrap_content"
37+
android:text="update" />
38+
39+
<Button
40+
android:id="@+id/remove"
41+
android:layout_width="wrap_content"
42+
android:layout_height="wrap_content"
43+
android:text="remove" />
44+
</LinearLayout>
45+
46+
<androidx.recyclerview.widget.RecyclerView
47+
android:id="@+id/list_horizontal"
48+
android:layout_width="match_parent"
49+
android:layout_height="wrap_content" />
50+
1651
<Button
1752
android:id="@+id/map"
1853
android:layout_width="wrap_content"
1954
android:layout_height="wrap_content"
55+
android:layout_marginTop="12dp"
2056
android:text="Map" />
2157

2258
<TextView
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools"
5+
android:layout_width="wrap_content"
6+
android:layout_height="wrap_content"
7+
android:minWidth="100dp"
8+
android:minHeight="100dp"
9+
app:cardCornerRadius="12dp">
10+
11+
<TextView
12+
android:id="@+id/title"
13+
android:layout_width="wrap_content"
14+
android:layout_height="wrap_content"
15+
android:layout_gravity="center"
16+
android:ellipsize="end"
17+
android:maxLines="1"
18+
tools:text="タイトル" />
19+
20+
</androidx.cardview.widget.CardView>

0 commit comments

Comments
 (0)