2

カスタム行を作成してListViewに追加する方法については、多くの投稿と無限の質問があります。ただし、私が見つけたすべての投稿は、Javaコードを使用してカスタム行レイアウトファイルをリストに設定します。ほとんどのサンプルは次のようなことをします:

ArrayAdapter<String> adapter = new ArrayAdapter<String>
    (this,R.layout.row_layout,R.id.text1,colors);

XMLファイルでこれを直接行う方法がないかどうか疑問に思いましたか?行のカスタムレイアウトファイルを設定できるプロパティがListViewにありませんか?

私が質問している理由は、Eclipseのグラフィカルレイアウトツールですべての(ほとんどの)レイアウト作業を実行しようとしており、アプリを起動せずにレイアウトをプレビューできるようにするためです。

確かにこれは何らかの方法で可能であるはずですか?XMLでそれができれば、私は非常に満足しますが、グラフィカルレイアウトエディタの「カスタム」部分に表示されるカスタムクラスを作成する必要がある場合も同様です。

お時間をいただき、ありがとうございました。

4

2 に答える 2

12

グラフィカルエディタでListViewをプレビューすることができます。ListViewを右クリックして、[リストコンテンツのプレビュー]を選択するだけです。次に、[カスタム]を選択し、カスタム行レイアウトを選択します。

次に例を示します。

my_custom_list.xml

 <ListView android:id="@android:id/list"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:layout_weight="1"
           android:drawSelectorOnTop="false">
     <!-- Preview: listitem=@layout/account_list_item -->
 </ListView>

my_custom_row.xml

<CheckBox
    android:id="@+id/icon"

    android:layout_width="wrap_content"
    android:layout_height="fill_parent"

    android:layout_alignParentTop="true"
    android:layout_alignParentBottom="true"
    android:layout_marginRight="6dip"
     />

<TextView  
    android:id="@+id/secondLine"

    android:layout_width="fill_parent"
    android:layout_height="26dip" 

    android:layout_toRightOf="@id/icon"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"

    android:singleLine="true"
    android:ellipsize="marquee"
    android:text="mail@mail.com" />

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"

    android:layout_toRightOf="@id/icon"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_above="@id/secondLine"
    android:layout_alignWithParentIfMissing="true"

    android:gravity="center_vertical"
    android:text="Account name" />

my_custom_list.xmlのグラフィカルエディターで、リストを右クリックします。「プレビューリストコンテンツ」の「カスタム」オプションからmy_custom_rowを選択します。

于 2012-06-26T10:17:10.343 に答える
1

はい、もちろんですが、最初にArrayAdapter<>を拡張するクラスを作成する必要があります

このクラスは次のようになります。

public class Mylistcustom extends ArrayAdapter<String> {

private LayoutInflater mInflater;
private int mViewResourceId;
private String[] your_date;

    public Mylistcustom(Context ctx, int viewResourceId, String[] data) {

    super(ctx, viewResourceId,data);
    this.contexto=ctx;
    mInflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    mViewResourceId = viewResourceId;
    this.your_data=data;



    @Override
    public int getCount() {
    return your_data.length;
}

@Override
    public String getItem(int position) {
    return your_data[position];
}

@Override
    public long getItemId(int position) {
    return 0;
}

@Override
    public View getView(final int position, View convertView, ViewGroup parent) {

    rowView = mInflater.inflate(mViewResourceId, null);// you are inflating your xml layout
    text=(textView) rowView.findViewById(R.id.textView);//find a textview in your layout, of course you can add whatever you want such as ImaveView Buttons ....
    text.setText(your_data[position]); // and set text from your_data
    return rowView;  

}}

リストビューアクティビティでは、コンストラクターを呼び出して、データ、レイアウト、コンテキストを渡すだけです。

Myadapter = new Mylistcustom(context, R.layout.YOUR_XML_LAYOUT,DATA);

// and finally set the adapter to your list
ListView lv = (ListView) findViewById(android.R.id.list);
lv.setAdapter(Myadapter);
于 2012-06-26T09:24:30.817 に答える