50% Off/-

50% Off/-

Php

50% Off/-

50% Off/-

Web

50% Off/-

50% Off/-

Fragment with ViewPager Example

21-01-2019
import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.forex.stockexchange4.R;
import com.forex.stockexchange4.models.CalculateSummaryObject;

import java.util.ArrayList;
import java.util.List;

public class CalculateFragment extends Fragment {
    private OnInvestingListFragmentInteractionListener mListener;
    public CalculateFragment() {
        // Required empty public constructor
    }
    
    /**
    * Use this factory method to create a new instance of
    * this fragment
    *
    * @return A new instance of fragment StepPagerFragment.
    */
    public static CalculateFragment newInstance() {
        return new CalculateFragment();
    }
    
    @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
        View view = inflater.inflate(R.layout.fragment_main, container, false);
        
        setupViewPager(view);
        return view;
    }
    
    private void setupViewPager(View view) {
        
        ViewPager viewPager = view.findViewById(R.id.viewPager);
        ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getChildFragmentManager());
        viewPagerAdapter.addFragment(HesapFragment.newInstance(),
        getResources().getString(R.string.calculator));
        viewPagerAdapter.addFragment(HesapSummaryFragment.newInstance(),
        getResources().getString(R.string.summary));
        
        viewPager.setAdapter(viewPagerAdapter);
        TabLayout tabLayout = view.findViewById(R.id.tabs);
        tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        tabLayout.setupWithViewPager(viewPager);
    }
    
    private class ViewPagerAdapter extends FragmentStatePagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();
        ViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }
        
        @Override
        public int getCount() {
            return mFragmentList.size();
        }
        public void addFragment(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }
        @Override
        public Fragment getItem(int position) {
            mListener.onFragmentInteractionInitTitle(getResources().getString(R.string.app_name));
            return mFragmentList.get(position);
        }
        
        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }
    }
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnInvestingListFragmentInteractionListener) {
            mListener = (OnInvestingListFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
            + " must implement OnInvestingListFragmentInteractionListener");
        }
        mListener.onFragmentInteractionInitTitle(getResources().getString(R.string.app_name));
    }
}

© 2019 Tüm Hakları Saklıdır. Codesenior.COM