簡體   English   中英

自定義android視圖不會在調用invalidate時更新

[英]custom android view does not update when `invalidate` is called

我正在嘗試創建用於分頁的自定義視圖。

這是代碼:

public class PagerView extends LinearLayout {
    private Button mPrevButton;
    private Button mNextButton;
    private TextView mInformationTextView;

    public PagerView(Context context) {
        this(context, null);
    }

    public PagerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.initChild(context, attrs);
    }

    private void initChild(Context context, AttributeSet attrs) {
        this.setOrientation(LinearLayout.HORIZONTAL);
        this.setVisibility(GONE);

        mPrevButton = new Button(context);
        mPrevButton.setText("Prev");

        mNextButton = new Button(context);
        mNextButton.setText("Next");

        mInformationTextView = new TextView(context);
        mInformationTextView.setText("");

        addView(mPrevButton);
        addView(mInformationTextView);
        addView(mNextButton);
    }


    public void setPageInformation(int page, int totalPage) {
        if (page >= 1 && totalPage >= 1) {
            this.setVisibility(VISIBLE);
            if (totalPage > 1) {
                mInformationTextView.setText(page + "/" + totalPage);

                mPrevButton.setEnabled(page != 1);
                mNextButton.setEnabled(page != totalPage);
            }
        } else
            this.setVisibility(GONE);
        postInvalidate();
    }
}

和呼叫者:

public class PoiListActivity extends ActionBarActivity  {

    private ListView mListView;
    private PagerView mPageBar;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.poi_result_layout);

    setupListView();

    PoiResult poiResult = (PoiResult) GlobalState.getInstance().get(IConstant.Search_Result);
    if (poiResult != null) {
        refreshListView(poiResult);
    }
}

private void refreshListView(PoiResult poiResult) {
    mPageBar.setPageInformation(1, 3);

    mAdapterItems.clear();
    mAdapterItems.addAll(poiResult.poiList);
    mAdapter.notifyDataSetChanged();
}

private void setupListView() {
    mPageBar = new PagerView(this, this);

    mListView = (ListView) findViewById(R.id.poiList);
    mListView.addFooterView(mPageBar);

    mAdapter = new PoiInfoAdapter(this, R.layout.poi_result_item, mAdapterItems);
    mListView.setAdapter(mAdapter);
}
}

但是我有兩個問題:

1 pageView將永遠不會顯示。

然后我試圖評論這一行

this.setVisibility(GONE);  // set it unvisible when inited

然后顯示頁面欄,但是mInformationTextView的文本仍然為空,並且啟用了mPrevButton (由於頁面為1,因此應將其禁用)。

我叫postInvalidate(); 設置頁面信息后,為什么它不能按預期運行?

2即使pageView出現了,如何使mPrevButton mNextButtonmInformationTextView中心對齊?

根據您提供的代碼,您無需手動使視圖層次結構的任何部分無效。 盡管您已經創建了一個自定義視圖,但您仍然僅在修改現有視圖屬性(例如,可見性),並且當這些更改發生時,這些視圖將使自身無效。

之所以看不到該視圖,是因為您已將其添加為ListView的頁腳視圖,但是尚未設置適配器。 使用ListView ,頁眉/頁腳將添加到您通過setAdapter()提供的任何適配器實現中,並作為列表項內容的一部分進行繪制。 沒有列表適配器(即使當前為空),也沒有視圖。 因此,始終的最佳做法是立即在列表上設置適配器並在列表數據更改時對其進行更新,而不是等待列表數據設置適配器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM