当寻呼机android

时间:2016-09-19 19:24:37

标签: android android-viewpager android-recyclerview fragment

我正在使用2个显示数据的片段,在寻呼机内部,我的问题是:我想在没有数据显示时显示对话框但是一旦我在其中一个上显示数据两个片段和它们的内容都是空的,显示了两个对话框。 我尝试了几种方法,但没有一种适合我。

这是我的寻呼机适配器:

public class PagerAdapter extends FragmentStatePagerAdapter { 

ArrayList<Fragment> pages = new ArrayList<>();


public PagerAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int position) {


    return pages.get(position);
}

@Override
public int getCount() {
    return pages.size();
}

@Override
public CharSequence getPageTitle(int position) {

    return pages.get(position).toString();
}

public void addPage (Fragment f){
    pages.add(f);
}
}

两个片段都很相似,所以我会显示其中一个片段的代码:

public class FragmentIng extends Fragment {
private View v;


public FragmentIng() {
    // Required empty public constructor

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment


        v = inflater.inflate(R.layout.fragment_ing, null);
        RecyclerView recyclerView = (RecyclerView) v.findViewById(R.id.recyclerview);
        Recycler_View_Adapter adapter = new Recycler_View_Adapter(personnes1, this.getActivity());
        recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
        recyclerView.setAdapter(adapter);

        RecyclerView.ItemAnimator itemAnimator = new DefaultItemAnimator();
        itemAnimator.setAddDuration(1000);
        itemAnimator.setRemoveDuration(1000);
        recyclerView.setItemAnimator(itemAnimator);
        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        int i = adapter.getItemCount();

    if ( i == 0  ){
        final NiftyDialogBuilder dialogBuilder= NiftyDialogBuilder.getInstance(this.getContext());
        dialogBuilder
                .withTitle("Employé")                                  //.withTitle(null)  no title
                .withTitleColor("#FFFFFF")                                  //def
                .withDividerColor("#11000000")                              //def
                .withMessage("Aucun employé à afficher")                     //.withMessage(null)  no Msg
                .withMessageColor("#FFFFFFFF")                              //def  | withMessageColor(int resid)
                .withDialogColor("#FFE74C3C")                               //def  | withDialogColor(int resid)
                .withIcon(getResources().getDrawable(R.drawable.icon))
                .withDuration(700)                                          //def
                .withEffect(Shake)                                         //def Effectstype.Slidetop
                .withButton1Text("Retour")                                      //def gone
                .withButton2Text("Annuler")                                  //def gone
                .isCancelableOnTouchOutside(true)                           //def    | isCancelable(true)
                //.setCustomView(R.layout.custom_view,v.getContext())         //.setCustomView(View or ResId,context)
                .setButton1Click(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialogBuilder.cancel();
                        Toast.makeText(v.getContext(), "Principale", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(getActivity(), MainActivity.class);
                        getActivity().startActivity(intent);



                    }
                })
                .setButton2Click(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //Toast.makeText(v.getContext(),"i'm btn2",Toast.LENGTH_SHORT).show();
                        dialogBuilder.cancel();
                    }
                })
                .show();
    }



    return v;

}
@Override
public String toString() {
    return "Les Employés";
}

}

这是我的recyclerview适配器:

public class Recycler_View_Adapter extends RecyclerView.Adapter<View_Holder>{


List<Personne> personnes = Collections.emptyList();
Context context;

Dialog dialog;

public Recycler_View_Adapter(List<Personne> list, Context context) {
    this.personnes = list;
    this.context = context;
}

private Context getContext() {
    return context;
}


@Override
public View_Holder onCreateViewHolder(ViewGroup parent, int viewType) {

    Context context = parent.getContext();
    LayoutInflater inflater = LayoutInflater.from(context);

    View v = inflater.inflate(R.layout.row_layout, parent, false);
    View_Holder holder = new View_Holder(v);
    return holder;
}

@Override
public void onBindViewHolder(View_Holder holder, final int position) {



    holder.title.setText(personnes.get(position).nom_prenom);
    //holder.description.setText("Etage N° : "+personnes.get(position).etage+" à "+personnes.get(position).date.toDateTime());
    holder.occurence.setText(personnes.get(position).occurence+" / "+personnes.get(position).NbrDeJour);




    animate(holder);

}


@Override
public int getItemCount() {
            return personnes.size();
}



@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);
}
public void animate(RecyclerView.ViewHolder viewHolder) {
    final Animation animAnticipateOvershoot = AnimationUtils.loadAnimation(context, R.anim.bounce_interpolator);
    viewHolder.itemView.setAnimation(animAnticipateOvershoot);
}

}

1 个答案:

答案 0 :(得分:1)

我认为你可以通过覆盖setUserVisibleHint方法解决问题。

您可以将此添加到viewpager上的第二个片段,以便知道片段何时对用户可见,然后您可以检查recyclerview是否为空以显示您的对话框。

例如

$arrays = @{
    foo = "name1", "name2", "name3"
    bar = "name4", "name5", "name6"
}

$searchname = Read-Host "Enter the name to search for"
ForEach ($entry in $arrays.GetEnumerator()) {
   if ($searchname -in $entry.value) {
       Write-Host "$searchname found in array $($entry.key)"
   }
}