TabLayout 使用过程中出现的一些问题

AlexLesser 2018-07-31 08:33:23
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 并没有出现。这行代码注释掉后和现在没有什么差别
...全文
362 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
键盘舞者113 2018-08-01
  • 打赏
  • 举报
回复
那你把项目上传,链接给我,我改一下
AlexLesser 2018-08-01
  • 打赏
  • 举报
回复
引用 3 楼 z979451341 的回复:
[quote=引用 2 楼 AlexLesser 的回复:]
[quote=引用 1 楼 z979451341 的回复:]
你在后面加两行代码

 viewPager.setAdapter(fragmentAdapter);
        tabLayout.setupWithViewPager(viewPager);
这个有啊,在69行[/quote]
我指的是在tabFragments.add(WeatherFragment.newInstance(weathertext)); 这个后面加[/quote]
没有起作用,标签上的字消失了,但是viewpager还是没有显示
键盘舞者113 2018-08-01
  • 打赏
  • 举报
回复
引用 2 楼 AlexLesser 的回复:
[quote=引用 1 楼 z979451341 的回复:]
你在后面加两行代码

 viewPager.setAdapter(fragmentAdapter);
        tabLayout.setupWithViewPager(viewPager);
这个有啊,在69行[/quote]
我指的是在tabFragments.add(WeatherFragment.newInstance(weathertext)); 这个后面加
AlexLesser 2018-08-01
  • 打赏
  • 举报
回复
引用 1 楼 z979451341 的回复:
你在后面加两行代码

 viewPager.setAdapter(fragmentAdapter);
        tabLayout.setupWithViewPager(viewPager);
这个有啊,在69行
YXTS122 2018-08-01
  • 打赏
  • 举报
回复
嵌套的视图没搞清楚。。。。。
verejava 2018-08-01
  • 打赏
  • 举报
回复
Android 之 TabView 选项视图

http://www.verejava.com/?id=17467921621630
键盘舞者113 2018-08-01
  • 打赏
  • 举报
回复
你在后面加两行代码

 viewPager.setAdapter(fragmentAdapter);
        tabLayout.setupWithViewPager(viewPager);
AlexLesser 2018-08-01
  • 打赏
  • 举报
回复
引用 5 楼 z979451341 的回复:
那你把项目上传,链接给我,我改一下

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

80,492

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧