如何设置通过寻呼机适配器创建的片段内的xml元素的可见性?

时间:2015-06-17 18:00:54

标签: java android android-fragments android-viewpager

我有这个活动,它实例化代表乘客的5个不同的片段,并将它们添加到选项卡中的视图寻呼机中。每个都膨胀相同的xml文件。我想要做的是能够根据乘客的类型设置每个片段中某些xml字段的可见性。我根据标签名称确定乘客的类型。

例如:字段' etDataNasc'只有在乘客属于' INF'

的情况下才能出现

当我实例化片段时,我将它们放在片段数组中,并检查对应的标签名称是否具有我想要的条件(最后有' INF')。如果是这样,我想从该片段访问etDataNasc字段并将其可见性设置为VISIBLE,否则它必须是INVISIBLE。

我在这里做了一些关于stackoverflow的研究,但是找不到任何对我有帮助的东西。基于类似问题的一些答案,我尝试在newInstance方法中扩展布局,但是当我尝试在我的活动中设置其可见性时,它仍然显示每个乘客的字段!

很抱歉这个问题很长,但任何帮助都会深表感谢。

以下是我的活动代码(我的问题的相关部分):

public class DadosPassageirosActivity extends android.support.v7.app.ActionBarActivity {

Toolbar toolbar;
private android.support.v7.app.ActionBar bar;
ViewPager pager;
ViewPagerPassageirosAdapter pagerAdapter;
SlidingTabLayout tabs;
CharSequence Titles[];
private int nTitlesPassageiros = 1;

public int nAdultos, nCriancas, nBebes, nPassageiros;
private Passageiro[] passageirosArray;
private int arrayControl = 0;

private DrawerLayout filtrosDrawerLayout;
private ListView drawerList;

public DadosPassageirosFragment[] passageiroFragment;

public Button btnProsseguir;

static boolean isInf;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dados_passageiros);

    // Creating The Toolbar and setting it as the Toolbar for the activity
    toolbar = (Toolbar) findViewById(R.id.tool_bar);
    setSupportActionBar(toolbar);
    bar = getSupportActionBar();
    bar.setDisplayHomeAsUpEnabled(true);

    Intent intent = getIntent();
    nAdultos = intent.getExtras().getInt("nAdultos");
    nCriancas = intent.getExtras().getInt("nCriancas");
    nBebes = intent.getExtras().getInt("nBebes");
    nPassageiros = intent.getExtras().getInt("nPassageiros");
    passageirosArray = new Passageiro[nPassageiros];
    passageiroFragment = new DadosPassageirosFragment[nPassageiros];

    Titles = new CharSequence[nPassageiros];

    int i;
    for (i=0; i<nPassageiros; i++){
        Titles[i] = nTitlesPassageiros + "° Passageiro" + " (ADT)";
        nTitlesPassageiros +=1;
    }

    if (nCriancas > 0){
        for (i=nCriancas; i>0; i--){
            Titles[(Titles.length-i)-nBebes] = Titles[(Titles.length-i)-nBebes].subSequence(0,13) + " (CHD)";
        }
    }

    if (nBebes >0){
        for (i=nBebes; i>0; i--){
            Titles[Titles.length-i] = Titles[Titles.length-i].subSequence(0,13) + " (INF)";
        }
    }


    // Creating The ViewPagerAdapter and Passing Fragment Manager, Titles fot the Tabs and Number Of Tabs.
    pagerAdapter =  new ViewPagerPassageirosAdapter(getSupportFragmentManager(),Titles,nPassageiros, this);

    // Assigning ViewPager View and setting the adapter
    pager = (NonSwipeableViewPager) findViewById(R.id.pager);
    pager.setAdapter(pagerAdapter);

    for (i=0; i<nPassageiros; i++){
        passageiroFragment[i] = (DadosPassageirosFragment)pagerAdapter.getItem(i);
        getTabName(i);
    }
}

public void getTabName(int pos){
    CharSequence title = pagerAdapter.getPageTitle(pos);
    if (title.subSequence(title.length()-4, title.length()-1).equals("INF")){
            passageiroFragment[pos].etDataNasc.setVisibility(View.VISIBLE);
    }
    else {
        passageiroFragment[pos].etDataNasc.setVisibility(View.INVISIBLE);
    }
}
}

这是我的适配器:

public class ViewPagerPassageirosAdapter extends FragmentStatePagerAdapter {

Context mContext;

CharSequence Titles[]; // This will Store the Titles of the Tabs which are Going to be passed when ViewPagerAdapter is created
int NumbOfTabs; // Store the number of tabs, this will also be passed when the ViewPagerAdapter is created


// Build a Constructor and assign the passed Values to appropriate values in the class
public ViewPagerPassageirosAdapter(FragmentManager fm,CharSequence mTitles[], int mNumbOfTabsumb, Context c) {
    super(fm);

    this.Titles = mTitles;
    this.NumbOfTabs = mNumbOfTabsumb;
    this.mContext = c;

}

//This method return the fragment for the every position in the View Pager
@Override
public Fragment getItem(int position) {
    DadosPassageirosFragment tab = DadosPassageirosFragment.newInstance(this.mContext);
    return tab;

}

// This method return the titles for the Tabs in the Tab Strip

@Override
public CharSequence getPageTitle(int position) {
    return Titles[position];
}

// This method return the Number of tabs for the tabs Strip

@Override
public int getCount() {
    return NumbOfTabs;
}
}

以下是使用Instance方法的片段本身:

public class DadosPassageirosFragment extends Fragment {


static EditText etNome, etSobrenome, etEmail;
TextView aviso;
public EditText etDataNasc;

DadosPassageirosActivity dadosPassageiros;

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_form_passageiro, container, false);

    dadosPassageiros = (DadosPassageirosActivity)getActivity();

    etNome = (EditText)v.findViewById(R.id.formPassageirosNome);
    //etSobrenome = (EditText)v.findViewById(R.id.formPassageirosSobrenome);
    //etEmail = (EditText)v.findViewById(R.id.formPassageirosEmail);
    etDataNasc = (EditText)v.findViewById(R.id.formPassageirosDataNasc);

    return v;
}

public static DadosPassageirosFragment newInstance(Context c){
    DadosPassageirosFragment f = new DadosPassageirosFragment();
    LayoutInflater inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflater.inflate(R.layout.fragment_form_passageiro, null, false);

    f.etDataNasc = (EditText)v.findViewById(R.id.formPassageirosDataNasc);
    return f;
}
}

1 个答案:

答案 0 :(得分:0)

这是一个很长的解决方案所以请问我,如果你不明白的东西.. 在DadosPassageirosFragment上添加一个布尔值,决定该对象是否应该可见..

boolean isVisible;
主Activity上的

创建所有片段并根据您的需要更新isVisible var ..

将所有片段发送到适配器,不要在片段内创建它们,只需根据所选的poistion返回正确的内容。

public ViewPagerPassageirosAdapter(`DadosPassageirosFragment[] fragments`...

当系统构建每个Fragment时,它将检查isVisible var并显示/隐藏此EditText

使用onStart()函数来查看/隐藏EditText