|
|
行1: |
行1: |
− | ==記事サンプル==
| + | [[android/ListView/カスタム/記事]] |
− | 例)タイトルを2カラムに分けて表示するようなリストを作る
| + | |
| | | |
− | activity_main.xmlに以下を追加
| + | [[android/ListView/カスタム/画像]] |
− | <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">
| + | |
− | | + | |
− | <TextView
| + | |
− | 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="title1"/>
| + | |
− | | + | |
− | <TextView
| + | |
− | android:id="@+id/description"
| + | |
− | android:layout_width="wrap_content"
| + | |
− | android:layout_height="70dp"
| + | |
− | android:textColor="#000"
| + | |
− | android:padding="5dp"
| + | |
− | android:gravity="center_vertical"
| + | |
− | android:maxLines="1"
| + | |
− | android:text="description1"/>
| + | |
− | | + | |
− | </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>
| + | |
− | | + | |
− | SampleListAdapter.java
| + | |
− | <pre>
| + | |
− | import android.content.Context;
| + | |
− | import android.util.Log;
| + | |
− | import android.view.LayoutInflater;
| + | |
− | import android.view.View;
| + | |
− | import android.view.ViewGroup;
| + | |
− | import android.widget.ArrayAdapter;
| + | |
− | import java.util.List;
| + | |
− | public class SampleListAdapter extends ArrayAdapter<SampleListItem> {
| + | |
− | private int mResource;
| + | |
− | private List<SampleListItem> mItems;
| + | |
− | private LayoutInflater mInflater;
| + | |
− | public SampleListAdapter(Context context, int resource, List<SampleListItem> items) {
| + | |
− | super(context, resource, items);
| + | |
− | mResource = resource;
| + | |
− | mItems = items;
| + | |
− | mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
| + | |
− | }
| + | |
− | @Override
| + | |
− | public View getView(int position, View convertView, ViewGroup parent) {
| + | |
− | View view;
| + | |
− | if (convertView != null) {
| + | |
− | view = convertView;
| + | |
− | }
| + | |
− | else {
| + | |
− | view = mInflater.inflate(mResource, null);
| + | |
− | }
| + | |
− | SampleListItem item = mItems.get(position);
| + | |
− | ImageView thumbnail = (ImageView)view.findViewById(R.id.thumbnail);
| + | |
− | thumbnail.setImageBitmap(item.getThumbnail());
| + | |
− | TextView title = (TextView)view.findViewById(R.id.title);
| + | |
− | title.setText(item.getTitle());
| + | |
− | return view;
| + | |
− | }
| + | |
− | }
| + | |
− | </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 data 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
| + | |