在片段

时间:2016-04-11 12:09:15

标签: android android-fragments

PageAdapter

class SampleFragmentPagerAdapter extends FragmentPagerAdapter {

private int PAGE_COUNT = 3;
private String tabTitles[];

public SampleFragmentPagerAdapter(FragmentManager fm, String[] categorie) {
    super(fm);
    PAGE_COUNT = categorie.length;
    tabTitles = new String[categorie.length];

    for(int i = 0; i < categorie.length; i++){
        tabTitles[i] = categorie[i];
    }
}

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

@Override
public Fragment getItem(int position) {
    return Pagina.newInstance(position + 1);
}

@Override
public CharSequence getPageTitle(int position) {
    // Generate title based on item position
    return tabTitles[position];
}
}

片段活动:

public class Pagina extends Fragment {
private int mPage;
public static final String ARG_PAGE = "ARG_PAGE";

public static Pagina newInstance(int page) {
    Bundle args = new Bundle();
    args.putInt(ARG_PAGE, page);
    Pagina fragment = new Pagina();
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mPage = getArguments().getInt(ARG_PAGE);
}

// Inflate the fragment layout we defined above for this fragment
// Set the associated text for the title
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_pagina, container, false);
    //TextView tvTitle = (TextView) view.findViewById(R.id.tvTitle);
    //tvTitle.setText("Fragment #" + mPage);
    Log.e("STATO", "CI SIAMOOO");
    return view;
}

public void getSecondScrollView(String str){
    Log.e("ECCOLOO", "AVVIATO");
}

}

mainActivity:

public class PrincipaleActivity extends FragmentActivity{

public String[] categorie;

private String linguaApp = Locale.getDefault().getLanguage();
private Boolean linguaPresente = false;

private float scale;

private NetworkImageView imgProdotto;
private ViewPager pager;
private PagerSlidingTabStrip tabs;

private int screenWidth;
private int screenHeight;

private JSONObject dati;




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

    Intent intent = getIntent();

    try {
        dati = new JSONObject(intent.getStringExtra("DatiArticolo"));
        checkLingua();
    } catch (JSONException e) {
        e.printStackTrace();
        Log.e("ERRORE", "Errore durante il parsing del JSONObject o del checkLingua");
    }

    ((myMainScrollView)findViewById(R.id.scrollPrincipale)).setScrollingEnabled(false);

    creaCategorie();
    caricaImgProdotto();

    // Initialize the ViewPager and set an adapter
    pager = (ViewPager) findViewById(R.id.pager);
    pager.setAdapter(new SampleFragmentPagerAdapter(getSupportFragmentManager(),categorie));

    // Bind the tabs to the ViewPager
    tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
    tabs.setViewPager(pager);

    setDimensioniSchermo();
    inizializzaPagina();

}

private void creaCategorie(){
    categorie = new String[dati.optJSONArray("categorie").length()];

    for (int h = 0; h < dati.optJSONArray("categorie").length(); h++){
        try {
            JSONObject temp = (JSONObject) dati.optJSONArray("categorie").get(h);
            categorie[h] = temp.optJSONObject("nome").optString(linguaApp);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

private void caricaImgProdotto(){
    imgProdotto = (NetworkImageView) findViewById(R.id.imgProdotto);

    String url = "http://46.101.209.16/"+dati.optJSONObject("anagrafica").optJSONObject(linguaApp).optJSONObject("img").optString("path");
    Log.e("LINK IMG", url);

    ImageLoader imageLoader = MyImageLoader.getInstance(this.getApplicationContext()).getImageLoader();
    imageLoader.get(url, ImageLoader.getImageListener(imgProdotto, R.drawable.icona_flash, android.R.drawable .ic_dialog_alert));
    imgProdotto.setImageUrl(url, imageLoader);
}

private void checkLingua() throws JSONException {
    JSONArray valLingue = dati.optJSONObject("lingue").optJSONArray("lingueApp");


    for(int i = 0; i < valLingue.length(); i++){
        if(valLingue.get(i).equals(linguaApp)){
            linguaPresente = true;
        }
    }
    if (linguaPresente == false){
        linguaApp = dati.optJSONObject("lingue").optString("principale");
    }

    Log.e("Lingua Scelta ",linguaApp);
}

private void setDimensioniSchermo(){
    scale = getResources().getDisplayMetrics().density;

    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    screenWidth = size.x;
    screenHeight = size.y;
}

private void inizializzaPagina(){
    imgProdotto.getLayoutParams().height = (screenWidth/3)*2;
    tabs.getLayoutParams().height = (int)(scale * 30);
    pager.getLayoutParams().height = screenHeight - (int)(scale * 30);
}

}

如何从getSecondScrollView调用方法SampleFragmentPagerAdapter? 我正在使用这个:https://github.com/jpardogo/PagerSlidingTabStrip 我需要片段内的方法来获取一些视图 抱歉英语不好,我是意大利人:D

0 个答案:

没有答案