florent37 / MaterialViewPager

A Material Design ViewPager easy to use library
https://www.fiches-plateau-moto.fr
Apache License 2.0
8.13k stars 1.48k forks source link

Get the tab name. #261

Open Haswell19 opened 8 years ago

Haswell19 commented 8 years ago

I have a principal activity : ` public class VilleActivity extends ActionBarActivity {

MaterialViewPager materialViewPager;
View headerLogo;
int intLenght;
private String jsonResult;
//URL to get JSON Array
private static String url = "http://jstnvffry.ch/capdroid.php";

String strIdVille ;
String strDescription;
Toolbar toolbar;
private RecyclerView recyclerView;

@Override
protected void onCreate(Bundle savedInstanceState) {

    getSupportActionBar().hide();
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ville);

    //récupere les infos sur quelle ville on a cliqué
    Intent i = getIntent();
    strIdVille = i.getStringExtra("id");

    TextView txt = (TextView) findViewById(R.id.logo_white);
    txt.setText(strIdVille);

    //3 onglets
    final int tabCount = 3;

    //les vues définies dans @layout/header_logo
    headerLogo = findViewById(R.id.logo_white);

    //le MaterialViewPager
    this.materialViewPager = (MaterialViewPager) findViewById(R.id.materialViewPager);

        this.materialViewPager.getViewPager().setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                //je créé pour chaque onglet un RecyclerViewFragment
                return RecyclerVille.newInstance();
            }

            public void setOldItemPosition(int oldItemPosition) {
                this.oldItemPosition = oldItemPosition;
            }

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

            //le titre à afficher pour chaque page
            @Override
            public CharSequence getPageTitle(int position) {
                switch (position) {
                    case 0:
                        return "Description";
                    case 1:
                        return "Photo";
                    case 2:
                        return "Point d'intêret";
                    default:
                        return "Page " + position;
                }
            }

            int oldItemPosition = -1;

            @Override
            public void setPrimaryItem(ViewGroup container, int position, Object object) {
                super.setPrimaryItem(container, position, object);

                //seulement si la page est différente
                if (oldItemPosition != position) {
                    oldItemPosition = position;

                    //définir la nouvelle couleur et les nouvelles images
                    String imageUrl = null;
                    int color = Color.BLACK;
                    Drawable newDrawable = null;

                    switch (position) {
                        case 0:
                            imageUrl = "http://grandtour.myswitzerland.com/img/poi/25/lausannestadt/lausannestadt_static.jpg";
                            color = getResources().getColor(R.color.colorPrimary);
                            break;
                        case 1:
                            imageUrl = "http://www.larousse.fr/encyclopedie/data/images/1311904-Balle_de_tennis_et_filet.jpg";
                            color = getResources().getColor(R.color.colorPrimaryDark);
                            break;
                        case 2:
                            imageUrl = "http://soocurious.com/fr/wp-content/uploads/2014/03/8-facettes-de-notre-cerveau-qui-ont-evolue-avec-la-technologie8.jpg";
                            color = getResources().getColor(R.color.colorAccent);
                            break;

                    }

                    //puis modifier les images/couleurs
                    int fadeDuration = 100;
                    materialViewPager.setColor(color, fadeDuration);

                }
            }
        });

        //permet au viewPager de garder 4 pages en mémoire (à ne pas utiliser sur plus de 4 pages !)
        this.materialViewPager.getViewPager().setOffscreenPageLimit(tabCount);
        //relie les tabs au viewpager
        this.materialViewPager.getPagerTitleStrip().setViewPager(this.materialViewPager.getViewPager());
    }
}`

After I have another activity :

`public class RecyclerVille extends Fragment {

private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
MaterialViewPager materialViewPager;

int intLenght;
private String jsonResult;
//URL to get JSON Array
private static String url = "http://jstnvffry.ch/capdroid.php";
String strIdVille ;

public static RecyclerVille newInstance() {
    return new RecyclerVille();
    //le MaterialViewPager

}

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_ville, container, false);
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerVille);

    //permet un affichage sous forme liste verticale
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
    mRecyclerView.setLayoutManager(layoutManager);
    mRecyclerView.setHasFixedSize(true);

    TextView txt = (TextView) getActivity().findViewById(R.id.logo_white);
    strIdVille = txt.getText().toString();

    accessWebService();

    ViewPager vP ;
      //.getPagerTitleStrip().getTabBackground();

    MaterialViewPagerHelper.registerRecyclerView(getActivity(), mRecyclerView, null);

}

List<VilleObject> mContentItems = new ArrayList<>();

private class JsonReadTask extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... params) {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(params[0]);
        try {
            HttpResponse response = httpclient.execute(httppost);
            jsonResult = inputStreamToString(
                    response.getEntity().getContent()).toString();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    private StringBuilder inputStreamToString(InputStream is) {
        String rLine = "";
        StringBuilder answer = new StringBuilder();
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));

        try {
            while ((rLine = rd.readLine()) != null) {
                answer.append(rLine);
            }
        }

        catch (IOException e) {
            // e.printStackTrace();
            Toast.makeText(getActivity().getApplicationContext(),
                    "Error..." + e.toString(), Toast.LENGTH_LONG).show();
        }
        return answer;
    }

    @Override
    protected void onPostExecute(String result) {
        ListDrwaer();
    }
}// end async task

//methode de connexion à la bdd
public void accessWebService() {
    JsonReadTask task = new JsonReadTask();
    // passes values for the urls string array
    task.execute(new String[]{url});
}

// build hash set for list view
public void ListDrwaer() {
    materialViewPager = (MaterialViewPager) getActivity().findViewById(R.id.materialViewPager);
    try {

            JSONObject jsonResponse = new JSONObject(jsonResult);
            JSONArray jsonMainNode = jsonResponse.optJSONArray("t_ville");

            intLenght = jsonMainNode.length();
            //ajout des éléments au tableau
            for (int i = 0; i < intLenght; i++) {

                JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
                String ville = jsonChildNode.optString("NomVille");

                if (ville.equals(strIdVille)) {
                    mContentItems.add(new VilleObject(jsonChildNode.optString("DescriptionVille")));

                }

            }
           /*if(tab equals Photo){ JSONObject jsonResponse = new JSONObject(jsonResult);
            JSONArray jsonMainNode = jsonResponse.optJSONArray("t_image");

            intLenght = jsonMainNode.length();
            //ajout des éléments au tableau
            for (int i = 0; i < intLenght; i++) {

                JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
                String ville = jsonChildNode.optString("NomVille");

                if (ville.equals(strIdVille)) {
                    mContentItems.add(new VilleObject(jsonChildNode.optString("LienImg")));

                }

            }
            }*/

            mContentItems.add(new VilleObject("Hello"));

        //créer la carte pour la ville a afficher
        mAdapter = new RecyclerViewMaterialAdapter(new VilleAdapter(mContentItems));
        mRecyclerView.setAdapter(mAdapter);

    } catch (JSONException e) {
        Toast.makeText(getActivity().getApplicationContext(), "Error" + e.toString(),
                Toast.LENGTH_SHORT).show();
    }
}

} `

jayhack7 commented 8 years ago

Are you referring to getting the name of these tabs? public CharSequence getPageTitle(int position) { switch (position) { case 0: return "Description"; case 1: return "Photo"; case 2: return "Point d'intêret"; default: return "Page " + position; }

Haswell19 commented 8 years ago

I don't know how to get this on my RecyclerVille