2013-10-29 34 views
0

我想创建一个自定义的分页视图。定制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显示,如何使mPrevButtonmNextButtonmInformationTextView居中对齐?

回答

1

根据您提供的代码,您不需要手动使视图层次结构的任何部分失效。尽管您已经创建了一个自定义视图,但您仍然只是修改现有的视图属性(例如可见性),并且这些视图会在发生这些更改时自行失效。

您无法看到该视图的原因是因为您已将其添加为ListView的页脚视图,但尚未设置适配器。通过ListView,页眉/页脚被添加到您通过setAdapter()提供的任何适配器实现中,并作为列表项内容的一部分进行绘制。没有列表适配器(即使它当前为空),没有视图。因此,将您的适配器立即设置到列表中并在列表数据发生更改时进行更新,而不是等待您的列表数据设置适配器,这总是一种很好的做法。

+0

实际上,我已经为'listView'设置了'adapter'。当我在这里发布代码时,我修复了代码,现在我更新了整个代码,你能检查我的udpate吗? – hguser

+0

在你的新代码中出现了一些奇怪的事情,因为2参数版本需要一个'AttributeSet',所以'new PagerView(this,this)'没有有效的构造函数。当我用'new PagerView(this)'代替它并运行你的代码时,页脚显示得很好。 – Devunwired

+0

工作,谢谢。:) – hguser

相关问题