0

私は現在、Adapter起動時にXMLから膨らんだビューを使用して作成しています

private void addView(Context context) {
    LayoutInflater inflater = LayoutInflater.from(context);
    View view = inflater.inflate(R.layout.deal_tile, this, null);
    mViews.add(view);
}

ただし、ビューを 内のリストに格納すると、AdapterViewそれらのビュー内のコントロールで問題が発生することがわかったので、 のリサイクル機能を使用するように変更したいと考えていますAdapter#getView(int position, View recycle, ViewGroup container)

if(recycle!=null && recycle instanceof CustomView)このため、アダプタに再設定する前にサニティ チェック ( ) を実行できるように、カスタム ビュー クラスを使用したいと考えています。ただし、XML からカスタム ビュー クラスを拡張する方法がわかりません。インフレートされたビューをカスタム ビューに追加する方法、カスタム ビューを XML レイアウトに挿入する方法などを見つけることができます。明らかに、これらのものを直接使用して非常に喜んでインフレートしてLayoutInflaterいますが、見つけることができません。カスタム ビュー自体を生成するのと同等です。既に持っている XML を再利用したい。したがって、要素 (およびそれらの外観) を直接プログラムしたくありません。

4

1 に答える 1

0

これを使用して独自のスライド ギャラリーを作成しました。役立つと思います。

    LinearLayout internalWrapper = new LinearLayout(getContext());
    internalWrapper.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    internalWrapper.setOrientation(LinearLayout.HORIZONTAL);
    addView(internalWrapper);
    this.mItems = items;
    LinearLayout generalLayout = new LinearLayout(this.getContext());

    generalLayout = (LinearLayout) View.inflate(this.getContext(), R.layout.galleryrow, null);

    // inside linear layout
    LinearLayout generalLinear = (LinearLayout) generalLayout.findViewById(R.id.rowgenerallin);
    // set height & width to the LINEAR
    generalLinear.setLayoutParams(new LayoutParams(reference_width, reference_height));

    ImageView ivl = (ImageView) generalLayout.findViewById(R.id.arrow_left);
    ImageView ivr = (ImageView) generalLayout.findViewById(R.id.arrow_right);
    internalWrapper.addView(generalLayout);

私の場合、R.layout.gallery_row管理したい 2 つの画像が含まれており、 によってネストされています。LinearLayous (rowgenerllin)内部ラッパーは、LinearLayoutアクティビティのメイン レイアウトで宣言された空です。LayoutParams コードを再確認しないと、大きな NULL が返されます:)

乾杯!

于 2012-11-19T12:43:20.793 に答える