在学习编码和现在之间的很长一段时间之后,我正在尝试创建一个使用卡片列表视图并使用选项卡的 Android 应用程序。
我希望能够在所有 3 个选项卡中都有卡片列表,但每个选项卡都有不同的列表。现在我所做的,我在所有 3 个选项卡中都得到了相同的设置。我知道我做错了什么,但是在阅读了片段和标签之后,我无法弄清楚如何实现这一点。
我的 MainActivity 中有以下内容。
public class MainActivity extends FragmentActivity implements Card.CardMenuListener<Card>{
private final Handler handler = new Handler();
private PagerSlidingTabStrip tabs;
private ViewPager pager;
private MyPagerAdapter adapter;
private Drawable oldBackground = null;
private int currentColor = 0xFF3F9FE0;
@Override
public void onCreate(Bundle savedInstanceState) {
// This is quick way of theming the action bar without using styles.xml (e.g. using ActionBar Style Generator)
getActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(android.R.color.holo_blue_dark)));
getActionBar().setDisplayShowHomeEnabled(false);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
pager = (ViewPager) findViewById(R.id.pager);
adapter = new MyPagerAdapter(getSupportFragmentManager());
pager.setAdapter(adapter);
// Initializes a CardAdapter with a blue accent color and basic popup menu for each card
CardAdapter<Card> cardsAdapter = new CardAdapter<Card>(this)
.setAccentColorRes(android.R.color.holo_blue_light)
.setPopupMenu(R.menu.card_popup, this);
cardsAdapter.add(new CardHeader(this, R.string.themeheader));
cardsAdapter.add(new Card("Action", "Launcher")
.setThumbnail(this, R.drawable.apps_actionlauncherpro)); // sets a thumbnail image from drawable resources
cardsAdapter.add(new Card("ADW", "Launcher")
.setThumbnail(this, R.drawable.apps_adwex)); // sets a thumbnail image from drawable resources
cardsAdapter.add(new Card("Apex", "Launcher")
.setThumbnail(this, R.drawable.apps_apexlauncher)); // sets a thumbnail image from drawable resources
cardsAdapter.add(new Card("Nova", "Launcher")
.setThumbnail(this, R.drawable.apps_novalauncher)); // sets a thumbnail image from drawable resources
CardListView cardsList = (CardListView) findViewById(R.id.cardsList);
cardsList.setAdapter(cardsAdapter);
final int pageMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getResources()
.getDisplayMetrics());
pager.setPageMargin(pageMargin);
tabs.setViewPager(pager);
}
@Override
public void onMenuItemClick(Card card, MenuItem item) {
Toast.makeText(this, card.getTitle() + ": " + item.getTitle(), Toast.LENGTH_SHORT).show();
}
我有这个来设置选项卡以提供我想要的 3 个
public class MyPagerAdapter extends FragmentPagerAdapter {
private final int[] TITLES = { R.string.tab1, R.string.tab2, R.string.tab3 };
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.tab1).toUpperCase(l);
case 1:
return getString(R.string.tab2).toUpperCase(l);
case 2:
return getString(R.string.tab3).toUpperCase(l);
}
return null;
}
@Override
public Fragment getItem(int position) {
Fragment f = new Fragment();
switch(position){
case 0:
f=ThemeCardFragment.newInstance(position);
break;
case 1:
f=ThemeCardFragment.newInstance(position);
break;
}
return f;
}
@Override
public int getCount() {
return TITLES.length;
}
我将以下内容放在我的 Fragment 中,我确信这是错误的。
public class ThemeCardFragment extends Fragment {
private static final String ARG_POSITION = "position";
private int position;
public static ThemeCardFragment newInstance(int position) {
ThemeCardFragment f = new ThemeCardFragment();
Bundle b = new Bundle();
b.putInt(ARG_POSITION, position);
f.setArguments(b);
return f;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
position = getArguments().getInt(ARG_POSITION);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
FrameLayout fl = new FrameLayout(getActivity());
fl.setLayoutParams(params);
return fl;
}
}
我试图在这里查看示例、教程和 Android 开发人员,但我错过了一些可以让我这样做的东西。
实际上,我想定义
("Action", "Launcher")
在 CardsAdapter 作为我的 Strings.xml 中的字符串
任何能指出我正确方向的见解将不胜感激。我似乎一直在朝着错误的方向前进,并且花了至少 10 个小时试图弄清楚。谢谢。