|
|
(同じ利用者による、間の4版が非表示) |
行1: |
行1: |
− | ==画像サンプル==
| + | [[android/ListView/カスタム/記事]] |
− | activity_main.xmlに以下を追加
| + | |
− | <ListView
| + | |
− | android:id="@+id/sample_listview"
| + | |
− | android:layout_width="match_parent"
| + | |
− | android:layout_height="wrap_content" />
| + | |
− | samplelist_item.xml
| + | |
− | <pre>
| + | |
− | <?xml version="1.0" encoding="utf-8"?>
| + | |
− | <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
| + | |
− | android:orientation="horizontal"
| + | |
− | android:layout_width="match_parent"
| + | |
− | android:layout_height="match_parent">
| + | |
| | | |
− | <ImageView
| + | [[android/ListView/カスタム/画像]] |
− | android:id="@+id/thumbnail"
| + | |
− | android:layout_width="70dp"
| + | |
− | android:layout_height="70dp"
| + | |
− | android:padding="3dp" />
| + | |
| | | |
− | <TextView
| + | [[android/ListView/カスタム/getViewが複数回呼ばれる問題]] |
− | android:id="@+id/title"
| + | |
− | android:layout_width="wrap_content"
| + | |
− | android:layout_height="70dp"
| + | |
− | android:textColor="#000"
| + | |
− | android:padding="5dp"
| + | |
− | android:gravity="center_vertical"
| + | |
− | android:maxLines="1"
| + | |
− | android:text="Sample Text"/>
| + | |
− | | + | |
− | </LinearLayout>
| + | |
− | </pre>
| + | |
− | | + | |
− | SampleListItem.java
| + | |
− | <pre>
| + | |
− | import android.graphics.Bitmap;
| + | |
− | | + | |
− | public class SampleListItem {
| + | |
− | private Bitmap mThumbnail = null;
| + | |
− | private String mTitle = null;
| + | |
− | | + | |
− | public SampleListItem() {};
| + | |
− | | + | |
− | public SampleListItem(Bitmap thumbnail, String title) {
| + | |
− | mThumbnail = thumbnail;
| + | |
− | mTitle = title;
| + | |
− | }
| + | |
− | | + | |
− | public void setThumbnail(Bitmap thumbnail) {
| + | |
− | mThumbnail = thumbnail;
| + | |
− | }
| + | |
− | | + | |
− | public void setmTitle(String title) {
| + | |
− | mTitle = title;
| + | |
− | }
| + | |
− | | + | |
− | public Bitmap getThumbnail() {
| + | |
− | return mThumbnail;
| + | |
− | }
| + | |
− | | + | |
− | public String getTitle() {
| + | |
− | return mTitle;
| + | |
− | }
| + | |
− | }
| + | |
− | </pre>
| + | |
− | | + | |
− | MainActivity.javaのonCreateに追加
| + | |
− | <pre>
| + | |
− | ListView listView = (ListView)findViewById(R.id.sample_listview); | + | |
− | ArrayList<SampleListItem> listItems = new ArrayList<>();
| + | |
− | for (int i = 0; i < 4; i++) {
| + | |
− | Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
| + | |
− | SampleListItem item = new SampleListItem(bmp, "sample text No. " + String.valueOf(i));
| + | |
− | listItems.add(item);
| + | |
− | }
| + | |
− | SampleListAdapter adapter = new SampleListAdapter(this, R.layout.samplelist_item, listItems);
| + | |
− | listView.setAdapter(adapter);
| + | |
− | </pre>
| + | |
− | | + | |
− | 参考:https://qiita.com/ksugawara61/items/2d63f0be279a94b74550
| + | |