2

私の Android プロジェクトでは、ID ではなくタグでアクティビティ内のビューを見つける必要があります。(単純に を使用しない理由を聞かれたら、それは私の好みではなく、説明が複雑ですが、現在、定数findViewByIdに頼ることはできませんR.id.!) これまでのところ、コード内でビューを見つけるために、なんとかこれは成功しました:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.index);
    mainViewGroup = (ViewGroup)getWindow().getDecorView();
    TextView indexCaption = (TextView) mainViewGroup.findViewWithTag("index_caption");
    // Now I have my TextView and can use it without problem
}

しかし今、問題は同じアクティビティのカスタム ArrayAdapter です。

    class IconicAdapter extends ArrayAdapter<String> {
    IconicAdapter() {
                    // How I can use Tag instead of R.layout.index_row_caption here?!!!
        super(IndexActivity.this, R.layout.index_row, R.id.index_row_caption, arrayList);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = super.getView(position, convertView, parent);
                    // Some customizations...
        return (row);
    }
}

そのindex_row.xmlIconicAdapter の場合:

android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/content_bg" >

<TextView
    android:id="@+id/index_row_caption"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_toLeftOf="@+id/index_row_icon"
    android:layout_toRightOf="@+id/index_row_search"
    android:gravity="right"
    android:tag="index_row_caption"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<ImageView
    android:id="@+id/index_row_icon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:tag="index_row_icon" />

<ImageView
    android:id="@+id/index_row_search"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:src="@drawable/index_row_search"
    android:tag="index_row_search" />

R.id.index_row_captionコンストラクターでの使用を避け、そのタグに関連するものに置き換えるにはどうすればよいですか?

編集:ついに、私はこのコードに行き着きました、そしてそれはうまくいきました: (クレジットと賞金は、これで私を助けてくれたpawelziebaに行きます)

    class IconicAdapter extends ArrayAdapter<String> {
    IconicAdapter() {
        super(IndexActivity.this, R.layout.index_row, 0, arrayList);
    }

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

        View view;
        if (convertView == null) {
            LayoutInflater mInflater = IndexActivity.this.getLayoutInflater();
            view = mInflater.inflate(R.layout.index_row, parent, false);
        } else {
            view = convertView;
        }

        //find views
        TextView text = (TextView) view.findViewWithTag("index_row_caption");

        //fill views with data
        text.setText(arrayList.get(position));          

        return view;
    }
}
4

3 に答える 3

2

メソッドを拡張ArrayAdapterしてオーバーライドしますgetView()

public View getView(int position, View convertView, ViewGroup parent) {
    View view;

    if (convertView == null) {
        view = mInflater.inflate(R.layout.index_row, parent, false);
    } else {
        view = convertView;
    }

    //find views
    TextView text = (TextView) view.findViewWithTag("index_row_caption");

    //fill views with data
    //example:
    T item = getItem(position);
    text.setText(item.toString());


    return view;
}

その場合、コンストラクターに渡された行リソース ID は使用されません。

パフォーマンスを向上させるには、ビュー ホルダー パターンを使用して、行のビューを作成するときに findViewWithTag を 1 回だけ呼び出します

于 2012-09-08T15:10:39.947 に答える
0

これは、次の方法で実行できます。

android:tag="someTag"属性をviewインxmlレイアウト ファイルに追加します。

そのビューにアクセスするには、次を使用します。

findViewByTag(); method

このようにして、ID ではなくタグを介してビューにアクセスできます。

于 2012-09-03T09:38:33.600 に答える