简体   繁体   中英

custom android view does not update when `invalidate` is called

I am trying to create a custom view for pagination.

This is the codes:

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();
    }
}

And the caller:

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);
}
}

However I have two problems:

1 The pageView will never show up.

Then I tried to comment the line

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

Then the page bar shows, but the text of mInformationTextView is still empty, and the mPrevButton is enabled(it should be un-enabled since the page is 1).

I have called the postInvalidate(); once I set the page information, but why it does not work as expected?

2 Even the pageView show up, how to make the mPrevButton mNextButton and mInformationTextView center aligned?

Based on the code you've provided, you don't need to manually invalidate any part of the view hierarchy. Despite the fact that you have created a custom view, you are still only modifying existing view properties (eg visibility) and those views will invalidate themselves when those changes take place.

The reason you cannot see the view is because you have added it as a footer view for your ListView , but you haven't set an adapter. With ListView the header/footers are added to whichever adapter implementation you provide via setAdapter() and are drawn as part of the list item contents. No list adapter (even if it is currently empty), no views. Because of this, it is always good practice to set your adapter on your list immediately and update it when your list data changes, rather than waiting for your list data to set the adapter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM