简体   繁体   English

自定义android视图不会在调用invalidate时更新

[英]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. 1 pageView将永远不会显示。

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). 然后显示页面栏,但是mInformationTextView的文本仍然为空,并且启用了mPrevButton (由于页面为1,因此应将其禁用)。

I have called the postInvalidate(); 我叫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? 2即使pageView出现了,如何使mPrevButton mNextButtonmInformationTextView中心对齐?

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. 之所以看不到该视图,是因为您已将其添加为ListView的页脚视图,但是尚未设置适配器。 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. 使用ListView ,页眉/页脚将添加到您通过setAdapter()提供的任何适配器实现中,并作为列表项内容的一部分进行绘制。 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. 因此,始终的最佳做法是立即在列表上设置适配器并在列表数据更改时对其进行更新,而不是等待列表数据设置适配器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android - 名为invalidate()但视图不刷新 - Android--called invalidate() but view does not refresh 自定义视图上的invalidate()不会导致调用onDraw(Canvas c) - invalidate() on custom View does not cause onDraw(Canvas c) to be called Android自定义视图,调用invalidate()后未调用onDraw - Android custom view, onDraw not called after calling invalidate() 自定义视图未在 BottomSheetDialogFragment 中绘制,有时在无效后调用 onSizeChanged - Custom View is not drawing in BottomSheetDialogFragment, when sometimes onSizeChanged is called after invalidate Android 使自定义视图无效并重绘 - Android invalidate and redraw custom view invalidate()不会立即更改自定义视图的内容 - invalidate() does not changes content of custom view immediately 调用notifyDataSetChanged()时,ArrayAdapter不更新视图 - ArrayAdapter does not update view when notifyDataSetChanged() is called Invalidate 不调用自定义视图类的 onDraw - Custom View class' onDraw isn't called by Invalidate 无缝背景作为自定义视图组件,invalidate()不会调用onDraw - Seamless background as custom view component , onDraw is not get called by invalidate() android invalidate函数未调用 - android invalidate function is not called
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM