我正在尝试为分页创建自定义视图。
这是代码:
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
mNextButton
和mInformationTextView
居中对齐?