我有一个FragmentActivity和FragmentPagerAdapter,它包含许多Fragment对象,我可以通过水平滑动来滑动它们.
我想知道我是否可以从Fragment对象中访问FragmentActivity中的元素("elementToBeChanged")?我想将Fragment类中的String传递给FragmentActivity对象中的TextView元素.
public class GalleryPagerActivity extends FragmentActivity implements OnClickListener {
private TextView elementToBeChanged;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.gallery_pager);
elementToBeChanged = (TextView) findViewById(R.id.tvTop);
elementToBeChanged.setOnClickListener(this);
}
}
Run Code Online (Sandbox Code Playgroud)
我怎么会这样做?每当Fragment对象进入视线(使用滑动)时,都必须更新TextView元素.
它应该看起来像这样:
public class GalleryFragment extends Fragment {
private FragmentActivity fa;
private LinearLayout ll;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
fa = super.getActivity();
ll = (LinearLayout) inflater.inflate(R.layout.gallery_fragment, container, false);
...
// ////////
// UPDATE THE "TextView elementToBeChanged" object in the parent FragmentActivity class HERE WHENEVER THIS FRAGMENTS BECOMES …Run Code Online (Sandbox Code Playgroud) 我已经使用名为TabHostFragment的片段内的动作栏实现了标签
public class TabHostFragment extends Fragment {
public TabHostFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(savedInstanceState == null){
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_tab_host, null, false);
ViewPager mViewPager = (ViewPager) rootView.findViewById(R.id.pager);
ActionBar actionbar = this.getActivity().getActionBar();
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionbar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
TabsAdapter mTabsAdapter = new TabsAdapter(this, mViewPager);
mTabsAdapter.addTab(actionbar.newTab().setText("Summoner"),
SummonerFragment.class, null);
mTabsAdapter.addTab(actionbar.newTab().setText("Games"),
GameHistoryListFragment.class, null);
mTabsAdapter.addTab(actionbar.newTab().setText("League"),
LeagueFragment.class, null);
mTabsAdapter.addTab(actionbar.newTab().setText("Masteries"),
MasteriesFragment.class, null);
mTabsAdapter.addTab(actionbar.newTab().setText("Runes"),
RunesFragment.class, null);
return rootView;
}
Run Code Online (Sandbox Code Playgroud)
}`
TabsAdapter类在这里
public class TabsAdapter …Run Code Online (Sandbox Code Playgroud) tabs android-fragments android-actionbar fragmentpageradapter
onCreateView因为我在FragmentPagerAdapter中的第一个片段不是很快.因此,将curent tab更改为第一个有延迟.
如何禁用在选项卡选项卡上的FragmentPagerAdapter中重新创建第一个片段?
private class TabsAdapter extends FragmentPagerAdapter implements ViewPager.OnPageChangeListener, ActionBar.TabListener {
private final Context context;
private ActionBar bar;
private final ViewPager viewPager;
private final ArrayList<TabInfo> tabs = new ArrayList<TabInfo>();
final class TabInfo {
private final Class<?> clss;
private final Bundle args;
TabInfo(Class<?> _class, Bundle _args) {
clss = _class;
args = _args;
}
}
TabsAdapter(SherlockFragmentActivity activity, ViewPager pager) {
super(activity.getSupportFragmentManager());
context = activity;
bar = activity.getSupportActionBar();
viewPager = pager;
viewPager.setAdapter(this);
viewPager.setOnPageChangeListener(this);
}
public void addTab(ActionBar.Tab tab, Class<?> clss, …Run Code Online (Sandbox Code Playgroud) 我有一个奇怪的问题,即片段视图无法正确刷新,但只有在应用程序关闭并在一段时间后重新打开后才会发生。
我有一个TabSwitcherActivity包含ViewPager. 的ViewPager3个片段之间切换,并且每个片段表示相同的信息的不同视图。可能会发生某些事件,这会导致信息变得陈旧,因此需要通知片段以刷新视图。为此,我TabSwitcherActivity调用了一个方法,该方法notifyDataSetChanged()将迭代 3 个片段并告诉它们刷新。这是(我认为)最相关的代码:
public class TabSwitcherActivity extends FragmentActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Used to keep track of the child fragments of this activity so we can update their list adapters.
mFragments = new ArrayList<Fragment>();
// Set up the view pager and tab adapter.
setContentView(R.layout.pager_layout);
mViewPager = (ViewPager) super.findViewById(R.id.pager);
mFragments.add(Fragment.instantiate(this, FragmentOne.class.getName()));
mFragments.add(Fragment.instantiate(this, FragmentTwo.class.getName()));
mFragments.add(Fragment.instantiate(this, FragmentThree.class.getName()));
mViewPager.setAdapter(new ListPagerAdapter(super.getSupportFragmentManager(), mFragments));
}
public void notifyDataSetChanged() {
for (Fragment …Run Code Online (Sandbox Code Playgroud) 我正在测试FragmentPagerAdapter,之前我在一个类中完成了所有这些操作.一切正常,但是一旦我分离了SectionsPagerAdapter类,getString就不能在getPageTitle函数下工作了.
我知道getPageTitle是PagerAdapter类的一部分,但我想知道在这个类中包含该函数的最佳方法是什么.我需要延长课程吗?
SectionsPageAdapter类
import java.util.Locale;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
// A FragmentPagerAdapter that returns a fragment corresponding to one of the sections/tabs/pages.
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
Fragment fragment = new …Run Code Online (Sandbox Code Playgroud) 我有FragmentPagerAdapter的问题.
我无法保存片段的状态,然后在片段中有视图.每当我使用左右滑动时,通过覆盖扩展FragmentPagerAdapter的静态类中的方法getItem(int position)来重新创建Fragment.
public static class GraphicsCollectionPagerAdapter extends FragmentPagerAdapter {
final int NUM_ITEMS = 3; // number of tabs
public GraphicsCollectionPagerAdapter(FragmentManager fm) {
super(fm);
fragmentList = new AnalyzeFragmentPageListWithDate();
fragment1 = new AnalyzeFragmentPage1();
fragment2 = new AnalyzeFragmentPage2();
}
@Override
public Fragment getItem(int position) {
//Log.i(TAG, "getItem() -> New fragment at position " + position);
switch (position) {
case 0:
return fragmentList;
case 1:
return fragment1;
case 2:
return fragment2;
}
return null;
}
@Override
public int getCount() {
return 3;
} …Run Code Online (Sandbox Code Playgroud) java android android-fragments fragmentpageradapter android-sqlite
我正在使用view pager(动态)来显示来自服务的数据.它工作正常但每当我再次调用服务时,视图寻呼机中的数据需要刷新和更新新数据(来自服务).
视图寻呼机中的所有片段都使用新数据进行刷新,但视图寻呼机的前两个片段未使用新数据进行更新,这两个片段(第一个和第二个)仅显示以前的数据.请任何人帮助我.
我的代码:
RechargeplansActivity.java:
public class RechargeplansActivity extends FragmentActivity /*implements RechargePlansFragment.UserInterface*/ {
static final String TAG = RechargeplansActivity.class.getSimpleName();
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
Intent intent;
private ArrayList<String> mPlanNamesList;
private ArrayList<BrowsePlan> mBrowsePlansList;
TextView tx_network;
Dialog message_dialg;
Context context;
String operatorid,circleid;
Map<String, ArrayList<BrowsePlan>> mBrowsePlansMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rechargeplans);
context = this;
intent = getIntent();
setPlan_types(new ArrayList<String>());
setBrowseplans(new ArrayList<BrowsePlan>());
mBrowsePlansMap = new HashMap<String, ArrayList<BrowsePlan>>();
//db = DatabaseHandler.getInstance(getApplicationContext());
initUI();
operatorid=intent.getStringExtra("op_id");
circleid=intent.getStringExtra("cir_id");
executeBrowsePlans();
tx_network.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) …Run Code Online (Sandbox Code Playgroud) 我正在尝试为我的片段创建一个适配器,但我得到了这个:
错误:(19, 72) 错误:类型不兼容:android.app.FragmentManager 无法转换为 android.support.v4.app.FragmentManager
这是我主要活动的一部分:
import android.app.Activity;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
public class MainActivity extends Activity {
CustomPagerAdapter mCustomPagerAdapter;
ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// == Setting up the ViewPager ==
mCustomPagerAdapter = new CustomPagerAdapter(getFragmentManager(),this); <--- Error is on this line
Run Code Online (Sandbox Code Playgroud)
这是我的 customAdapter 类的一部分:
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class CustomPagerAdapter extends FragmentPagerAdapter {
protected Context mContext;
public CustomPagerAdapter(FragmentManager fm, Context context) {
super(fm);
mContext = context; …Run Code Online (Sandbox Code Playgroud) getActivity()null如果之前被调用,片段内将返回fragment.onAttach(activity).
fragment.onActivityCreated(savedInstanceState)被称为fragment.onAttach(activity).有关片段生命周期的信息,请参阅developer.android.
我的片段中有一个尝试实例化数据库连接的方法.最初调用该方法时,它可以正常工作.
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
Log.d("MyTag", "Activity being created");
if (savedInstanceState == null){
listAdapter.setContext(getActivity());
execSearch(0);
} else { listAdapter.setContext(getActivity()); }
}
public void execSearch(Long searchId) {
MySQLiteHelper dbHelper = new MySQLiteHelper(getActivity());
SQLiteDatabase database = dbHelper.getReadableDatabase();
...
}
Run Code Online (Sandbox Code Playgroud)
现在,当在列表视图中单击按钮后通过接口调用此相同方法时,getActivity()返回null(在片段中).
以下是ListAdapter中的按钮回调:
private final View.OnClickListener editSearchListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
Long search_id = (Long) v.getTag();
((MyInterface) context).searchCalled(search_id);
}
};
Run Code Online (Sandbox Code Playgroud)
以及MainActivity中的接口实现:
@Override …Run Code Online (Sandbox Code Playgroud) 我有一个MainActivIty,它支持两个片段,即Home和Profile。Home具有一个TabLayout,而TabLayout依次具有趋势,关注和附近三个片段。因此,问题在于,首次加载Home时,它会显示Tab片段的内容,并且Tab布局的滑块会正确移动,但是当我进行剖析然后回到home时,则不会加载Tab布局的片段,而从左向右滑动不能平稳移动这里是代码
public class MainActivity extends AppCompatActivity {
ImageButton home_btn,myProfile_btn;
FragmentManager fragmentManager;
FragmentTransaction fragmentTransaction;
TextView txt_action_bar;
Home home;
ProfileFragment profile;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
home_btn=(ImageButton)findViewById(R.id.home_btn) ;
discover_btn=(ImageButton)findViewById(R.id.discover_btn) ;
myProfile_btn=(ImageButton)findViewById(R.id.profile_btn) ;
notification_btn=(ImageButton)findViewById(R.id.notification_btn) ;
plus_btn=(ImageButton)findViewById(R.id.btn_plus) ;
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
txt_action_bar = (TextView)findViewById(R.id.toolbar_title);
displayMetrics = getResources().getDisplayMetrics();
Typeface custom_font = Typeface.createFromAsset(getAssets(),"fonts/NoteworthyLight.ttf");
txt_action_bar.setTypeface(custom_font);
setSupportActionBar(toolbar);
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
home = new Home();
profile = new ProfileFragment();
fragmentTransaction.add(R.id.main_fragment_container,home,"home");
fragmentTransaction.commit();
}
public void onClickHome(View view)
{
fragmentManager = getSupportFragmentManager();
fragmentTransaction …Run Code Online (Sandbox Code Playgroud)