Java 在tablayout中切换选项卡时,应用程序滞后

Java 在tablayout中切换选项卡时,应用程序滞后,java,android,Java,Android,当我从一个标签切换到另一个标签时,我的应用程序非常滞后。总的来说,它是滞后的。我不知道该怎么办。这是我的密码。我怎样才能加快速度?我哪里出错了 主要活动 package com.example.apple.project2; import android.content.Context; import android.graphics.Color; import android.os.Bundle; import android.support.design.widget.Tab

当我从一个标签切换到另一个标签时,我的应用程序非常滞后。总的来说,它是滞后的。我不知道该怎么办。这是我的密码。我怎样才能加快速度?我哪里出错了

主要活动

    package com.example.apple.project2;
    import android.content.Context;
import android.graphics.Color;
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.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

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

public class MainActivity extends AppCompatActivity {
Context mContext;
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.setBackgroundResource(Color.TRANSPARENT);
    setSupportActionBar(toolbar);

    getSupportActionBar().setDisplayHomeAsUpEnabled(false);

    viewPager = (ViewPager) findViewById(R.id.viewpager);
    viewPager.setOffscreenPageLimit(3);

    setupViewPager(viewPager);

    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_main, menu);
    return true;

}


@Override
public boolean onOptionsItemSelected(MenuItem menu) {
    if(menu.getItemId() == R.id.change_city){
        showInputDialog();
    }
    return false;
}

private void showInputDialog(){

}

public void changeCity(String city){

    mContext = this;
    KeyValueDB.setCity(mContext, city);
    String s=city;



}

private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFragment(new ListContentFragment(), "Current");
    adapter.addFragment(new DetailsFragment(),"Current Details");
    adapter.addFragment(new CardContentFragment(), "10 Day Forecast");
    viewPager.setAdapter(adapter);
}


class ViewPagerAdapter extends FragmentPagerAdapter {

    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
}


}
}

DetailsFragment.java

   package com.example.apple.project2;

  import android.content.Context;
  import android.os.Bundle;
  import android.support.v4.app.Fragment;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.TextView;

  import butterknife.BindView;
  import butterknife.ButterKnife;


  public class ListContentFragment extends Fragment {
@BindView(R.id.city_field)
TextView cityText;
@BindView(R.id.temp_field)
TextView tempText;
@BindView(R.id.humity_field)
TextView humidText;

@BindView(R.id.speed_field)
TextView speedText;
Context mContext;
ViewGroup rootView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    rootView = (ViewGroup) inflater
            .inflate(R.layout.item_list, container, false);
    ButterKnife.bind(this, rootView);

    mContext = getContext();
    String t = KeyValueDB.getTemp(mContext);
    double cel = Double.parseDouble(t);
    tempText.setText(String.format("%.2f", cel)+"°C");
    mContext = getContext();
    String city = KeyValueDB.getCity(mContext);
    city=city.toUpperCase();
    mContext = getContext();
    String country=KeyValueDB.getCountry(mContext);
    country=country.toUpperCase();
    cityText.setText(city+" , "+country);
    mContext = getContext();
    String humid=KeyValueDB.getHumidity(mContext);
    mContext = getContext();
    String speed=KeyValueDB.getSpeed(mContext);
    humidText.setText(humid+"%");
    speedText.setText(speed+" km/hr");
    return rootView;
}
 public class DetailsFragment extends Fragment {

public final String AppID = "ebfcac32bda131ed5a160f2757938396";
public final String BASE_URL = "http://api.openweathermap.org";

@BindView(R.id.latitudeTextView)
TextView latitudeTextView;
@BindView(R.id.longitudeTextView)
TextView longitudeTextView;
@BindView(R.id.countryTextView)
TextView countryTextView;
@BindView(R.id.descriptionTextView)
TextView descriptionTextView;
@BindView(R.id.tempTextView)
TextView tempTextView;
@BindView(R.id.humidityTextView)
TextView humidityTextView;
@BindView(R.id.tempMinTextView)
TextView tempMinTextView;
@BindView(R.id.tempMaxTextView)
TextView tempMaxTextView;
@BindView(R.id.speedTextView)
TextView speedTextView;
@BindView(R.id.nameTextView)
TextView nameTextView;

private ProgressDialog mProgressDialog;
private ViewPager mCategoriesViewPager;
private TabLayout tabLayout;
private View mViewHome;
String CITY;
View view;
Context mContext;
ViewGroup rootView;
public DetailsFragment() {
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    rootView = (ViewGroup) inflater
            .inflate(R.layout.item_detail, container, false);

    mProgressDialog = new ProgressDialog(getActivity());
    mProgressDialog.setMessage(getString(R.string.fetching_weather_info));
    mProgressDialog.setCancelable(false);
    weatherUpdate();

    FloatingActionButton fab = (FloatingActionButton) rootView.findViewById(R.id.fab);
    fab.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.cityicon));

    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            builder.setTitle("Change city");
            final EditText input = new EditText(getActivity());
            input.setInputType(InputType.TYPE_CLASS_TEXT);
            builder.setView(input);
            builder.setPositiveButton("Go", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    changeCity(input.getText().toString());
                }
            });
            builder.show();

        }
    });




    return rootView;
}
public void weatherUpdate(){

    ButterKnife.bind(this, rootView);


    Context mContext;
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity().getBaseContext());

    mContext = getContext();

    String city = KeyValueDB.getCity(mContext);
    CITY=city;
    Retrofit retrofit = new Retrofit
            .Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    WeatherService weatherService = retrofit.create(WeatherService.class);

    Call<WeatherInfo> weatherInfoCall = weatherService.getWeatherInfo(CITY, AppID);

    weatherInfoCall.enqueue(new Callback<WeatherInfo>() {
        @Override
        public void onResponse(Call<WeatherInfo> call, Response<WeatherInfo> response) {
            showProgressDialog(false);
            onDisplayWeatherInfo(response.body());
            showProgressDialog(false);            }

        @Override
        public void onFailure(Call<WeatherInfo> call, Throwable t) {
            showProgressDialog(false);
            Toast.makeText(getActivity(), R.string.fetching_weather_info_failure, Toast.LENGTH_SHORT).show();
        }
    });
}
private void showProgressDialog(boolean show) {
    if (show) {
        if (!mProgressDialog.isShowing()) {
            mProgressDialog.show();
        }
    } else {
        if (mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
        }
    }
}


public void changeCity(String city){
    mContext = getActivity();
    KeyValueDB.setCity(mContext, city);
    CITY=city;
    weatherUpdate();
}
double f,c;
String humid,temp,speed,countryname,cityname;
private void onDisplayWeatherInfo(WeatherInfo weatherInfo) {
    if (weatherInfo != null) {

        latitudeTextView.setText(String.valueOf(weatherInfo.coord.lat));
        latitudeTextView.setText(String.valueOf(weatherInfo.coord.lat));
        longitudeTextView.setText(String.valueOf(weatherInfo.coord.lon));

        countryTextView.setText(String.valueOf(weatherInfo.sys.country));
        descriptionTextView.setText(String.valueOf(weatherInfo.weather[0].description));
        tempTextView.setText(String.valueOf(weatherInfo.main.temp));
        temp=String.valueOf(weatherInfo.main.temp);

        humidityTextView.setText(String.valueOf(weatherInfo.main.humidity));
         humid=String.valueOf(weatherInfo.main.humidity);

        tempMinTextView.setText(String.valueOf(weatherInfo.main.temp_min));
        tempMaxTextView.setText(String.valueOf(weatherInfo.main.temp_max));
        cityname=(String.valueOf(weatherInfo.name));

         countryname=(String.valueOf(weatherInfo.sys.country));

        speedTextView.setText(String.valueOf(weatherInfo.wind.speed));
        speed=String.valueOf(weatherInfo.wind.speed);

        nameTextView.setText(String.valueOf(weatherInfo.name));
        updateUI();
    }
}
public void updateUI()
{
    f=Double.parseDouble(temp);
    c= (f-273.15);
    temp = String.valueOf(c);
    mContext=getActivity();
    KeyValueDB.setTemp(mContext, temp);
    mContext=getActivity();
    KeyValueDB.setHumidity(mContext, humid);
    mContext=getActivity();
    KeyValueDB.setCity(mContext, cityname);
    mContext=getActivity();
    KeyValueDB.setCountry(mContext, countryname);
    mContext=getActivity();
    KeyValueDB.setSpeed(mContext, speed);
}


}
公共类详细信息片段扩展片段{
公共最终字符串AppID=“ebfcac32bda131ed5a160f2757938396”;
公共最终字符串BASE_URL=”http://api.openweathermap.org";
@BindView(R.id.latitudeTextView)
TextView latitudeTextView;
@BindView(R.id.longitudeTextView)
文本视图纵向视图;
@BindView(R.id.countryTextView)
TextView countryTextView;
@BindView(R.id.descriptionTextView)
文本视图描述文本视图;
@BindView(R.id.extView)
文本视图和文本视图;
@BindView(R.id.humidityTextView)
TextView-HumidityExtView;
@BindView(R.id.tempMinTextView)
文本视图tempMinTextView;
@BindView(R.id.tempMaxTextView)
TextView tempMaxTextView;
@BindView(R.id.speedTextView)
TextView speedTextView;
@BindView(R.id.nameTextView)
TextView名称TextView;
private ProgressDialog mProgressDialog;
专用浏览页面McAtegoriesViewPage;
私人摊位摊位;
私有视图mViewHome;
字符串城市;
视图;
语境;
视图组根视图;
公共详细信息片段(){
}
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
rootView=(视图组)充气器
.充气(右布局图、项目详细信息、容器、假);
mProgressDialog=newprogressdialog(getActivity());
设置消息(getString(R.string.fetching_weather_info));
mProgressDialog.setCancelable(假);
天气更新();
FloatingActionButton fab=(FloatingActionButton)rootView.findViewById(R.id.fab);
setImageDrawable(ContextCompat.getDrawable(getActivity(),R.drawable.cityicon));
fab.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
AlertDialog.Builder=新建AlertDialog.Builder(getActivity());
建筑商名称(“变更城市”);
最终编辑文本输入=新编辑文本(getActivity());
input.setInputType(InputType.TYPE\u CLASS\u TEXT);
builder.setView(输入);
setPositiveButton(“Go”,新的DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int which){
changeCity(input.getText().toString());
}
});
builder.show();
}
});
返回rootView;
}
public-void-weatherUpdate(){
ButterKnife.bind(这个,rootView);
语境;
LinearLayoutManager LinearLayoutManager=新的LinearLayoutManager(getActivity().getBaseContext());
mContext=getContext();
字符串city=KeyValueDB.getCity(mContext);
城市=城市;
改装=新改装
.Builder()
.baseUrl(基本URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
WeatherService WeatherService=改装.create(WeatherService.class);
Call weatherInfoCall=weatherService.getWeatherInfo(城市,AppID);
enqueue(新的回调函数(){
@凌驾
公共void onResponse(调用、响应){
showProgressDialog(假);
onDisplayWeatherInfo(response.body());
showProgressDialog(false);}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
showProgressDialog(假);
Toast.makeText(getActivity(),R.string.fetching_weather_info_failure,Toast.LENGTH_SHORT).show();
}
});
}
专用void showProgressDialog(布尔显示){
如果(显示){
如果(!mProgressDialog.isShowing()){
mProgressDialog.show();
}
}否则{
if(mProgressDialog.isShowing()){
mProgressDialog.disclose();
}
}
}
公共城市(字符串城市){
mContext=getActivity();
KeyValueDB.setCity(mContext,city);
城市=城市;
天气更新();
}
双f,c;
字符串潮湿、温度、速度、国家名称、城市名称;
私有void onDisplayWeatherInfo(WeatherInfo WeatherInfo){
如果(weatherInfo!=null){
latitudeTextView.setText(String.valueOf(weatherInfo.coord.lat));
latitudeTextView.setText(String.valueOf(weatherInfo.coord.lat));
longitudeTextView.setText(String.valueOf(weatherInfo.coord.lon));
countryTextView.setText(String.valueOf(weatherInfo.sys.country));
descriptionTextView.setText(String.valueOf(weatherInfo.weather[0].description));
testextview.setText(String.valueOf(weatherInfo.main.temp));
temp=字符串值of(weatherInfo.main.temp);
humidityTextView.setText(String.valueOf(weatherInfo.main.湿度));
潮湿=字符串.valueOf(weatherInfo.main.湿度);
tempMinTextView.setText(String.valueOf(weatherInfo.main.temp_min));
tempMaxTextView.setText(String.valueOf(weatherInfo.main.temp_max));
cityname=(String.valueOf(weatherInfo.name));
countryname=(String.valueOf(weatherInfo.sys.country));
speedTextView.setText(String.valueOf(weatherInfo.wind.speed));
速度=字符串.valueOf(weatherInfo.wind.speed);
nameTextView.setText(String.valueOf(weatherInfo.name));
updateUI();
}
}
公共void updateUI()
{
f=双精度。双精度(温度);
c=(f-273.15);
temp=字符串的值(c);
mContext=getActivity();
KeyValueDB.settmp(mContext,temp);
mContext=getActivity();
KeyValueDB.设置湿度(mContext,潮湿);
mContext=getActivity();
KeyValueDB.setCity(mContext,cityname);
mContext=getActivity();
KeyValueDB.setCountry(mContext,countryname);
mContext=getActivity();
KeyValueDB.setSpeed(mContext,速度);
}
}