如何在viewpager中重新创建片段?

时间:2018-10-23 10:15:44

标签: android android-fragments android-viewpager

我只是尝试在viewpager中重新创建片段,但我不能。当我从当前片段回到上一个片段时,我想重新创建上一个片段。

例如,我有12个月是零散的。当我转到“三月页面”时,如果回退“二月页面”,则看不到“二月页面”的任何变化。

MonthPagerAdapter

public class MonthsPagerAdapter extends FragmentStatePagerAdapter {

private String [] tabTitles={"OCAK","SUBAT","MART","NISAN","MAYIS","HAZIRAN","TEMMUZ","AGUSTOS","EYLÜL","EKIM","KASIM","ARALIK","OCAK"};



public MonthsPagerAdapter(FragmentManager fm) {
    super(fm);

}

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

@Override
public int getItemPosition(@NonNull Object object) {
    return POSITION_NONE;
}

@Override
public Fragment getItem(int i) {
    switch (i) {
        case 0:
            return new PageMonth1Fragment();

        case 1:
            return new PageMonth2Fragment();

        case 2:
            return new PageMonth3Fragment();
        case 3:
            return new PageMonth4Fragment();

        case 4:
            return new PageMonth5Fragment();

        case 5:
            return new PageMonth6Fragment();

        case 6:
            return new PageMonth7Fragment();

        case 7:
            return new PageMonth8Fragment();

        case 8:
            return new PageMonth9Fragment();

        case 9:
            return new PageMonth10Fragment();

        case 10:
            return new PageMonth11Fragment();

        case 11:
            return new PageMonth12Fragment();

        case 12:
            return new PageMonth13Fragment();
        default:
                return null;


    }
}

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

DaysAdapter

public class DaysAdapter extends RecyclerView.Adapter<DaysAdapter.ViewHolder> {

private Context context;
private List<Integer> dayList;
private RecyclerView recyclerView;

View view;


private int i=0;
private int selectedMonth;
private int rezerStartDay,rezerfinishDay;

int startMonth,finishMonth;


public DaysAdapter(Context context, List<Integer> dayList, int selectedMonth,RecyclerView recyclerView) {
    this.context = context;
    this.dayList = dayList;
    this.recyclerView = recyclerView;
    this.selectedMonth = selectedMonth;

}

@NonNull
@Override
public DaysAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    view = LayoutInflater.from(context).inflate(R.layout.item_days, parent, false);
    return new ViewHolder(view);
}

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

    this.holder=holder;

    holder.daysText.setText(String.valueOf(dayList.get(position)));

    startMonth= SelectedDateIntervalSingleton.getInstance().getSelectedStartMonth();
    finishMonth = SelectedDateIntervalSingleton.getInstance().getSelectedFinishMonth();

    Log.e("e","onBindViewHolder");


    final SelectedDateIntervalSingleton selectedDateIntervalSingleton=SelectedDateIntervalSingleton.getInstance();

    if(selectedMonth==startMonth&&selectedDateIntervalSingleton.getClickInterval()==3) {
        for(int j=selectedDateIntervalSingleton.getRezervationStartDay();j<=dayList.get(dayList.size()-1);j++)
            if(i==j){
                holder.daysCardview.setBackgroundColor(Color.parseColor("#5d87d9"));
                holder.daysText.setTextColor(Color.parseColor("#ffffff"));



            }

    }

    holder.daysCardview.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(startMonth==finishMonth){
                if(selectedDateIntervalSingleton.getClickInterval()==1){
                    selectedDateIntervalSingleton.setRezervationStartDay(Integer.valueOf(holder.daysText.getText().toString()));
                    holder.daysCardview.setBackgroundColor(Color.parseColor("#5d87d9"));
                    holder.daysText.setTextColor(Color.parseColor("#ffffff"));
                    rezerStartDay=Integer.valueOf(holder.daysText.getText().toString());
                    selectedDateIntervalSingleton.setClickInterval(selectedDateIntervalSingleton.getClickInterval()+1);
                }else if(selectedDateIntervalSingleton.getClickInterval()==2){
                    rezerfinishDay=Integer.valueOf(holder.daysText.getText().toString());
                    if(rezerfinishDay>rezerStartDay){
                        for(int i=rezerStartDay;i<rezerfinishDay;i++){
                            ViewHolder holder= (ViewHolder) recyclerView.findViewHolderForAdapterPosition(i);
                            holder.daysCardview.setBackgroundColor(Color.parseColor("#5d87d9"));
                            holder.daysText.setTextColor(Color.parseColor("#ffffff"));
                        }

                        selectedDateIntervalSingleton.setClickInterval(selectedDateIntervalSingleton.getClickInterval()+1);
                        Toast.makeText(context,"finishDay="+rezerfinishDay,Toast.LENGTH_LONG).show();
                        selectedDateIntervalSingleton.setRezervationFinishDay(rezerfinishDay);
                    }else {
                        ViewHolder holder= (ViewHolder) recyclerView.findViewHolderForAdapterPosition(rezerStartDay-1);
                        holder.daysCardview.setBackgroundColor(Color.parseColor("#ffffff"));
                        holder.daysText.setTextColor(Color.parseColor("#43425d"));
                        holder= (ViewHolder) recyclerView.findViewHolderForAdapterPosition(position);
                        rezerStartDay=Integer.valueOf(holder.daysText.getText().toString());
                        Toast.makeText(context,"2startDay="+holder.daysText.getText().toString(),Toast.LENGTH_LONG).show();
                        selectedDateIntervalSingleton.setRezervationStartDay(Integer.valueOf(holder.daysText.getText().toString()));
                        holder.daysCardview.setBackgroundColor(Color.parseColor("#5d87d9"));
                        holder.daysText.setTextColor(Color.parseColor("#ffffff"));
                        selectedDateIntervalSingleton.setClickInterval(2);
                    }
                }else if(selectedDateIntervalSingleton.getClickInterval()==3){
                    for (int i=rezerStartDay-1;i<rezerfinishDay;i++){
                        ViewHolder holder= (ViewHolder) recyclerView.findViewHolderForAdapterPosition(i);
                        holder.daysCardview.setBackgroundColor(Color.parseColor("#ffffff"));
                        holder.daysText.setTextColor(Color.parseColor("#43425d"));

                    }
                    holder.daysCardview.setBackgroundColor(Color.parseColor("#5d87d9"));
                    holder.daysText.setTextColor(Color.parseColor("#ffffff"));
                    rezerStartDay=Integer.valueOf(holder.daysText.getText().toString());

                    selectedDateIntervalSingleton.setClickInterval(2);

                    Toast.makeText(context,"3startDay="+holder.daysText.getText().toString(),Toast.LENGTH_LONG).show();
                    selectedDateIntervalSingleton.setRezervationStartDay(Integer.valueOf(holder.daysText.getText().toString()));


                }
                //start mont ve finish mont not equal
            }else{
                //selected month start montsa
                if(startMonth==selectedMonth){
                    if(selectedDateIntervalSingleton.getClickInterval()==1){
                        Toast.makeText(context,"1startDay="+holder.daysText.getText().toString(),Toast.LENGTH_LONG).show();
                        selectedDateIntervalSingleton.setRezervationStartDay(Integer.valueOf(holder.daysText.getText().toString()));
                        holder.daysCardview.setBackgroundColor(Color.parseColor("#5d87d9"));
                        holder.daysText.setTextColor(Color.parseColor("#ffffff"));
                        rezerStartDay=Integer.valueOf(holder.daysText.getText().toString());
                        selectedDateIntervalSingleton.setClickInterval(selectedDateIntervalSingleton.getClickInterval()+1);
                        selectedDateIntervalSingleton.setMonthStartControl(true);
                    }
                    else if(selectedDateIntervalSingleton.getClickInterval()==2){
                        rezerfinishDay=Integer.valueOf(holder.daysText.getText().toString());
                        if(!selectedDateIntervalSingleton.isFinishMonthControl()){
                            if(rezerfinishDay>rezerStartDay){
                                for(int i=rezerStartDay;i<rezerfinishDay;i++){
                                    ViewHolder holder= (ViewHolder) recyclerView.findViewHolderForAdapterPosition(i);
                                    holder.daysCardview.setBackgroundColor(Color.parseColor("#5d87d9"));
                                    holder.daysText.setTextColor(Color.parseColor("#ffffff"));
                                    Log.e("e",holder.daysText.getText().toString());
                                }
                                selectedDateIntervalSingleton.setClickInterval(selectedDateIntervalSingleton.getClickInterval()+1);
                                Toast.makeText(context,"finishDay="+rezerfinishDay,Toast.LENGTH_LONG).show();
                                selectedDateIntervalSingleton.setRezervationFinishDay(rezerfinishDay);
                            }else {
                                ViewHolder holder= (ViewHolder) recyclerView.findViewHolderForAdapterPosition(rezerStartDay-1);
                                holder.daysCardview.setBackgroundColor(Color.parseColor("#ffffff"));
                                holder.daysText.setTextColor(Color.parseColor("#43425d"));
                                holder= (ViewHolder) recyclerView.findViewHolderForAdapterPosition(position);
                                rezerStartDay=Integer.valueOf(holder.daysText.getText().toString());
                                Toast.makeText(context,"start month 2startDay="+holder.daysText.getText().toString(),Toast.LENGTH_LONG).show();
                                selectedDateIntervalSingleton.setRezervationStartDay(Integer.valueOf(holder.daysText.getText().toString()));
                                holder.daysCardview.setBackgroundColor(Color.parseColor("#5d87d9"));
                                holder.daysText.setTextColor(Color.parseColor("#ffffff"));
                                selectedDateIntervalSingleton.setClickInterval(2);
                            }
                        }else {

                            rezerStartDay=Integer.valueOf(holder.daysText.getText().toString());
                            Toast.makeText(context,"2startDay="+holder.daysText.getText().toString(),Toast.LENGTH_LONG).show();
                            selectedDateIntervalSingleton.setRezervationStartDay(Integer.valueOf(holder.daysText.getText().toString()));
                            holder.daysCardview.setBackgroundColor(Color.parseColor("#5d87d9"));
                            holder.daysText.setTextColor(Color.parseColor("#ffffff"));
                            selectedDateIntervalSingleton.setClickInterval(2);
                            selectedDateIntervalSingleton.setFinishMonthClear(true);
                        }

                    }else if(selectedDateIntervalSingleton.getClickInterval()==3){
                        for (int i=rezerStartDay-1;i<rezerfinishDay;i++){
                            ViewHolder holder= (ViewHolder) recyclerView.findViewHolderForAdapterPosition(i);
                            holder.daysCardview.setBackgroundColor(Color.parseColor("#ffffff"));
                            holder.daysText.setTextColor(Color.parseColor("#43425d"));

                        }
                        holder.daysCardview.setBackgroundColor(Color.parseColor("#5d87d9"));
                        holder.daysText.setTextColor(Color.parseColor("#ffffff"));
                        rezerStartDay=Integer.valueOf(holder.daysText.getText().toString());
                        selectedDateIntervalSingleton.setClickInterval(2);

                        Toast.makeText(context,"3startDay="+holder.daysText.getText().toString(),Toast.LENGTH_LONG).show();
                        selectedDateIntervalSingleton.setRezervationStartDay(Integer.valueOf(holder.daysText.getText().toString()));

                    }
                    //selected month finish montsa
                }else if(finishMonth==selectedMonth){
                    if(selectedDateIntervalSingleton.getClickInterval()==1){
                        Toast.makeText(context,"finish month startDay="+holder.daysText.getText().toString(),Toast.LENGTH_LONG).show();
                        selectedDateIntervalSingleton.setRezervationStartDay(Integer.valueOf(holder.daysText.getText().toString()));
                        holder.daysCardview.setBackgroundColor(Color.parseColor("#5d87d9"));
                        holder.daysText.setTextColor(Color.parseColor("#ffffff"));
                        rezerStartDay=Integer.valueOf(holder.daysText.getText().toString());
                        selectedDateIntervalSingleton.setClickInterval(selectedDateIntervalSingleton.getClickInterval()+1);
                        selectedDateIntervalSingleton.setFinishMonthControl(true);
                    }else if(selectedDateIntervalSingleton.getClickInterval()==2){
                        rezerfinishDay=Integer.valueOf(holder.daysText.getText().toString());
                        if(!selectedDateIntervalSingleton.isMonthStartControl()){
                            if(rezerfinishDay>rezerStartDay){
                                for(int i=rezerStartDay;i<rezerfinishDay;i++){
                                    ViewHolder holder= (ViewHolder) recyclerView.findViewHolderForAdapterPosition(i);
                                    holder.daysCardview.setBackgroundColor(Color.parseColor("#5d87d9"));
                                    holder.daysText.setTextColor(Color.parseColor("#ffffff"));
                                }
                                selectedDateIntervalSingleton.setClickInterval(selectedDateIntervalSingleton.getClickInterval()+1);
                                Toast.makeText(context,"finish month finishDay="+rezerfinishDay,Toast.LENGTH_LONG).show();
                                selectedDateIntervalSingleton.setRezervationFinishDay(rezerfinishDay);
                            }else {
                                ViewHolder holder= (ViewHolder) recyclerView.findViewHolderForAdapterPosition(rezerStartDay-1);
                                holder.daysCardview.setBackgroundColor(Color.parseColor("#ffffff"));
                                holder.daysText.setTextColor(Color.parseColor("#43425d"));
                                holder= (ViewHolder) recyclerView.findViewHolderForAdapterPosition(position);
                                rezerStartDay=Integer.valueOf(holder.daysText.getText().toString());
                                Toast.makeText(context,"2startDay="+holder.daysText.getText().toString(),Toast.LENGTH_LONG).show();
                                selectedDateIntervalSingleton.setRezervationStartDay(Integer.valueOf(holder.daysText.getText().toString()));
                                holder.daysCardview.setBackgroundColor(Color.parseColor("#5d87d9"));
                                holder.daysText.setTextColor(Color.parseColor("#ffffff"));
                                selectedDateIntervalSingleton.setClickInterval(2);
                            }
                        }else {
                            for(int i=0;i<rezerfinishDay;i++){
                                ViewHolder holder= (ViewHolder) recyclerView.findViewHolderForAdapterPosition(i);
                                holder.daysCardview.setBackgroundColor(Color.parseColor("#5d87d9"));
                                holder.daysText.setTextColor(Color.parseColor("#ffffff"));

                            }
                            selectedDateIntervalSingleton.setClickInterval(selectedDateIntervalSingleton.getClickInterval()+1);
                            Toast.makeText(context,"finish month finishDay="+rezerfinishDay,Toast.LENGTH_LONG).show();
                            selectedDateIntervalSingleton.setRezervationFinishDay(rezerfinishDay);
                            selectedDateIntervalSingleton.setFinishMonthCompleteControl(true);
                        }
                        selectedDateIntervalSingleton.setFinishMonthControl(true);

                    }else if(selectedDateIntervalSingleton.getClickInterval()==3){
                        if(!selectedDateIntervalSingleton.isMonthStartControl()){
                            for (int i=rezerStartDay-1;i<rezerfinishDay;i++){
                                ViewHolder holder= (ViewHolder) recyclerView.findViewHolderForAdapterPosition(i);
                                holder.daysCardview.setBackgroundColor(Color.parseColor("#ffffff"));
                                holder.daysText.setTextColor(Color.parseColor("#43425d"));

                            }


                        }else {
                            for (int i=0;i<rezerfinishDay;i++){
                                ViewHolder holder= (ViewHolder) recyclerView.findViewHolderForAdapterPosition(i);
                                holder.daysCardview.setBackgroundColor(Color.parseColor("#ffffff"));
                                holder.daysText.setTextColor(Color.parseColor("#43425d"));

                            }

                        }
                        holder.daysCardview.setBackgroundColor(Color.parseColor("#5d87d9"));
                        holder.daysText.setTextColor(Color.parseColor("#ffffff"));
                        rezerStartDay=Integer.valueOf(holder.daysText.getText().toString());
                        selectedDateIntervalSingleton.setClickInterval(2);
                        selectedDateIntervalSingleton.setMonthStartControl(false);
                        selectedDateIntervalSingleton.setFinishMonthControl(true);
                        Toast.makeText(context,"finish mont 3startDay="+holder.daysText.getText().toString(),Toast.LENGTH_LONG).show();
                        selectedDateIntervalSingleton.setRezervationStartDay(Integer.valueOf(holder.daysText.getText().toString()));
                    }
                }
            }
        }
    });
}




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

public class ViewHolder extends RecyclerView.ViewHolder {
    TextView daysText;
    ConstraintLayout daysCardview;
    public ViewHolder(@NonNull View itemView) {
        super(itemView);
        daysText = itemView.findViewById(R.id.daysText);
        daysCardview = itemView.findViewById(R.id.daysCons);
        int startMonth = SelectedDateIntervalSingleton.getInstance().getSelectedStartMonth();
        int finishMonth = SelectedDateIntervalSingleton.getInstance().getSelectedFinishMonth();
        int startDay = SelectedDateIntervalSingleton.getInstance().getSelectedStartDay();
        int finishDay = SelectedDateIntervalSingleton.getInstance().getSelectedFinishDay();

        RentalRezervation rentalDetailRezervation= (RentalRezervation) context;
        int size=dayList.size();

        itemView.setClickable(false);
        itemView.setEnabled(false);
        daysText.setTextColor(Color.parseColor("#c8c8c8"));
        i++;
        if(startMonth ==selectedMonth&& !(finishMonth ==selectedMonth)){
            for (int j = startDay; j<=size; j++){
                if(i==j){
                    itemView.setClickable(true);
                    itemView.setEnabled(true);
                    daysText.setTextColor(Color.parseColor("#43425d"));
                }
            }
            rentalDetailRezervation.setTvStartYear();

        }else if(finishMonth ==selectedMonth&& !(startMonth ==selectedMonth)){
            for (int j = 1; j<= finishDay; j++){
                if(i==j){
                    itemView.setClickable(true);
                    itemView.setEnabled(true);
                    daysText.setTextColor(Color.parseColor("#43425d"));
                }
            }
            rentalDetailRezervation.setTvFinishYear();


        }else if(startMonth ==selectedMonth){
            for(int j = startDay; j<= finishDay; j++){
                if(i==j){
                    itemView.setClickable(true);
                    itemView.setEnabled(true);
                    daysText.setTextColor(Color.parseColor("#43425d"));
                }
            }
            rentalDetailRezervation.setTvStartYear();


        }
        Log.e("e","onCreateViewHolder" +
                "");


    }
}
}

enter image description here

3 个答案:

答案 0 :(得分:2)

您可以添加

viewPager.setOffscreenPageLimit(12);

到您的viewpager。因此它永远不会破坏您的任何事业。 然后编辑您的适配器,以免您每次都不创建新片段:

    private String [] tabTitles=

{"OCAK","SUBAT","MART","NISAN","MAYIS","HAZIRAN",
"TEMMUZ","AGUSTOS","EYLÜL","EKIM","KASIM","ARALIK","OCAK"};
        private ArrayList<fragment> fragments=new ArrayList();


        public MonthsPagerAdapter(FragmentManager fm) {
            super(fm);

        }

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

    @Override
    public int getItemPosition(@NonNull Object object) {
        return POSITION_NONE;
    }

    @Override
    public Fragment getItem(int i) {
        fragments.get(i);


        }
    }

  public addFragment(Fragment fragment){
         fragments.add(fragment);
  }
    @Override
    public int getCount() {
        return fragments.size();
    }

在将Adapter设置为viewpager之前,向其添加片段:

 monthsPagerAdapter.add( new PageMonth1Fragment());
 monthsPagerAdapter.add(new PageMonth2Fragment();
 monthsPagerAdapter.add( new PageMonth3Fragment());
 monthsPagerAdapter.add(new PageMonth4Fragment());
 monthsPagerAdapter.add(new  PageMonth5Fragment());
 monthsPagerAdapter.add(new PageMonth6Fragment());
 monthsPagerAdapter.add(new PageMonth7Fragment());
 monthsPagerAdapter.add( new PageMonth8Fragment());
 monthsPagerAdapter.add(new  PageMonth9Fragment());
 monthsPagerAdapter.add(new  PageMonth10Fragment());
 monthsPagerAdapter.add(new  PageMonth11Fragment());
 monthsPagerAdapter.add( new PageMonth12Fragment());
 monthsPagerAdapter.add( new PageMonth13Fragment());

答案 1 :(得分:0)

无论何时切换片段,都尝试按id查找片段或按标签查找片段,如果它存在于内存中,则无需创建片段,

PageMonth1Fragment pageMonthFragment
if (findViewByTag(R.id.fragment1) != null) {
pageMonthFragment  = findViewById(R.id.fragment1);
}
// if the listing fragment is null then create new one
else{
  pageMonthFragment = new PageMonth1Fragment();
}

getSupportFragmentManager()。beginTransaction()                 .add(R.id.fragment_container,firstFragment).commit();

或者您可以根据需要将其添加到堆栈中。

当您将fragemnt添加到Frament Manager时,还有另一件事是提供标签,以便您可以按标签找到该片段。就像这样:

getSupportFragmentManager().beginTransaction()
.add(pageMonthFragment,"pageMonthFragment");

为您在交易中添加的每个片段传递唯一标签。

答案 2 :(得分:0)

因为viewpager在最大堆栈中保留了3个片段 你有1 2 3 如果在第二个,则将1和3放在堆栈中 否则创建新的,如此处指示

getItem不应每次都创建新的,创建适配器时需要创建片段的实例 然后只返回这个实例 通常来说,有13个具有相同列表的片段是个坏主意)