从 Activity 获取 Spinner Value 到我的适配器中
Get the Spinner Value from Activity into my adapter
我在 Activity 的工具栏中设置了微调器。
在我的 activity 中,我有一个 FragmentPagerAdapter
,其中填充了五个 Pages
,来自片段的 newInstance
。
将微调器更改为片段后,如何获得微调器的价值。我正在使用此微调器在片段页面中的 RecyclerView
中设置 Single/Multi 选择模式。
我知道如何在我的 activity 中获取值,甚至将它传递给 FragmentPagerAdapter
,但我的问题是如果我更改它,为了让它在我的 RecyclerView
中生效,我必须重新加载 Activity.
我的Activity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pattern_selector);
Intent intent = getIntent();
patternImages = intent.getParcelableArrayListExtra(PATTERN_IMAGES);
patternSelectedMode = intent.getIntExtra(PATTERN_SELECTED_MODE, 0);
titles = getResources().getStringArray(R.array.tabs_pattern_title);
// spinnerItems = getResources().getStringArray(R.array.spinner_pattern_selector);
setUpPagerAdapter();
setUpToolbar();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_pattern_selector, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
View mainContent = findViewById(R.id.view_pager_container);
if (mainContent != null) {
mainContent.setAlpha(0);
mainContent.animate().alpha(1).setDuration(MAIN_CONTENT_FADE_IN_DURATION);
} else {
Log.d(TAG, "No view with ID main_content to fade in.");
}
}
private void setUpPagerAdapter() {
patternsPagerAdapter = new PatternsPagerAdapter(getSupportFragmentManager(), this);
for (int i = 0; i < titles.length; i++) {
switch (i) {
case 0:
minIndex = 0;
maxIndex = 3;
fragmentTitle = titles[i];
break;
case 1:
minIndex = 3;
maxIndex = 50;
fragmentTitle = titles[i];
break;
case 2:
minIndex = 50;
maxIndex = 60;
fragmentTitle = titles[i];
break;
case 3:
minIndex = 60;
maxIndex = 86;
fragmentTitle = titles[i];
break;
case 4:
minIndex = 0;
maxIndex = 0;
fragmentTitle = titles[i];
break;
}
Bundle bundle = new Bundle();
bundle.putInt(MIN_NUMBERS, minIndex);
bundle.putInt(MAX_NUMBERS, maxIndex);
bundle.putInt(PATTERN_SELECTED_MODE, patternSelectedMode);
bundle.putString(TITLE, fragmentTitle);
patternsPagerAdapter.addFragment(FragmentCommonPatterns.newInstance(minIndex, maxIndex, fragmentTitle, patternSelectedMode));
}
viewPager = (ViewPager) findViewById(R.id.view_pager_container);
viewPager.setAdapter(patternsPagerAdapter);
tabLayout = (TabLayout) findViewById(R.id.pattern_tab_layout);
tabLayout.setupWithViewPager(viewPager);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
//TODO Check if I need this
@Override
public void onTabSelected(TabLayout.Tab tab) {
patternsPagerAdapter.getItem(tab.getPosition());
// System.out.println("Fragment Selected " + tab.getText().toString());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
// System.out.println("Fragment Deselected " + tab.getText().toString());
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
// System.out.println("Fragment Reselected " + tab.getText().toString());
}
});
}
public void setUpToolbar() {
toolbar = (Toolbar) findViewById(R.id.toolbar_pattern_selector);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
SpinnerAdapter spinnerAdapter = ArrayAdapter.createFromResource(this, R.array.spinner_pattern_selector, R.layout.item_spinner_pattern_selector);
spinner = new Spinner(getSupportActionBar().getThemedContext());
spinner.setAdapter(spinnerAdapter);
toolbar.addView(spinner, 0);
spinner.setSelection(patternSelectedMode);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 1:
patternSelectedMode = 1;
Toast.makeText(getApplicationContext(), getResources().getString(R.string.pattern_image_selected_single), Toast.LENGTH_SHORT).show();
for (PatternImages current : patternImages) {
current.setSelected(false);
}
patternsPagerAdapter.notifyDataSetChanged();
break;
case 2:
patternSelectedMode = 2;
Toast.makeText(getApplicationContext(), getResources().getString(R.string.pattern_image_selected_multiple), Toast.LENGTH_SHORT).show();
patternsPagerAdapter.notifyDataSetChanged();
break;
default:
patternSelectedMode = 0;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
public ArrayList<PatternImages> getPatternImages() {
return patternImages;
}
public int getSpinnerValue() {
return patternSelectedMode;
}
public void setPattern(MenuItem item) {
Intent returnIntent = new Intent();
returnIntent.putParcelableArrayListExtra(PATTERN_IMAGES, patternImages);
returnIntent.putExtra(PATTERN_SELECTED_MODE, patternSelectedMode);
setResult(RESULT_OK, returnIntent);
finish();
overridePendingTransition(R.anim.in_left, R.anim.out_right);
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
// spinner.startAnimation(AnimationUtils.loadAnimation(this, R.anim.get_attention));
// Log.d(TAG, "onWindowFocusChanged Started");
super.onWindowFocusChanged(hasFocus);
}
@Override
public void onBackPressed() {
if (tabLayout.getSelectedTabPosition() > 0) {
viewPager.setCurrentItem(tabLayout.getSelectedTabPosition() - 1);
} else {
Intent returnIntent = new Intent();
returnIntent.putParcelableArrayListExtra(PATTERN_IMAGES, patternImages);
returnIntent.putExtra(PATTERN_SELECTED_MODE, patternSelectedMode);
setResult(RESULT_CANCELED, returnIntent);
finish();
overridePendingTransition(R.anim.in_left, R.anim.out_right);
super.onBackPressed();
}
}
片段
public static FragmentCommonPatterns newInstance(int minIndex, int maxIndex, String fragmentTitle, int patternSelectedMode) {
FragmentCommonPatterns patterns = new FragmentCommonPatterns();
Bundle args = new Bundle();
args.putInt(MIN_NUMBERS, minIndex);
args.putInt(MAX_NUMBERS, maxIndex);
args.putInt(PATTERN_SELECTED_MODE, patternSelectedMode);
args.putString(TITLE, fragmentTitle);
patterns.setArguments(args);
return patterns;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate: ");
minIndex = getArguments().getInt(MIN_NUMBERS);
maxIndex = getArguments().getInt(MAX_NUMBERS);
fragmentTitle = getArguments().getString(TITLE);
titles = getResources().getStringArray(R.array.tabs_pattern_title);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_common_patterns, container, false);
if (fragmentTitle.equals(titles[4])) {
floatingActionButton = (FloatingActionButton) view.findViewById(R.id.fab_custom_patterns);
floatingActionButton.setVisibility(View.VISIBLE);
floatingActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "Edit your own custom patterns", Toast.LENGTH_SHORT).show();
//TODO
}
});
}
ActivityPatternSelector activityPatternSelector = (ActivityPatternSelector) getActivity();
patternImages = activityPatternSelector.getPatternImages();
patternSelectedMode=activityPatternSelector.getSpinnerValue();
setUpPatternsRecyclerView(view);
return view;
}
private void setUpPatternsRecyclerView(View view) {
RecyclerView recyclerPatternsView = (RecyclerView) view.findViewById(R.id.pattern_image_recycler_view);
PatternImageAdapter adapter = new PatternImageAdapter(getActivity(), patternImages.subList(minIndex, maxIndex), patternSelectedMode);
recyclerPatternsView.setAdapter(adapter);
ColumnQty columnQty = new ColumnQty(getActivity(), R.layout.item_image_pattern_cardview);
GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), columnQty.calculateNoOfColumns());
recyclerPatternsView.setHasFixedSize(true);
recyclerPatternsView.setLayoutManager(gridLayoutManager);
recyclerPatternsView.setItemAnimator(new DefaultItemAnimator());
recyclerPatternsView.addItemDecoration(new GridSpacing(columnQty.calculateSpacing()));
}
您可以向您的片段添加一个 "updatePattern" 方法。
然后遍历适配器中的片段并更新它们。
伪代码:
for (int i = 0 ; i < fragmentsArray.size() ; i ++) {
Fragment fragment = fragmentPagerAdapter.getItem(i);
fragment.updateSelectedPatternMode(newPattern);
}
PS:对于 cleanred 代码,您可以将迭代片段封装在 PagerAdapter 中,并向适配器添加 public 更新方法。
我在 Activity 的工具栏中设置了微调器。
在我的 activity 中,我有一个 FragmentPagerAdapter
,其中填充了五个 Pages
,来自片段的 newInstance
。
将微调器更改为片段后,如何获得微调器的价值。我正在使用此微调器在片段页面中的 RecyclerView
中设置 Single/Multi 选择模式。
我知道如何在我的 activity 中获取值,甚至将它传递给 FragmentPagerAdapter
,但我的问题是如果我更改它,为了让它在我的 RecyclerView
中生效,我必须重新加载 Activity.
我的Activity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pattern_selector);
Intent intent = getIntent();
patternImages = intent.getParcelableArrayListExtra(PATTERN_IMAGES);
patternSelectedMode = intent.getIntExtra(PATTERN_SELECTED_MODE, 0);
titles = getResources().getStringArray(R.array.tabs_pattern_title);
// spinnerItems = getResources().getStringArray(R.array.spinner_pattern_selector);
setUpPagerAdapter();
setUpToolbar();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_pattern_selector, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
View mainContent = findViewById(R.id.view_pager_container);
if (mainContent != null) {
mainContent.setAlpha(0);
mainContent.animate().alpha(1).setDuration(MAIN_CONTENT_FADE_IN_DURATION);
} else {
Log.d(TAG, "No view with ID main_content to fade in.");
}
}
private void setUpPagerAdapter() {
patternsPagerAdapter = new PatternsPagerAdapter(getSupportFragmentManager(), this);
for (int i = 0; i < titles.length; i++) {
switch (i) {
case 0:
minIndex = 0;
maxIndex = 3;
fragmentTitle = titles[i];
break;
case 1:
minIndex = 3;
maxIndex = 50;
fragmentTitle = titles[i];
break;
case 2:
minIndex = 50;
maxIndex = 60;
fragmentTitle = titles[i];
break;
case 3:
minIndex = 60;
maxIndex = 86;
fragmentTitle = titles[i];
break;
case 4:
minIndex = 0;
maxIndex = 0;
fragmentTitle = titles[i];
break;
}
Bundle bundle = new Bundle();
bundle.putInt(MIN_NUMBERS, minIndex);
bundle.putInt(MAX_NUMBERS, maxIndex);
bundle.putInt(PATTERN_SELECTED_MODE, patternSelectedMode);
bundle.putString(TITLE, fragmentTitle);
patternsPagerAdapter.addFragment(FragmentCommonPatterns.newInstance(minIndex, maxIndex, fragmentTitle, patternSelectedMode));
}
viewPager = (ViewPager) findViewById(R.id.view_pager_container);
viewPager.setAdapter(patternsPagerAdapter);
tabLayout = (TabLayout) findViewById(R.id.pattern_tab_layout);
tabLayout.setupWithViewPager(viewPager);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
//TODO Check if I need this
@Override
public void onTabSelected(TabLayout.Tab tab) {
patternsPagerAdapter.getItem(tab.getPosition());
// System.out.println("Fragment Selected " + tab.getText().toString());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
// System.out.println("Fragment Deselected " + tab.getText().toString());
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
// System.out.println("Fragment Reselected " + tab.getText().toString());
}
});
}
public void setUpToolbar() {
toolbar = (Toolbar) findViewById(R.id.toolbar_pattern_selector);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
SpinnerAdapter spinnerAdapter = ArrayAdapter.createFromResource(this, R.array.spinner_pattern_selector, R.layout.item_spinner_pattern_selector);
spinner = new Spinner(getSupportActionBar().getThemedContext());
spinner.setAdapter(spinnerAdapter);
toolbar.addView(spinner, 0);
spinner.setSelection(patternSelectedMode);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 1:
patternSelectedMode = 1;
Toast.makeText(getApplicationContext(), getResources().getString(R.string.pattern_image_selected_single), Toast.LENGTH_SHORT).show();
for (PatternImages current : patternImages) {
current.setSelected(false);
}
patternsPagerAdapter.notifyDataSetChanged();
break;
case 2:
patternSelectedMode = 2;
Toast.makeText(getApplicationContext(), getResources().getString(R.string.pattern_image_selected_multiple), Toast.LENGTH_SHORT).show();
patternsPagerAdapter.notifyDataSetChanged();
break;
default:
patternSelectedMode = 0;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
public ArrayList<PatternImages> getPatternImages() {
return patternImages;
}
public int getSpinnerValue() {
return patternSelectedMode;
}
public void setPattern(MenuItem item) {
Intent returnIntent = new Intent();
returnIntent.putParcelableArrayListExtra(PATTERN_IMAGES, patternImages);
returnIntent.putExtra(PATTERN_SELECTED_MODE, patternSelectedMode);
setResult(RESULT_OK, returnIntent);
finish();
overridePendingTransition(R.anim.in_left, R.anim.out_right);
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
// spinner.startAnimation(AnimationUtils.loadAnimation(this, R.anim.get_attention));
// Log.d(TAG, "onWindowFocusChanged Started");
super.onWindowFocusChanged(hasFocus);
}
@Override
public void onBackPressed() {
if (tabLayout.getSelectedTabPosition() > 0) {
viewPager.setCurrentItem(tabLayout.getSelectedTabPosition() - 1);
} else {
Intent returnIntent = new Intent();
returnIntent.putParcelableArrayListExtra(PATTERN_IMAGES, patternImages);
returnIntent.putExtra(PATTERN_SELECTED_MODE, patternSelectedMode);
setResult(RESULT_CANCELED, returnIntent);
finish();
overridePendingTransition(R.anim.in_left, R.anim.out_right);
super.onBackPressed();
}
}
片段
public static FragmentCommonPatterns newInstance(int minIndex, int maxIndex, String fragmentTitle, int patternSelectedMode) {
FragmentCommonPatterns patterns = new FragmentCommonPatterns();
Bundle args = new Bundle();
args.putInt(MIN_NUMBERS, minIndex);
args.putInt(MAX_NUMBERS, maxIndex);
args.putInt(PATTERN_SELECTED_MODE, patternSelectedMode);
args.putString(TITLE, fragmentTitle);
patterns.setArguments(args);
return patterns;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate: ");
minIndex = getArguments().getInt(MIN_NUMBERS);
maxIndex = getArguments().getInt(MAX_NUMBERS);
fragmentTitle = getArguments().getString(TITLE);
titles = getResources().getStringArray(R.array.tabs_pattern_title);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_common_patterns, container, false);
if (fragmentTitle.equals(titles[4])) {
floatingActionButton = (FloatingActionButton) view.findViewById(R.id.fab_custom_patterns);
floatingActionButton.setVisibility(View.VISIBLE);
floatingActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "Edit your own custom patterns", Toast.LENGTH_SHORT).show();
//TODO
}
});
}
ActivityPatternSelector activityPatternSelector = (ActivityPatternSelector) getActivity();
patternImages = activityPatternSelector.getPatternImages();
patternSelectedMode=activityPatternSelector.getSpinnerValue();
setUpPatternsRecyclerView(view);
return view;
}
private void setUpPatternsRecyclerView(View view) {
RecyclerView recyclerPatternsView = (RecyclerView) view.findViewById(R.id.pattern_image_recycler_view);
PatternImageAdapter adapter = new PatternImageAdapter(getActivity(), patternImages.subList(minIndex, maxIndex), patternSelectedMode);
recyclerPatternsView.setAdapter(adapter);
ColumnQty columnQty = new ColumnQty(getActivity(), R.layout.item_image_pattern_cardview);
GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), columnQty.calculateNoOfColumns());
recyclerPatternsView.setHasFixedSize(true);
recyclerPatternsView.setLayoutManager(gridLayoutManager);
recyclerPatternsView.setItemAnimator(new DefaultItemAnimator());
recyclerPatternsView.addItemDecoration(new GridSpacing(columnQty.calculateSpacing()));
}
您可以向您的片段添加一个 "updatePattern" 方法。
然后遍历适配器中的片段并更新它们。
伪代码:
for (int i = 0 ; i < fragmentsArray.size() ; i ++) {
Fragment fragment = fragmentPagerAdapter.getItem(i);
fragment.updateSelectedPatternMode(newPattern);
}
PS:对于 cleanred 代码,您可以将迭代片段封装在 PagerAdapter 中,并向适配器添加 public 更新方法。