我想创建一个自定义的分页视图。定制android视图不会更新当'invalidate`被调用
这是代码:
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浏览量永远不会出现。
然后我试图评论的行
this.setVisibility(GONE); // set it unvisible when inited
然后在页面栏显示,但mInformationTextView
文本仍然是空的,而mPrevButton
启用(应启用该取消,因为页面是1 )。
一旦我设置了页面信息,我就调用了postInvalidate();
,但为什么它不按预期工作?
2即使是pageView显示,如何使mPrevButton
mNextButton
和mInformationTextView
居中对齐?
实际上,我已经为'listView'设置了'adapter'。当我在这里发布代码时,我修复了代码,现在我更新了整个代码,你能检查我的udpate吗? – hguser
在你的新代码中出现了一些奇怪的事情,因为2参数版本需要一个'AttributeSet',所以'new PagerView(this,this)'没有有效的构造函数。当我用'new PagerView(this)'代替它并运行你的代码时,页脚显示得很好。 – Devunwired
工作,谢谢。:) – hguser