将旧活动转换为碎片

时间:2015-06-08 10:48:26

标签: android android-fragments

我想使用导航抽屉列出我的活动,但你必须在抽屉中使用片段,所以我想将旧活动转换为片段。

不确定我是否必须扩展Fragment或FragmentActivity以及我在哪里粘贴我的所有UI组件,例如工具栏等。

活动是使用ListView滑动标签

public class SkillsPsActivity extends AppCompatActivity {

static ListViewAdapterOne mAdapterOne;
static ListViewAdapterTwo mAdapterTwo;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_skills_ps);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    assert getSupportActionBar() != null;
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
    pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
    PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
    tabs.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
    tabs.setIndicatorColor(getResources().getColor(R.color.colorAccent));
    tabs.setDividerColor(getResources().getColor(R.color.colorPrimary));
    tabs.setTextColor(getResources().getColor(R.color.colorTest));
    tabs.setViewPager(pager);
}



class MyPagerAdapter extends FragmentPagerAdapter {

    String[] skills_tabs;


    public MyPagerAdapter(FragmentManager fm) {
        super(fm);
        skills_tabs = getResources().getStringArray(R.array.skills_tabs);
    }

    @Override
    public Fragment getItem(int position) {

        return MyFragment.getInstance(position);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return skills_tabs[position];
    }

    @Override
    public int getCount() {
        return 2;
    }
}


public static class MyFragment extends Fragment {
    public static final String TAB_POSITION_KEY = "TAB_POSITION_KEY";
    private ListView mainListView;
    private ArrayAdapter<String> listAdapter;


    public static MyFragment getInstance(int position) {

        MyFragment myFragment = new MyFragment();
        Bundle args = new Bundle();
        args.putInt(TAB_POSITION_KEY, position);
        myFragment.setArguments(args);
        return myFragment;
    }

    @Override

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

        View layout = inflater.inflate(R.layout.fragment_my, container, false);


        Bundle bundle = getArguments();

        int position = bundle.getInt(TAB_POSITION_KEY);

        ArrayList<String> collection = new ArrayList<String>();

        mainListView = (ListView) layout.findViewById(R.id.mainListView);

        switch (position) {

            case 0:

                mAdapterOne=new ListViewAdapterOne();
                mainListView.setAdapter(mAdapterOne);

                break;

            case 1:

                mAdapterTwo=new ListViewAdapterTwo();
                mainListView.setAdapter(mAdapterTwo);

                break;


            default:



                Toast.makeText(getActivity().getBaseContext(), "check tabs position", Toast.LENGTH_SHORT).show();



        }

        return layout;
    }





    class ListViewAdapterOne extends BaseAdapter {


        String text1[]={"Ball Juggle (while standing)","Add Foot Fake (while standing)"};

        int text2[]={R.drawable.ic_star,R.drawable.ic_star};

        String text3[]={"Hold","Hold"};

        int text4[]={R.drawable.image_l2,R.drawable.image_ls};

        String text5[]={"+",""};

        int text6[]={R.drawable.ic_star,R.drawable.ic_star};

        String text7[]={"Tap",""};

        int text8[]={R.drawable.image_r1,R.drawable.ic_star};

        String text9[]={"",""};

        int text10[]={R.drawable.ic_star,R.drawable.ic_star};

        String text11[]={"",""};

        int text12[]={R.drawable.ic_star,R.drawable.ic_star};

        String text13[]={"",""};

        int text14[]={R.drawable.ic_star,R.drawable.ic_star};





        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return text1.length;
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return text1[position];
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub



            LayoutInflater infla=getActivity().getLayoutInflater();


            View v = infla.inflate(R.layout.list_view_layout, null);



            TextView tv1 = (TextView) v.findViewById(R.id.textView1);
            ImageView iv1 = (ImageView) v.findViewById(R.id.imageView1);
            TextView tv2 = (TextView) v.findViewById(R.id.textView2);
            ImageView iv2 = (ImageView) v.findViewById(R.id.imageView2);
            TextView tv3 = (TextView) v.findViewById(R.id.textView3);
            ImageView iv3 = (ImageView) v.findViewById(R.id.imageView3);
            TextView tv4 = (TextView) v.findViewById(R.id.textView4);
            ImageView iv4 = (ImageView) v.findViewById(R.id.imageView4);
            TextView tv5 = (TextView) v.findViewById(R.id.textView5);
            ImageView iv5 = (ImageView) v.findViewById(R.id.imageView5);
            TextView tv6 = (TextView) v.findViewById(R.id.textView6);
            ImageView iv6 = (ImageView) v.findViewById(R.id.imageView6);
            TextView tv7 = (TextView) v.findViewById(R.id.textView7);
            ImageView iv7 = (ImageView) v.findViewById(R.id.imageView7);




            tv1.setText(text1[position]);
            iv1.setImageResource(text2[position]);
            tv2.setText(text3[position]);
            iv2.setImageResource(text4[position]);
            tv3.setText(text5[position]);
            iv3.setImageResource(text6[position]);
            tv4.setText(text7[position]);
            iv4.setImageResource(text8[position]);
            tv5.setText(text9[position]);
            iv5.setImageResource(text10[position]);
            tv6.setText(text11[position]);
            iv6.setImageResource(text12[position]);
            tv7.setText(text13[position]);
            iv7.setImageResource(text14[position]);



            if(text2[position]==R.drawable.ic_star){
                iv1.setVisibility(View.GONE);
            }
            if(text3[position].matches("")) {
                tv2.setVisibility(View.GONE);
            }
            if(text4[position]==R.drawable.ic_star){
                iv2.setVisibility(View.GONE);
            }
            if(text5[position].matches("")) {
                tv3.setVisibility(View.GONE);
            }
            if(text6[position]==R.drawable.ic_star){
                iv3.setVisibility(View.GONE);
            }
            if(text7[position].matches("")){
                tv4.setVisibility(View.GONE);
            }
            if(text8[position]==R.drawable.ic_star){
                iv4.setVisibility(View.GONE);
            }
            if(text9[position].matches("")){
                tv5.setVisibility(View.GONE);
            }
            if(text10[position]==R.drawable.ic_star){
                iv5.setVisibility(View.GONE);
            }
            if(text11[position].matches("")){
                tv6.setVisibility(View.GONE);
            }
            if(text12[position]==R.drawable.ic_star){
                iv6.setVisibility(View.GONE);
            }
            if(text13[position].matches("")){
                tv7.setVisibility(View.GONE);
            }
            if(text14[position]==R.drawable.ic_star){
                iv7.setVisibility(View.GONE);
            }




            return v;
        }

    }

    class ListViewAdapterTwo extends BaseAdapter{


        String text1[]={"Body Feint Right","Body Feint Left","Stepover Right","Stepover Left","Reverse Stepover Right","Reverse Stepover Left","Ball Roll Left","Ball Roll Right","Drag Back (while standing)"};

        int text2[]={R.drawable.image_rs,R.drawable.image_rs,R.drawable.image_rs_rightdown,R.drawable.image_rs_leftdown,R.drawable.image_rs_upleft,R.drawable.image_rs_upright,R.drawable.image_rs,R.drawable.image_rs,R.drawable.image_r1};

        String text3[]={"Flick","Flick","","","","","Hold","Hold","+"};

        int text4[]={R.drawable.image_right,R.drawable.image_left,R.drawable.ic_star,R.drawable.ic_star,R.drawable.ic_star,R.drawable.ic_star,R.drawable.image_left,R.drawable.image_right,R.drawable.image_ls};

        String text5[]={"","","","","","","","","Flick"};

        int text6[]={R.drawable.ic_star,R.drawable.ic_star,R.drawable.ic_star,R.drawable.ic_star,R.drawable.ic_star,R.drawable.ic_star,R.drawable.ic_star,R.drawable.ic_star,R.drawable.image_down,};

        String text7[]={"","","","","","","","",""};

        int text8[]={R.drawable.ic_star,R.drawable.ic_star,R.drawable.ic_star,R.drawable.ic_star,R.drawable.ic_star,R.drawable.ic_star,R.drawable.ic_star,R.drawable.ic_star,R.drawable.ic_star,};

        String text9[]={"","","","","","","","",""};

        int text10[]={R.drawable.ic_star,R.drawable.ic_star,R.drawable.ic_star,R.drawable.ic_star,R.drawable.ic_star,R.drawable.ic_star,R.drawable.ic_star,R.drawable.ic_star,R.drawable.ic_star,};

        String text11[]={"","","","","","","","",""};

        int text12[]={R.drawable.ic_star,R.drawable.ic_star,R.drawable.ic_star,R.drawable.ic_star,R.drawable.ic_star,R.drawable.ic_star,R.drawable.ic_star,R.drawable.ic_star,R.drawable.ic_star,};

        String text13[]={"","","","","","","","",""};

        int text14[]={R.drawable.ic_star,R.drawable.ic_star,R.drawable.ic_star,R.drawable.ic_star,R.drawable.ic_star,R.drawable.ic_star,R.drawable.ic_star,R.drawable.ic_star,R.drawable.ic_star,};





        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return text1.length;
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return text1[position];
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub



            LayoutInflater infla=getActivity().getLayoutInflater();


            View v = infla.inflate(R.layout.list_view_layout, null);




            TextView tv1 = (TextView) v.findViewById(R.id.textView1);
            ImageView iv1 = (ImageView) v.findViewById(R.id.imageView1);
            TextView tv2 = (TextView) v.findViewById(R.id.textView2);
            ImageView iv2 = (ImageView) v.findViewById(R.id.imageView2);
            TextView tv3 = (TextView) v.findViewById(R.id.textView3);
            ImageView iv3 = (ImageView) v.findViewById(R.id.imageView3);
            TextView tv4 = (TextView) v.findViewById(R.id.textView4);
            ImageView iv4 = (ImageView) v.findViewById(R.id.imageView4);
            TextView tv5 = (TextView) v.findViewById(R.id.textView5);
            ImageView iv5 = (ImageView) v.findViewById(R.id.imageView5);
            TextView tv6 = (TextView) v.findViewById(R.id.textView6);
            ImageView iv6 = (ImageView) v.findViewById(R.id.imageView6);
            TextView tv7 = (TextView) v.findViewById(R.id.textView7);
            ImageView iv7 = (ImageView) v.findViewById(R.id.imageView7);





            tv1.setText(text1[position]);
            iv1.setImageResource(text2[position]);
            tv2.setText(text3[position]);
            iv2.setImageResource(text4[position]);
            tv3.setText(text5[position]);
            iv3.setImageResource(text6[position]);
            tv4.setText(text7[position]);
            iv4.setImageResource(text8[position]);
            tv5.setText(text9[position]);
            iv5.setImageResource(text10[position]);
            tv6.setText(text11[position]);
            iv6.setImageResource(text12[position]);
            tv7.setText(text13[position]);
            iv7.setImageResource(text14[position]);




            if(text2[position]==R.drawable.ic_star){
                iv1.setVisibility(View.GONE);
            }
            if(text3[position].matches("")) {
                tv2.setVisibility(View.GONE);
            }
            if(text4[position]==R.drawable.ic_star){
                iv2.setVisibility(View.GONE);
            }
            if(text5[position].matches("")) {
                tv3.setVisibility(View.GONE);
            }
            if(text6[position]==R.drawable.ic_star){
                iv3.setVisibility(View.GONE);
            }
            if(text7[position].matches("")){
                tv4.setVisibility(View.GONE);
            }
            if(text8[position]==R.drawable.ic_star){
                iv4.setVisibility(View.GONE);
            }
            if(text9[position].matches("")){
                tv5.setVisibility(View.GONE);
            }
            if(text10[position]==R.drawable.ic_star){
                iv5.setVisibility(View.GONE);
            }
            if(text11[position].matches("")){
                tv6.setVisibility(View.GONE);
            }
            if(text12[position]==R.drawable.ic_star){
                iv6.setVisibility(View.GONE);
            }
            if(text13[position].matches("")){
                tv7.setVisibility(View.GONE);
            }
            if(text14[position]==R.drawable.ic_star){
                iv7.setVisibility(View.GONE);
            }


            return v;



        }


    }

}
}

2 个答案:

答案 0 :(得分:0)

创建一个扩展FragmentActivity的活动,在FragmentActivity中,您可以粘贴导航抽屉代码和可重用组件。

答案 1 :(得分:0)

您应该从v4支持扩展Fragment。并在onCreateView方法中初始化您的视图。

import android.support.v4.app.Fragment;

public class SkillsPsFragment extends Fragment {

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


     View root = inflater.inflate(R.layout.activity_skills_ps, container, false);

     Toolbar toolbar = (Toolbar) root.findViewById(R.id.toolbar);
     //repeat this for all views

     return root:
}

要使用片段,您可以使用xml布局进行基本活动,如此

<FrameLayout
        android:id="@+id/maincontainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

然后在您的Activities onCreate方法结束时使用它来显示片段

getSupportFragmentManager().beginTransaction()
                .replace(R.id.maincontainer, new SkillsPsFragment(), "TAG")
                .commit();