Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
187 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
.../app/src/main/java/com/hatenablog/atuyan39/androidtraining/TrainingListViewActivity2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.hatenablog.atuyan39.androidtraining; | ||
|
||
import androidx.appcompat.app.AppCompatActivity; | ||
|
||
import android.graphics.Bitmap; | ||
import android.graphics.BitmapFactory; | ||
import android.os.Bundle; | ||
import android.widget.ListView; | ||
|
||
import com.hatenablog.atuyan39.androidtraining.adapter.ImageTextListAdapter; | ||
import com.hatenablog.atuyan39.androidtraining.view.ImageTextListItem; | ||
|
||
import java.util.ArrayList; | ||
|
||
public class TrainingListViewActivity2 extends AppCompatActivity { | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_training_list_view_actvity2); | ||
|
||
// ListViewを取得 | ||
ListView listView = findViewById(R.id.list_view_2); | ||
|
||
// リストに表示するアイテムを作成 | ||
ArrayList<ImageTextListItem> listItems = new ArrayList<>(); | ||
for (int i = 0; i < 3; i++) { | ||
Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.gohan); | ||
String text = "List item " + i; | ||
ImageTextListItem item = new ImageTextListItem(image, text); | ||
listItems.add(item); | ||
} | ||
|
||
// 作成したリストを表示する | ||
ImageTextListAdapter adapter = new ImageTextListAdapter(this, R.layout.image_text_list_item, listItems); | ||
listView.setAdapter(adapter); | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
...p/src/main/java/com/hatenablog/atuyan39/androidtraining/adapter/ImageTextListAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.hatenablog.atuyan39.androidtraining.adapter; | ||
|
||
import android.content.Context; | ||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
import android.widget.ArrayAdapter; | ||
import android.widget.ImageView; | ||
import android.widget.TextView; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
|
||
import com.hatenablog.atuyan39.androidtraining.R; | ||
import com.hatenablog.atuyan39.androidtraining.view.ImageTextListItem; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* {@link ImageTextListItem}のアダプター。 | ||
*/ | ||
public class ImageTextListAdapter extends ArrayAdapter<ImageTextListItem> { | ||
|
||
private final int mResource; | ||
private final List<ImageTextListItem> mItems; | ||
private final LayoutInflater mInflater; | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public ImageTextListAdapter(@NonNull Context context, int resource, @NonNull List<ImageTextListItem> items) { | ||
super(context, resource, items); | ||
|
||
mResource = resource; | ||
mItems = items; | ||
mInflater = LayoutInflater.from(context); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@NonNull | ||
@Override | ||
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { | ||
View view; | ||
|
||
if (convertView != null) { | ||
view = convertView; | ||
} else { | ||
view = mInflater.inflate(mResource, null); | ||
} | ||
|
||
// リストに表示するアイテムを取得 | ||
ImageTextListItem item = mItems.get(position); | ||
|
||
// Imageを設定 | ||
ImageView image = view.findViewById(R.id.list_item_image); | ||
image.setImageBitmap(item.getImage()); | ||
|
||
// Textを設定 | ||
TextView text = view.findViewById(R.id.list_item_text); | ||
text.setText(item.getText()); | ||
|
||
return view; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...ing/app/src/main/java/com/hatenablog/atuyan39/androidtraining/view/ImageTextListItem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.hatenablog.atuyan39.androidtraining.view; | ||
|
||
import android.graphics.Bitmap; | ||
|
||
import androidx.annotation.NonNull; | ||
|
||
/** | ||
* ImageViewとTextが配置されたリストに使うアイテム | ||
*/ | ||
public class ImageTextListItem { | ||
private Bitmap mImage; | ||
private String mText; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param image 画像 | ||
* @param text テキスト | ||
*/ | ||
public ImageTextListItem(@NonNull Bitmap image, @NonNull String text) { | ||
mImage = image; | ||
mText = text; | ||
} | ||
|
||
// alt + insert(Getter and Setter)で自動追加できる | ||
public Bitmap getImage() { | ||
return mImage; | ||
} | ||
|
||
public void setImage(Bitmap mImage) { | ||
this.mImage = mImage; | ||
} | ||
|
||
public String getText() { | ||
return mText; | ||
} | ||
|
||
public void setText(String mText) { | ||
this.mText = mText; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
AndroidTraining/app/src/main/res/layout/activity_training_list_view_actvity2.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<androidx.constraintlayout.widget.ConstraintLayout 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=".TrainingListViewActivity2"> | ||
|
||
<ListView | ||
android:id="@+id/list_view_2" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" /> | ||
</androidx.constraintlayout.widget.ConstraintLayout> |
20 changes: 20 additions & 0 deletions
20
AndroidTraining/app/src/main/res/layout/image_text_list_item.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?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="match_parent" | ||
android:orientation="horizontal"> | ||
|
||
<ImageView | ||
android:id="@+id/list_item_image" | ||
android:layout_width="70dp" | ||
android:layout_height="70dp" | ||
app:srcCompat="@android:drawable/alert_dark_frame" | ||
android:contentDescription="@string/list_item_image_description" /> | ||
|
||
<TextView | ||
android:id="@+id/list_item_text" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:gravity="center_vertical" /> | ||
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
消さないで