Java 将现有活动添加到选项卡activity Android

Java 将现有活动添加到选项卡activity Android,java,android,xml,Java,Android,Xml,嗨,伙计们,我正在为现有的android应用程序添加一个选项卡活动!特别是,我应该将HomeActivity添加到TabActivity中,我该怎么做?我在网上读到,你应该在HomeActivity片段中实现,然后将其添加到TabActivity!但是如果我在HomeActivity中实现Fragment,我会有一系列错误 选项卡活动 package xx.myapp; import android.support.design.widget.TabLayout; import android

嗨,伙计们,我正在为现有的android应用程序添加一个选项卡活动!特别是,我应该将HomeActivity添加到TabActivity中,我该怎么做?我在网上读到,你应该在HomeActivity片段中实现,然后将其添加到TabActivity!但是如果我在HomeActivity中实现Fragment,我会有一系列错误

选项卡活动

package xx.myapp;

import android.support.design.widget.TabLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import android.widget.TextView;

public class TabActivity extends AppCompatActivity {

    /**
     * The {@link android.support.v4.view.PagerAdapter} that will provide
     * fragments for each of the sections. We use a
     * {@link FragmentPagerAdapter} derivative, which will keep every
     * loaded fragment in memory. If this becomes too memory intensive, it
     * may be best to switch to a
     * {@link android.support.v4.app.FragmentStatePagerAdapter}.
     */
    private SectionsPagerAdapter mSectionsPagerAdapter;

    /**
     * The {@link ViewPager} that will host the section contents.
     */
    private ViewPager mViewPager;

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

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        // Create the adapter that will return a fragment for each of the three
        // primary sections of the activity.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);

        mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });



    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_tab, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        private static final String ARG_SECTION_NUMBER = "section_number";

        public PlaceholderFragment() {
        }

        /**
         * Returns a new instance of this fragment for the given section
         * number.
         */
        public static PlaceholderFragment newInstance(int sectionNumber) {
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_tab, container, false);
            TextView textView = (TextView) rootView.findViewById(R.id.section_label);
            textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
            return rootView;
        }
    }

    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
     * one of the sections/tabs/pages.
     */
    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            // getItem is called to instantiate the fragment for the given page.
            // Return a PlaceholderFragment (defined as a static inner class below).
            return PlaceholderFragment.newInstance(position + 1);
        }

        @Override
        public int getCount() {
            // Show 3 total pages.
            return 3;
        }
    }
}
家庭活动:

package xx.myapp;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;

import xx.*;

public class HomeActivity extends AppCompatActivity  {

    int[] images = {R.drawable.accettato, R.drawable.fatturato, R.drawable.incorso, R.drawable.rifiutato};
    ArrayList CantieriRicerca = new ArrayList();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        setTitle("Home");

        /* Deserializzo  l'oggetto  */
        Intent i = getIntent();
        User u = (User) i.getSerializableExtra("User");
        Cantiere c = new Cantiere();
        CantieriRicerca = c.RicercaCantiere("a", u);

        //Listview che visulizza in cantieri con il customadapter
        ListView cantieriListView = (ListView) findViewById(R.id.ListViewCantieri);
        CustomAdapter cm = new CustomAdapter();
        cantieriListView.setAdapter(cm);
        try {
            FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
            fab.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                            .setAction("Action", null).show();
                }
            });


        } catch (Exception ex) {
            System.out.println("\n Errore HomeActivity.java : " + ex);
        }
    }

    class CustomAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return CantieriRicerca.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            convertView = getLayoutInflater().inflate(R.layout.row, null);
            ImageView imageView = (ImageView) convertView.findViewById(R.id.imageview_stato);
            TextView textview_nomecantiere = (TextView) convertView.findViewById(R.id.textView_NomeCantiere);
            TextView textview_cliente = (TextView) convertView.findViewById(R.id.textView_Cliente);

            Cantiere c = (Cantiere) CantieriRicerca.get(position);
            String statoCantiere = c.GetStatoCantiere();

            if (statoCantiere.equals("Chiuso")==true) {
                imageView.setImageResource(images[0]);
            } else if (statoCantiere.equals("InCorso")==true) {
                imageView.setImageResource(images[2]);
            } else if (c.GetStatoFatturazione() > 0) {
                imageView.setImageResource(images[1]);
            }
            else{
                imageView.setImageResource(images[3]);
            }

            textview_cliente.setText(c.c.GetRagioneSociale());
            textview_nomecantiere.setText(c.GetNomeCantiere());
            return convertView;
        }
    }

}

你应该考虑将你的家庭活动转换为<代码>片段<代码>,并将其放入你的代码> TabActudio中。片段和它们的API一样强大,所以应该不会有任何问题。

嗨,Riki。您想将活动放入选项卡activity中吗?@ArtyomDorosh是的,这是不可能的,您应该使用
Fragment
s来代替它。@ArtyomDorosh所以我必须将活动转换为fragments。将活动转换为片段不需要很多工作,所以这应该是非常容易的。另外,我刚刚为你的问题写了一个答案,所以如果你能接受,我将不胜感激。