Autore Topic: come inserire un activity in un fragment?  (Letto 250 volte)

Offline markella92

  • Nuovo arrivato
  • *
  • Post: 37
  • Respect: 0
    • Mostra profilo
come inserire un activity in un fragment?
« il: 27 Aprile 2016, 17:05:42 CEST »
0
Salve ragazzi, ho dei tabs scrollatili orizzontalmente, ad esempio se clicco sul primo deve uscire la listview con le pizze.....
come posso fare?
Vi mostro il codice.

Grazie in anticipo


Codice: [Seleziona]
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ListView;

import java.util.ArrayList;

public class Main extends ActionBarActivity implements
        android.widget.CompoundButton.OnCheckedChangeListener {

    ListView lv;
    ArrayList<Planet> planetList;
    PlanetAdapter plAdapter;

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

        lv = (ListView) findViewById(R.id.listview);
        displayPlanetList();


    }

    private void displayPlanetList() {

        planetList = new ArrayList<Planet>();
        planetList.add(new Planet("Margherita", 6, "€"));
        planetList.add(new Planet("Diavola", 7,"€"));
        planetList.add(new Planet("Bufalina", 5,"€"));
        planetList.add(new Planet("Marinara", 5,"€"));
        planetList.add(new Planet("Viennese", 4,"€"));

        plAdapter = new PlanetAdapter(planetList, this);
        lv.setAdapter(plAdapter);
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

        int pos = lv.getPositionForView(buttonView);
        if (pos != ListView.INVALID_POSITION) {
            Planet p = planetList.get(pos);
            p.setSelected(isChecked);


            /*Toast.makeText(
                    this,
                    "Clicked on Planet: " + p.getName() + ". State: is "
                            + isChecked, Toast.LENGTH_SHORT).show();*/
        }

    }
    public void showResult(View v) {
        String result = "Selected Product are :";
        int totalAmount=0;

        for (Planet p : plAdapter.getBox()) {
            if (p.selected){
                result += "\n" + p.name+" "+p.distance+"€"+"q.tà :"+p.getQuantità();
                //if (p.quantità.equals("") && p.quantità.equals(null) ){
                System.out.println("leggo questo record:"+p.name + " " + p.distance + " " + p.quantità );

                System.out.println("leggo questo p.getquatità :"+p.quantità );
                //}
                //else{
                    System.out.println("leggo questo in p.quantità: "+p.getQuantità());
                    int quantitaInt= Integer.parseInt(p.getQuantità() );
                    totalAmount+=p.distance * quantitaInt;
                //}
            }
        }
//      Toast.makeText(this, result+"\n"+"Total Amount:="+totalAmount, Toast.LENGTH_LONG).show();
        //Integer.toString(totalAmount);
        Intent i = new Intent(Main.this, SecondActivity.class);
        i.putExtra("NomeDati1", result);
        i.putExtra("NomeDati2", String.valueOf(totalAmount));
        startActivity(i);

    }
}

Questo:

Codice: [Seleziona]
public class ScrollableTabsActivity extends AppCompatActivity {

    private Toolbar toolbar;
    private TabLayout tabLayout;
    private ViewPager viewPager;

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

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        viewPager = (ViewPager) findViewById(R.id.viewpager);
        setupViewPager(viewPager);

        tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);
    }

    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFrag(new OneFragment(), "PIZZE");
        adapter.addFrag(new TwoFragment(), "TWO");
        adapter.addFrag(new ThreeFragment(), "THREE");
        adapter.addFrag(new FourFragment(), "FOUR");
        adapter.addFrag(new FiveFragment(), "FIVE");
        adapter.addFrag(new SixFragment(), "SIX");
        adapter.addFrag(new SevenFragment(), "SEVEN");
        adapter.addFrag(new EightFragment(), "EIGHT");
        adapter.addFrag(new NineFragment(), "NINE");
        adapter.addFrag(new TenFragment(), "TEN");
        viewPager.setAdapter(adapter);
    }


E questo:

Codice: [Seleziona]
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import info.androidhive.materialtabs.R;


public class OneFragment extends Fragment {


    public OneFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.main, container, false);
    }

}