0

这是来自我的自定义 PageAdapter:

public Object instantiateItem(View collection, int position) {
    LayoutInflater inflater = (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View view = null;
    switch (position) {
    case 0: view = dMngr.GetView(inflater); 
        break;
    case 1: view = mMngr.GetView(inflater); 
        break;
    case 2: view = cMngr.GetView(inflater); 
        break;
    }   
    ((ViewPager) collection).addView(view, 0);
    return view;
}

这是cMngr.GetView代码:

public View GetView(LayoutInflater inflater){
    View view = null;
    if (showCamDetail){
        view = inflater.inflate(R.layout.cam_view, null);
        ((Button)view.findViewById(R.id.button1)).setOnClickListener(myhandler2);
    } else {
        view = inflater.inflate(R.layout.cams_view, null);  
        ((ImageView)view.findViewById(R.id.imageView1)).setOnClickListener(myhandler);
    }
    return view;
}

View.OnClickListener myhandler = new View.OnClickListener() {
    public void onClick(View v) {
        showCamDetail = true;
        hPageAdapter.notifyDataSetChanged();
    }
};
View.OnClickListener myhandler2 = new View.OnClickListener() {
    public void onClick(View v) {
        showCamDetail = false;
        hPageAdapter.notifyDataSetChanged();
    }
};

layout仅当我滑动到第一页然后回到第三页时,此代码才会在第三页上更改。layout单击我的ImageView或后,我需要立即更改它Button

4

1 回答 1

0

如果两个视图状态之间的差异不是太复杂,请将它们都包含在同一布局中,View.setVisibility(View.GONE)View.setVisibility(View.VISIBLE)根据需要选择性地隐藏/显示其中的一部分。您可以setTag()在充气后进行布局,以便以后更容易找到它,findViewByTag()并且您可以应用可见性更改。

于 2013-04-08T10:55:10.733 回答