@brenna
Для создания списка с изображениями и текстом на Java можно использовать такие элементы пользовательского интерфейса, как ListView
или RecyclerView
вместе с адаптером для отображения данных.
- Создайте класс-модель Item, который будет представлять элемент списка. В данном классе можно добавить поля для хранения изображения и текста:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
public class Item {
private int image;
private String text;
public Item(int image, String text) {
this.image = image;
this.text = text;
}
public int getImage() {
return image;
}
public String getText() {
return text;
}
}
|
- Создайте макет элемента списка. Это может быть XML-файл, например, list_item.xml, который будет содержать ImageView и TextView для изображения и текста соответственно.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<!-- list_item.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/image_view"
android:layout_width="50dp"
android:layout_height="50dp" />
<TextView
android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
|
- Создайте адаптер, который свяжет данные с элементом списка:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
public class CustomAdapter extends ArrayAdapter<Item> {
public CustomAdapter(Context context, ArrayList<Item> items) {
super(context, 0, items);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
}
Item currentIte = getItem(position);
ImageView imageView = listItemView.findViewById(R.id.image_view);
imageView.setImageResource(currentItem.getImage());
TextView textView = listItemView.findViewById(R.id.text_view);
textView.setText(currentItem.getText());
return listItemView;
}
}
|
- Используйте ListView или RecyclerView в вашей активности/фрагменте для отображения списка:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
// MainActivity.java
public class MainActivity extends AppCompatActivity {
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<Item> itemList = new ArrayList<>();
itemList.add(new Item(R.drawable.image1, "Text 1"));
itemList.add(new Item(R.drawable.image2, "Text 2"));
// добавьте ваш список изображений и текста
CustomAdapter adapter = new CustomAdapter(this, itemList);
listView = findViewById(R.id.list_view);
listView.setAdapter(adapter);
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
|
<!-- activity_main.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
|
Таким образом, после выполнения этих шагов у вас будет список, который будет содержать изображения и текст.