package com.example.zzh.androidbestpractice;import android.app.Fragment;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.design.widget.TabLayout;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;import com.bumptech.glide.Glide;
import com.example.zzh.androidbestpractice.gson.Weather;import com.example.zzh.androidbestpractice.util.HttpUtil;import org.litepal.crud.DataSupport;import java.io.IOException;
import java.util.ArrayList;
import java.util.List;import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;public class TabActivity extends AppCompatActivity {    private String TAG = "TabActivity";    private TabLayout tabLayout;
    private ViewPager viewPager;    private Button button;
    private ImageView bingPicImage;    private List<String> tabIndicater;
    private List<android.support.v4.app.Fragment> tabFragments;
    private FragmentAdapter fragmentAdapter;    public DrawerLayout drawerLayout;
    public SwipeRefreshLayout swipeRefreshLayout;    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab);        button = (Button) findViewById(R.id.nav_button);        tabLayout = (TabLayout)findViewById(R.id.tab);
        viewPager = (ViewPager) findViewById(R.id.viewpager);        tabFragments = new ArrayList<>();
        tabIndicater = new ArrayList<>();
        tabLayout.setTabMode(TabLayout.MODE_FIXED);//        initTitle(); //添加标题
//        initfragment(); //  添加fragment
        fragmentAdapter = new FragmentAdapter(getSupportFragmentManager(), tabFragments, tabIndicater );
        viewPager.setAdapter(fragmentAdapter);
        tabLayout.setupWithViewPager(viewPager);
//        tabLayout.setTabsFromPagerAdapter(fragmentAdapter);        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        swipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.refresh_swipe);
        swipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                requestWeather();
            }
        });//        button.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                drawerLayout.openDrawer(GravityCompat.START);
//            }
//        });        requestWeather();    }    private void initTitle(){
        tabIndicater = new ArrayList<>();
        tabIndicater.add("asdasd");
        tabIndicater.add("asdasda");
        tabLayout.setTabMode(TabLayout.MODE_FIXED);
        tabLayout.addTab(tabLayout.newTab().setText(tabIndicater.get(0)));
        tabLayout.addTab(tabLayout.newTab().setText(tabIndicater.get(1)));
    }    private void initfragment(){
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        String weather = sharedPreferences.getString("weather", null);
        tabFragments = new ArrayList<>();
        Log.d("", "initfragment: "+weather);
        tabFragments.add(WeatherFragment.newInstance(weather));
        tabFragments.add(WeatherFragment.newInstance(weather));
    }//    public void requestWeather(){
         SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(TabActivity.this);
        List<String> weatherids = Trans.str_to_list(preferences.getString("weatherids",null));
        final List<String> weathertexts = new ArrayList<>();
        for(String s : weatherids){
            Log.d("sssssssssssssssssss", "requestWeather: "+s);
        }
        if(weatherids.size()!=0 && weatherids != null){
            tabIndicater.clear();
            tabFragments.clear();
            for(String weatherid: weatherids){
                String weathertext = preferences.getString(weatherid, null);
                if(weathertext != null && weathertext != ""){
                    weathertexts.add(weathertext);
                }
                else {
                    String weatherUrl = "http://guolin.tech/api/weather?cityid="+weatherid + "&key=asasasasasasasasasasasasas";
                    Log.d("xxxxxxxxxxxxxxxxxxxxx", "requestWeather: "+weatherUrl);
                    HttpUtil.sendOkHttpRequest(weatherUrl, new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {
                            e.printStackTrace();
                            Toast.makeText(TabActivity.this, "获取天气信息失败",Toast.LENGTH_SHORT).show();
                        }                        @Override
                        public void onResponse(Call call, Response response) throws IOException {
                            String responseText = response.body().string();
                            Log.d("TabActivity", "onResponse: "+responseText);
                            Weather weather = HttpUtil.handleWeatherResponse(responseText);
                            if(weather!=null&&"ok".equals(weather.status)){
                                SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(TabActivity.this).edit();
                                editor.putString(weather.basic.weatherId, responseText);
                                editor.apply();
                                weathertexts.add(responseText);
                            }
                        }
                    });
                }
            }
        }else {
            Toast.makeText(this, "获取天气失败", Toast.LENGTH_SHORT).show();
        }        for(String weathertext: weathertexts){
            Log.d("TabActivity", "requestWeather: "+weathertext);
            tabIndicater.add("");
            tabLayout.addTab(tabLayout.newTab().setText("aaaaaaa"));
            tabFragments.add(WeatherFragment.newInstance(weathertext));
        }
    }
}
<?xml version="1.0" encoding="utf-8"?>
<!--<LinearLayout-->
    <!--android:layout_height="match_parent"-->
    <!--android:layout_width="match_parent"-->
    <!--android:orientation="vertical"-->
    <!--android:background="@drawable/bg"-->
    <!--android:id="@+id/layout"-->
    <!--xmlns:android="http://schemas.android.com/apk/res/android">-->    <!--<ImageView-->
        <!--android:layout_width="match_parent"-->
        <!--android:layout_height="match_parent"-->
        <!--android:id="@+id/bing_pic_img"-->
        <!--android:scaleType="centerCrop"/>-->
<android.support.v4.widget.DrawerLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:background="@drawable/bg"
    android:id="@+id/drawer_layout"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">                <android.support.v4.widget.SwipeRefreshLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:id="@+id/refresh_swipe">                        <!--<include layout="@layout/title"/>-->                        <android.support.design.widget.TabLayout
                            android:id="@+id/tab"
                            android:layout_width="match_parent"
                            android:layout_height= "0dp">                        </android.support.design.widget.TabLayout>                        <android.support.v4.view.ViewPager
                            android:id="@+id/viewpager"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent">                        </android.support.v4.view.ViewPager>
                </android.support.v4.widget.SwipeRefreshLayout>
        </LinearLayout>        <fragment
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/choose_area_fragment"
    android:name="com.example.zzh.androidbestpractice.ChooseAreaFragment"
    android:layout_gravity = "start"
    /></android.support.v4.widget.DrawerLayout>求问各位前辈,为什么在执行了tabFragments.add(WeatherFragment.newInstance(weathertext)); 后viewpager 并没有出现。这行代码注释掉后和现在没有什么差别

解决方案 »

  1.   

    你在后面加两行代码 viewPager.setAdapter(fragmentAdapter);
            tabLayout.setupWithViewPager(viewPager);
      

  2.   

    我指的是在tabFragments.add(WeatherFragment.newInstance(weathertext)); 这个后面加
      

  3.   

    我指的是在tabFragments.add(WeatherFragment.newInstance(weathertext)); 这个后面加
    没有起作用,标签上的字消失了,但是viewpager还是没有显示
      

  4.   

    不用了,我发现是把 SwipeRefreshLayout  嵌套在 LinearLayout 里面产生的问题, 把它们调换一下位置就可以了,但我不知道为什么
      

  5.   

    Android 之 TabView 选项视图http://www.verejava.com/?id=17467921621630