package com.ecom.park_jy;

import android.content.Intent;
import android.os.Environment;
import android.support.design.widget.TabLayout;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import static com.ecom.park_jy.Start.RoadNum;

public class Road_list extends AppCompatActivity {  //路名List
    public static String[] Title;
    String findin, findout;
    public static TabLayout tab;
    ViewPager vp;
    TabLayout.Tab list;
    public static List listRoad = new ArrayList();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_road_list);
        File road_select = new File(Environment.getExternalStorageDirectory() + "/Park_JY/JiaYiCityRoad.txt");
        BufferedReader reader = null;
        listRoad.clear();

        try {
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(road_select)));
            while ((findin = reader.readLine()) != null) {
                findout = findin;
                Title = findout.split("\n");
                for (int i = 0; i < Title.length; i++) {
                    listRoad.add(Title[i]);
                }
            }
            reader.close();
        } catch (java.io.IOException e) {
            e.printStackTrace();
        }

        tab = (TabLayout) findViewById(R.id.tablayout);
        vp = (ViewPager) findViewById(R.id.viewpager);

        Skin_Road_list_adapter sRoad = new Skin_Road_list_adapter(getSupportFragmentManager());
        vp.setAdapter(sRoad);
        tab.setupWithViewPager(vp);
        tab.getTabAt(RoadNum).select();
        for (int j = 0; j < listRoad.size(); j++) {
            list = tab.getTabAt(j);
            list.setCustomView(getView(j));
            LinearLayout ln = (LinearLayout) tab.getChildAt(0);
            ln.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
            ln.setDividerPadding(20);  //設置LinerLayout的間距
        }
    }

    private View getView(int index) {
        View view = getLayoutInflater().inflate(R.layout.skin_road_list_tab_text, null);
        TextView t12 = (TextView) view.findViewById(R.id.textView12);
        t12.setText(listRoad.get(index).toString().substring(0, listRoad.get(index).toString().length() - 5));
        return view;

    }
}
