我一直在研究如何使用制表符完成一项活动,并且已经接近完成。不幸的是,我遇到了一些障碍。
在遵循了一个很好的指导之后,我创建了一个带有tabLayout和ViewPager的交互式活动来显示片段。
每个选项卡都显示相同的布局,左侧是一列TextView,保留其默认值,另一列是需要从不同位置获取其值的TextView。
作为测试,我让每个选项卡向其各自的TextView发送不同的文本值(示例选项卡#1被分配为使用“楼下”,#2被分配为使用“楼上”)-通过onTabSelected侦听器完成
我遇到的问题是将文本分配给TextViews。选择选项卡时,它永远不会显示。有时会显示在我打开的下一个标签上。在打开选项卡3之前,我已经打开选项卡2之后,有时它还会在选项卡3中显示选项卡1分配值。
我使用setText()时,我处理过的其他所有TextView都会立即进行更新。但是,我对使用Tab活动非常陌生,所以我很可能会搞砸一些东西。
Distribution.java
package local.mobileapps.bryandouglas.supremeshoppinglist;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
public class Distribution extends FragmentActivity {
private Context context;
private TabLayout tabLayout;
private ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_distribution);
context = this;
viewPager = (ViewPager) findViewById(R.id.viewpager);
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
switch (tab.getPosition()) {
case 0:
TabFragment.cream.setText("Downstairs");
TabFragment.freshnessBottles.setText("Downstairs");
TabFragment.soap.setText("Downstairs");
TabFragment.paperCups.setText("Downstairs");
TabFragment.shortStirSticks.setText("Downstairs");
TabFragment.longStirSticks.setText("Downstairs");
TabFragment.cafeRoyale.setText("Downstairs");
TabFragment.hotChocolate.setText("Downstairs");
TabFragment.equal.setText("Downstairs");
TabFragment.sugar.setText("Downstairs");
TabFragment.greenTea.setText("Downstairs");
TabFragment.orangePekoe.setText("Downstairs");
break;
case 1:
TabFragment.cream.setText("Upstairs");
TabFragment.freshnessBottles.setText("Upstairs");
TabFragment.soap.setText("Upstairs");
TabFragment.paperCups.setText("Upstairs");
TabFragment.shortStirSticks.setText("Upstairs");
TabFragment.longStirSticks.setText("Upstairs");
TabFragment.cafeRoyale.setText("Upstairs");
TabFragment.hotChocolate.setText("Upstairs");
TabFragment.equal.setText("Upstairs");
TabFragment.sugar.setText("Upstairs");
TabFragment.greenTea.setText("Upstairs");
TabFragment.orangePekoe.setText("Upstairs");
break;
case 2:
TabFragment.cream.setText("Shipping");
TabFragment.freshnessBottles.setText("Shipping");
TabFragment.soap.setText("Shipping");
TabFragment.paperCups.setText("Shipping");
TabFragment.shortStirSticks.setText("Shipping");
TabFragment.longStirSticks.setText("Shipping");
TabFragment.cafeRoyale.setText("Shipping");
TabFragment.hotChocolate.setText("Shipping");
TabFragment.equal.setText("Shipping");
TabFragment.sugar.setText("Shipping");
TabFragment.greenTea.setText("Shipping");
TabFragment.orangePekoe.setText("Shipping");
break;
case 3:
TabFragment.cream.setText("Solutions");
TabFragment.freshnessBottles.setText("Solutions");
TabFragment.soap.setText("Solutions");
TabFragment.paperCups.setText("Solutions");
TabFragment.shortStirSticks.setText("Solutions");
TabFragment.longStirSticks.setText("Solutions");
TabFragment.cafeRoyale.setText("Solutions");
TabFragment.hotChocolate.setText("Solutions");
TabFragment.equal.setText("Solutions");
TabFragment.sugar.setText("Solutions");
TabFragment.greenTea.setText("Solutions");
TabFragment.orangePekoe.setText("Solutions");
break;
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
}
TabFragment.java
package local.mobileapps.bryandouglas.supremeshoppinglist;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import static android.content.Context.MODE_PRIVATE;
public class TabFragment extends Fragment {
protected static final String ADMIRAL_DOWNSTAIRS_PREFS_NAME = "AdmiralDownstairsPrefsFile";
protected static final String ADMIRAL_UPSTAIRS_PREFS_NAME = "AdmiralUpstairsPrefsFile";
protected static final String ADMIRAL_SHIPPING_PREFS_NAME = "AdmiralShippingPrefsFile";
protected static final String SOLUTIONS_PREFS_NAME = "SolutionsPrefsFile";
protected static SharedPreferences AdmiralDownstairsData;
protected static SharedPreferences AdmiralUpstairsData;
protected static SharedPreferences AdmiralShippingData;
protected static SharedPreferences SolutionsData;
protected static SharedPreferences.Editor editor;
int position;
protected static TextView cream;
protected static TextView freshnessBottles;
protected static TextView soap;
protected static TextView paperCups;
protected static TextView shortStirSticks;
protected static TextView longStirSticks;
protected static TextView cafeRoyale;
protected static TextView hotChocolate;
protected static TextView equal;
protected static TextView sugar;
protected static TextView greenTea;
protected static TextView orangePekoe;
public static Fragment getInstance(int position) {
Bundle bundle = new Bundle();
bundle.putInt("pos", position);
TabFragment tabFragment = new TabFragment();
tabFragment.setArguments(bundle);
return tabFragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
position = getArguments().getInt("pos");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_tab, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
cream = view.findViewById(R.id.lblCreamValue);
freshnessBottles = view.findViewById(R.id.lblMilkValue);
soap = view.findViewById(R.id.lblSoapValue);
paperCups = view.findViewById(R.id.lblCupValue);
shortStirSticks = view.findViewById(R.id.lbl4_5StirValue);
longStirSticks = view.findViewById(R.id.lbl7StirValue);
cafeRoyale = view.findViewById(R.id.lblCafeRoyaleValue);
hotChocolate = view.findViewById(R.id.lblHotChocValue);
equal = view.findViewById(R.id.lblEqualValue);
sugar = view.findViewById(R.id.lblSugarValue);
greenTea = view.findViewById(R.id.lblGreenTeaValue);
orangePekoe = view.findViewById(R.id.lblOrangePekoeValue);
}
}
ViewPagerAdapter.java
package local.mobileapps.bryandouglas.supremeshoppinglist;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
class ViewPagerAdapter extends FragmentPagerAdapter {
private String title[] = {"Admiral - Downstairs", "Admiral - Upstairs", "Admiral - Shipping", "Solutions"};
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return TabFragment.getInstance(position);
}
@Override
public int getCount() {
return title.length;
}
@Override
public CharSequence getPageTitle(int position) {
return title[position];
}
}
我试图以一种使用SharedPreferences来存储和读取持久数据的方式来设计它。每个对应的选项卡都有一个特定的sharedPrefs文件。您会注意到sharedPrefs尚未实现,就像我只想使用基本setText进行测试那样。