使用选项卡从列表创建片段

时间:2013-09-27 14:21:36

标签: javascript android-listview android-fragmentactivity

在学习代码和现在之间的很长一段时间之后,我正在尝试创建一个使用卡列表视图并使用标签的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;
            }      

我将以下内容放入我的片段中,我确信这是错误的。

    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个小时试图解决这个问题。感谢。

1 个答案:

答案 0 :(得分:0)

我建议为单独的内容制作单独的片段。

从片段中的onCreateView()方法中扩充布局,并从那里初始化列表视图。从MainActivity中删除listview代码并将其放入片段中。

对于您的ViewPager,请将其更改为以下内容:

@Override
        public Fragment getItem(int position) {
                Fragment f = new Fragment();
                switch(position){
                case 0:
                        f= new FragmentTheme();     
                        break;
                case 1:
                        f= new FragmentExtras();     
                        break;
                case 2:
                        f= new FragmentContact();     
                        break;
                }
                return f;
        }

希望这会有所帮助。我修改了你的github repo以反映这些变化。

如果它解决了您的问题,请务必接受此答案。