2016-11-15 77 views
0

我正在寻找一种方法来刷新另一个片段(FragmentThree)中的活动片段(FragmentTwo)。我正在使用tabbar,它正在预加载下一个和上一个选项卡,因此不会更新视图。我必须进一步移动两个选项卡(FragmentFour或Five)才能刷新FragmentTwo视图。如何在FragmentThree中更改内容后立即刷新它?如何从其他片段(FragmentThree)的活动刷新FragmentTwo?

ViewPagerAdapter:

public class ViewPagerAdapter extends FragmentPagerAdapter { 

private ArrayList<Fragment> mFragmentList = new ArrayList<>(); 
private ArrayList<String> mFragmentListTitles = new ArrayList<>(); 

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

} 

@Override public Fragment getItem(int position) { 
    return mFragmentList.get(position); 
} 

public int getItemPosition(Object object) { 
    return POSITION_NONE; 
} 
@Override public int getCount() { 
    return mFragmentList.size(); 
} 

public void addFragment(Fragment fragment, String title){ 
    mFragmentList.add(fragment); 
    mFragmentListTitles.add(title); 
} 

@Override public CharSequence getPageTitle(int position) { 

    return mFragmentListTitles.get(position); 

} 
} 

FragmentTwo:

public class TabFragmentTwo extends Fragment { 

private static final String ARG_EXAMPLE = "this_is_a_constant"; 

private String example_data; 
private Bundle bundle; 
private static ArrayList<SupermarketData> ArrayOfSupermarkets = new ArrayList<>(); 
private static ArrayList<SupermarketData> ArrayOfSupermarkets2 = new ArrayList<>(); 
Firebase mRef; 
private String PREFS_NAME = "SelectionsSettings"; 
Context context; 

public TabFragmentTwo() { 

} 

public static TabFragmentTwo newInstance(String example_argument) { 
    TabFragmentTwo tabFragmentTwo = new TabFragmentTwo(); 
    Bundle args = new Bundle(); 
    args.putString(ARG_EXAMPLE, example_argument); 
    tabFragmentTwo.setArguments(args); 
    return tabFragmentTwo; 
} 

@Override public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    example_data = getArguments().getString(ARG_EXAMPLE); 
    Log.i("Fragment created with ", example_data); 
    bundle = getArguments(); 
    ArrayOfSupermarkets = TabBarActivity.ArrayOfSupermarkets; 
    ArrayOfSupermarkets2 = TabBarActivity.ArrayOfSupermarkets2; 
} 



int[] mThumbIds = { 
     R.drawable.albert_heijn, R.drawable.centrum_mahaai, 
     R.drawable.goisco, R.drawable.mangusa, R.drawable.playa_piskado, 
     R.drawable.vreugedenhil 
}; 


@Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

    // Creating the actual gridview 
    View view = inflater.inflate(R.layout.supermarket_collection_view, container, false); 

    ListAdapter imageAdapterCollectionViewSupermarkets = new ImageAdapterCollectionViewSupermarkets(getActivity(), mThumbIds, ArrayOfSupermarkets); 
    GridView gridview = (GridView) view.findViewById(R.id.gridView2); 
    gridview.setAdapter(imageAdapterCollectionViewSupermarkets); 

    //Click item in gridview 
    gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

     public void onItemClick(AdapterView<?> parent, View v, 
           int position, long id) { 
      Toast.makeText(getActivity(), "You chose: " + ArrayOfSupermarkets.get(position).getSupermarketName(), 
        Toast.LENGTH_SHORT).show(); 

      Intent intent = new Intent(TabFragmentTwo.this.getActivity(), SupermarketListActivity.class); 
      // pass the item information 
      intent.putExtra("supermarketName", ArrayOfSupermarkets.get(position).getSupermarketName()); 
      System.out.print(ArrayOfSupermarkets.get(position).getSupermarketName()); 
      startActivity(intent); 

     } 
    }); 


    return view; 

} 

@Override 
public void onViewStateRestored(@Nullable Bundle savedInstanceState) { 
    super.onViewStateRestored(savedInstanceState); 

    mRef = new Firebase("x"); 

    mRef.child("supermarkets").addValueEventListener(new ValueEventListener() { 
     @Override 
     public void onDataChange(DataSnapshot dataSnapshot) { 
      System.out.println("There are " + dataSnapshot.getChildrenCount() + " supermarkets"); 

      ArrayOfSupermarkets.clear(); 
      ArrayOfSupermarkets2.clear(); 
      for (DataSnapshot supermarketSnapshot : dataSnapshot.getChildren()) { 

       SupermarketData supermarket = supermarketSnapshot.getValue(SupermarketData.class); 

       SharedPreferences preferences = getContext().getSharedPreferences(PREFS_NAME, getContext().MODE_PRIVATE); 

       ArrayOfSupermarkets2.add(supermarket); 

       if (!preferences.contains(supermarket.getSupermarketName())) { 
        ArrayOfSupermarkets.add(supermarket); 
       } 
       else { 
        if (preferences.getBoolean(supermarket.getSupermarketName(), false) == true) { 
         ArrayOfSupermarkets.add(supermarket); 
        } 
       } 

      } 

      class supermarketComparator implements Comparator<SupermarketData> { 
       public int compare(SupermarketData left, SupermarketData right) { 

        return left.getSupermarketName().compareTo(right.getSupermarketName()); 
       } 
      } 
      Collections.sort(ArrayOfSupermarkets, new supermarketComparator()); 
      Collections.sort(ArrayOfSupermarkets2, new supermarketComparator()); 

      System.out.println(ArrayOfSupermarkets.get(1).getSupermarketName()); 
      Log.d("MyTag", "Oke3!"); 

     } 

     @Override 
     public void onCancelled(FirebaseError firebaseError) { 
      Log.d("MyTag2", "Something went wrong!!"); 
     } 
    }); 



    System.out.println("Again!"); 

    ListAdapter imageAdapterCollectionViewSupermarkets = new ImageAdapterCollectionViewSupermarkets(getActivity(), mThumbIds, ArrayOfSupermarkets); 
    GridView gridview = (GridView) getActivity().findViewById(R.id.gridView2); 
    gridview.setAdapter(imageAdapterCollectionViewSupermarkets); 

} 
} 

FragmentThree:

public class TabFragmentThree extends Fragment { 

private static final String ARG_EXAMPLE = "this_is_a_constant"; 
private String example_data; 
private static ArrayList<SupermarketData> ArrayOfSupermarkets; 
private static ArrayList<SubCategoryData> ArrayOfSubCategories; 
private Boolean switchBoolean; 



public TabFragmentThree() { 

} 

public static TabFragmentThree newInstance(String example_argument) { 
    TabFragmentThree tabFragmentOne = new TabFragmentThree(); 
    Bundle args = new Bundle(); 
    args.putString(ARG_EXAMPLE, example_argument); 
    tabFragmentOne.setArguments(args); 
    return tabFragmentOne; 
} 

@Override public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    example_data = getArguments().getString(ARG_EXAMPLE); 
    Log.i("Fragment created with ", example_data); 
    ArrayOfSupermarkets = TabBarActivity.ArrayOfSupermarkets2; 
    ArrayOfSubCategories = TabBarActivity.ArrayOfSubCategories2; 
    switchBoolean = true; 


} 

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

    final ListAdapter customAdapterSelections = new CustomAdapterSelections (getActivity(), ArrayOfSupermarkets, ArrayOfSubCategories, switchBoolean);// Pass the product array to the constructor. 
    final ListView customListView = (ListView) view.findViewById(R.id.selectionListView); 
    customListView.setAdapter(customAdapterSelections); 

    final Button button = (Button) view.findViewById(R.id.selectionTypeButton); 
    button.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      System.out.println("Button"); 
      if (switchBoolean == true){ 
       switchBoolean = false; 
       button.setText("<- Select Supermarkets"); 
       ((TextView) view.findViewById(R.id.selectionsTitleLabel)).setText("Select your Subcategories"); 
       final ListAdapter customAdapterSelections = new CustomAdapterSelections (getActivity(), ArrayOfSupermarkets, ArrayOfSubCategories, switchBoolean);// Pass the product array to the constructor. 
       final ListView customListView = (ListView) view.findViewById(R.id.selectionListView); 
       customListView.setAdapter(customAdapterSelections); 

      } 
      else { 
       switchBoolean = true; 
       button.setText("Select Subcategories ->"); 
       ((TextView) view.findViewById(R.id.selectionsTitleLabel)).setText("Select your Supermarkets"); 
       final ListAdapter customAdapterSelections = new CustomAdapterSelections (getActivity(), ArrayOfSupermarkets, ArrayOfSubCategories, switchBoolean);// Pass the product array to the constructor. 
       final ListView customListView = (ListView) view.findViewById(R.id.selectionListView); 
       customListView.setAdapter(customAdapterSelections); 

      } 
     } 

    }); 

    return view; 
} 

@Override 
public void onStart() { 
    super.onStart(); 




} 
} 

CustomAdapterSelections:

public class CustomAdapterSelections extends BaseAdapter { 

private static final String ARG_EXAMPLE = "this_is_a_constant"; 
private String example_data; 
private ArrayList<SupermarketData> ArrayOfSupermarkets; 
private ArrayList<SubCategoryData> ArrayOfSubCategories; 
private Context context; 
private Boolean switchBoolean = false; 
private String PREFS_NAME = "SelectionsSettings"; 
private String DATA_TAG = "DATA_TAG"; 
private Boolean data = true; 


public CustomAdapterSelections(Context context, ArrayList<SupermarketData> arrayOfSupermarkets, ArrayList<SubCategoryData> arrayOfSubCategories, Boolean switchBoolean) { 
    this.context = context; 
    this.ArrayOfSupermarkets = arrayOfSupermarkets; 
    this.ArrayOfSubCategories = arrayOfSubCategories; 
    this.switchBoolean = switchBoolean; 

} 

@Override 
public int getCount() { 
    if (switchBoolean == true) { 
     return ArrayOfSupermarkets.size(); 
    } else { 
     return ArrayOfSubCategories.size(); 
    } 
} 
@Override 
public Object getItem(int position) { 
    return null; 
} 

@Override 
public long getItemId(int position) { 
    return 0; 
} 

public View getView(final int position, View convertView, ViewGroup parent) { 

    View view; 

    if (convertView == null) { 
     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     view = inflater.inflate(R.layout.selections_item_row, null); 

     System.out.println("Test1"); 
    } else { 
     view = convertView; 
     System.out.println("Test2"); 
    } 

    TextView textView = (TextView) view.findViewById(R.id.selectionName); 
    CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkBoxSelection); 


    checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 


     @Override 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
      if (switchBoolean == true) { 
       if (isChecked) { 
        // perform logic 
        SharedPreferences preferences = context.getSharedPreferences(PREFS_NAME, context.MODE_PRIVATE); 
        SharedPreferences.Editor editor = preferences.edit(); 
        editor.putBoolean(ArrayOfSupermarkets.get(position).getSupermarketName(), true); 
        editor.commit(); 
       } 
       else { 

        SharedPreferences preferences = context.getSharedPreferences(PREFS_NAME, context.MODE_PRIVATE); 
        SharedPreferences.Editor editor = preferences.edit(); 
        editor.putBoolean(ArrayOfSupermarkets.get(position).getSupermarketName(), false); 
        editor.commit(); 
       } 

      } 
      else{ 
       if (isChecked) { 
        // perform logic 
        SharedPreferences preferences = context.getSharedPreferences(PREFS_NAME, context.MODE_PRIVATE); 
        SharedPreferences.Editor editor = preferences.edit(); 
        editor.putBoolean(ArrayOfSubCategories.get(position).getProductSubCategory(), true); 
        editor.commit(); 
       } 
       else { 

        SharedPreferences preferences = context.getSharedPreferences(PREFS_NAME, context.MODE_PRIVATE); 
        SharedPreferences.Editor editor = preferences.edit(); 
        editor.putBoolean(ArrayOfSubCategories.get(position).getProductSubCategory(), false); 
        editor.commit(); 
       } 
      } 

     } 


    }); 


    if (switchBoolean == true) { 
     textView.setText(ArrayOfSupermarkets.get(position).getSupermarketName()); 

     SharedPreferences preferences = context.getSharedPreferences(PREFS_NAME, context.MODE_PRIVATE); 

     if(preferences.contains(ArrayOfSupermarkets.get(position).getSupermarketName())) { 
      Boolean value = preferences.getBoolean(ArrayOfSupermarkets.get(position).getSupermarketName(), false); 
      checkBox.setChecked(value); 
     } 
     else{ 
      checkBox.setChecked(true); 
     } 





    } 
    else{ 
     textView.setText(ArrayOfSubCategories.get(position).getProductSubCategory()); 

     SharedPreferences preferences = context.getSharedPreferences(PREFS_NAME, context.MODE_PRIVATE); 

     if(preferences.contains(ArrayOfSubCategories.get(position).getProductSubCategory())) { 
      Boolean value = preferences.getBoolean(ArrayOfSubCategories.get(position).getProductSubCategory(), false); 
      checkBox.setChecked(value); 
     } 
     else { 
      checkBox.setChecked(true); 
     } 

    } 




    return view; 
} 



} 
+0

1 - 在第三个片段中创建接口 2 - 在第二个片段中实现该接口以更新数据 – Bhavnik

回答

0

当你去到第二个片段至第三片段时您可以添加一条线可以使用两种方法

  1. TargetFragment 。 fragmentTwo.setTargetFragment(CurrentFragment,RequestCode); 通过使用TargetFragment可以将第二个片段的公共方法调用为第三个片段

  2. 接口。 在活动中创建一个接口并设置为片段并执行。

对不起,我的英文不好。

+0

感谢您的回答。我尝试过但我无法获得第一个选项。你有没有我应该如何实施的例子? –

+0

ThirdFragment frag = new ThirdFragment(); 碎片。 setTargetFragment(this,0); ().getSimpleName())。commit()。().getFragmentManager()。beginFragmentTransaction()。add(R.id.container,frag).addTobackStack(frag.getClass()。getSimpleName() –

相关问题