2012年7月9日月曜日

ListViewの要素変更:ArrayAdapter viewの再利用


AndroidListViewTestActivity.java
import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.os.Bundle;

public class AndroidListViewTestActivity extends ListActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        List<BindData> list = new ArrayList<BindData>();
        // ここにlistに項目を追加する処理が入る
        list.add(new BindData("タイトル1", null , null));
        list.add(new BindData(null, "line1-1" ,"line1-2"));
  

        list.add(new BindData("タイトル2", null , null));
        list.add(new BindData(null, "line2-1" ,"line2-2"));
        list.add(new BindData(null, "line2-12" ,"line2-22"));

        MyAdapter adapter = new MyAdapter(this, list);
        setListAdapter(adapter);
    }
}

MyAdapter.java
import java.util.List;

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 android.widget.TextView;

class BindData {
    String title;
    String line1;
    String line2;

    public BindData(String string0, String string1, String string2) {
        this.title = string0;
        this.line1 = string1;
        this.line2 = string2;
    }
}

class ViewHolder {
    TextView title;
    TextView line1;
    TextView line2;
}

public class MyAdapter extends ArrayAdapter<BindData> {

    private LayoutInflater inflater;

    public MyAdapter(Context context, List<BindData> objects) {
        super(context, 0, objects);
        this.inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public boolean isEnabled(int position) {
        // 選択不可にする
        return true;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.raw, parent, false);
            holder = new ViewHolder();
            holder.title = (TextView) convertView.findViewById(R.id.title);
            holder.line1 = (TextView) convertView.findViewById(R.id.line1);
            holder.line2 = (TextView) convertView.findViewById(R.id.line2);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        BindData data = getItem(position);

        //Log.d("タグ", ":" + position);
        //Log.d("タグ", "::" + data.title);
        //Log.d("タグ", ":::" + isEnabled(position));

        // タイトル
        if ( getItem(position).title != null ) {
            holder.title.setVisibility(View.VISIBLE);
            holder.title.setText(data.title);
            holder.line1.setVisibility(View.GONE);
            holder.line2.setVisibility(View.GONE);
        // line1,2
        } else {
            holder.title.setVisibility(View.GONE);
            holder.line1.setVisibility(View.VISIBLE);
            holder.line1.setText(data.line1);
            holder.line2.setVisibility(View.VISIBLE);
            holder.line2.setText(data.line2);
        }
        return convertView;
    }
}

main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@id/android:list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

raw.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <!-- グループタイトル -->

    <TextView
        android:id="@+id/title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:background="#808080" android:textColor="#FFFFFF" android:text="title" android:gravity="center"/>
    <!-- アルバム名 -->

    <TextView
        android:id="@+id/line1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="line1"/>
    <!-- アーティスト名 -->

    <TextView
        android:id="@+id/line2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:text="line2"/>

</LinearLayout>

[ソースコード公開中]
Google Code

関連記事

0 件のコメント:

コメントを投稿