我目前正在开发我的第一个Android应用程序。现在,我真的不知道如何自己处理这个问题。我的主要布局实现了 ViewPager :
<android.support.v4.view.ViewPager
android:id="@+id/image_swipe"
android:layout_width="match_parent"
android:layout_height="590dp"
android:layout_marginTop="130dp" />
主布局还实现了一个工具栏,该工具栏拥有此菜单项:
<item
android:id="@+id/show_favorites"
android:orderInCategory="100"
android:icon="@drawable/heart_red"
android:title="@string/show_favorites" />
我基本上想做的事情是:当用户点击id为@show_favorites的菜单项时,应该过滤ViewPager的适配器。 ViewPager中显示的每个项目都有一个ImageButton(favouriteButton)。按钮可以是灰色或红色。按钮状态存储为SharedPreferences,表示:每个项目都有自己的键fav1 - fav10(10个项目),此键值为0(灰色)或值1(红色)。因此,当有人点击菜单项时,我只想在ViewPager中显示标记为收藏的项目(因此在SharedPreferences中存储1)。必须隐藏所有带0的项目。
我的适配器类看起来如下所示:
public class SwipeAdapter extends PagerAdapter {
/*
Define the image resources for our image fragment
*/
private int[] image_resources = {
R.drawable.island_maldives,
R.drawable.island_kreta,
R.drawable.island_australia,
R.drawable.island_kosamui,
R.drawable.island_domrep,
R.drawable.island_bahamas,
R.drawable.island_mallorca,
R.drawable.island_philippines,
R.drawable.island_seychelles,
R.drawable.island_borabora
};
private Context context;
private LayoutInflater layoutInflater;
public SwipeAdapter(Context context) {
this.context = context;
}
/*
Count the images in image_resources array
*/
@Override
public int getCount() {
return image_resources.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return (view == (LinearLayout) object);
}
/*
Create the page for the given position
Set image resources and text about current position
*/
@Override
public Object instantiateItem(ViewGroup container, int position) {
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View item_view = layoutInflater.inflate(R.layout.swipe_layout,container,false);
ImageView imageView = (ImageView) item_view.findViewById(R.id.swipe_image);
TextView textView = (TextView) item_view.findViewById(R.id.image_count);
ImageButton imageButton = (ImageButton) item_view.findViewById(R.id.favourite_button);
imageView.setImageResource(image_resources[position]);
// set tag for details
imageView.setTag(position);
textView.setText("["+(position+1)+"/10]");
SharedPreferences prefs = context.getSharedPreferences("favInfo", Context.MODE_PRIVATE);
// get the unique key consisting of fav and the current item position
String key = "fav" + (position+1);
imageButton.setTag(position+1);
String value = prefs.getString(key, "0");
// check if value of favorite key is 0
// set image resource dependent on what value is set
if (value.equals("1")) {
imageButton.setImageResource(R.drawable.heart_red);
} else {
imageButton.setImageResource(R.drawable.heart);
}
container.addView(item_view);
return item_view;
}
/*
Remove a page for the given position
*/
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout) object);
}
}
我的主要活动以这种方式组织:
public class SuggestionActivity extends AppCompatActivity {
ViewPager viewPager;
SwipeAdapter adapter;
private final AppCompatActivity activity = SuggestionActivity.this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_suggestion);
viewPager = (ViewPager) findViewById(R.id.image_swipe);
adapter = new SwipeAdapter(this);
viewPager.setAdapter(adapter);
// set the toolbar for the suggestion page
Toolbar sToolbar = (Toolbar) findViewById(R.id.suggestion_toolbar);
setSupportActionBar(sToolbar);
};
/*
Add options menu at view start
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.suggestion_menu, menu);
return super.onCreateOptionsMenu(menu);
}
/*
Declare what happens when user clicks an item of the options menu
@go_home start the CategoryActivity
@filter TO DO
*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.go_home:
Intent homeIntent = new Intent(activity, CategoryActivity.class);
startActivity(homeIntent);
return true;
case R.id.show_favorites:
Intent favIntent = new Intent(activity, SuggestionActivity.class);
//
// HERE IS WHERE I DON'T KNOW WHAT TO DO
//
startActivity(favIntent);
return true;
default:
// If we got here, the user's action was not recognized.
// Invoke the superclass to handle it.
return super.onOptionsItemSelected(item);
}
}
/*
Method for click actions on the favorite button
@heart grey heart symbolizes no favorite (flag 0)
@heart_red red heart symbolizes marked as favorite (flag 1)
*/
public void onClickFav(View v) {
ImageButton favouriteButton = (ImageButton) v;
SharedPreferences prefs = getSharedPreferences("favInfo", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
// get the unique key by getting the tag of the image button
String c = v.getTag().toString();
String key = "fav"+c;
// get value stored with that key, default: if no value exists take 0
String value = prefs.getString(key, "0");
// check if value of favorite key is 0,
// set image resource dependent on what value is stored behind the key
if (value.equals("0")) {
favouriteButton.setImageResource(R.drawable.heart_red);
} else {
favouriteButton.setImageResource(R.drawable.heart);
}
// do some magic text dependent on what value is set
if (value.equals("1")) {
Toast.makeText(SuggestionActivity.this, "No favourite anymore...",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(SuggestionActivity.this, "Woah, new favourite!",
Toast.LENGTH_SHORT).show();
}
String isFav;
// switch value to opposite
if (value.equals("0")) {
isFav = "1";
} else {
isFav = "0";
}
// store shared preferences
editor.putString(key, isFav);
editor.commit();
}
}
总之: ViewPager中每个项目的ImageButton状态已经以标记0/1的形式存储为SharedPreferenecs。点击id为@show_favorites的菜单项后,我只想显示1为存储值的项目。
我该怎么做? 谢谢,征求您的意见!