[英]How to update fragment content from activity (viewpager)?
I have a simple viewPager with two fragments. 我有一个带有两个片段的简单viewPager。 Each fragment contains a TextView.
每个片段都包含一个TextView。 I would like to be able to update the text of the current textView displayed from the activity.
我希望能够更新活动中显示的当前textView的文本。
Activity : 活动:
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
ArrayList<Fragment> fragments = new ArrayList<Fragment>();
fragments.add(Fragment.instantiate(this, Live.class.getName()));
fragments.add(Fragment.instantiate(this, Hit.class.getName()));
this.pagerAdapter = new MyPagerAdapter(getSupportFragmentManager(),fragments);
ViewPager pager = (ViewPager) findViewById(R.id.viewpager);
pager.setAdapter(pagerAdapter);
}
}
Adapter : 适配器:
public class MyPagerAdapter extends FragmentPagerAdapter {
private final ArrayList<Fragment> fragments;
public MyPagerAdapter(FragmentManager fm, ArrayList<Fragment> fragments)
{
super(fm);
this.fragments = fragments;
}
@Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}
@Override
public int getCount() {
return this.fragments.size();
}
}
Fragment : 片段:
public class Live extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.live, container,false);
}
}
XML Fragment : XML片段:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:text="First Fragment"
android:id="@+id/status"/>
</LinearLayout>
Each fragment have a TextView with the id "status". 每个片段都有一个ID为“status”的TextView。 Updating the text view in
onCreateView
works. 更新
onCreateView
的文本视图。 But how can i update the textView from anywhere in the activity ? 但是,如何从活动中的任何位置更新textView?
I have tried this without succes : 我试过这个没有成功:
Fragment : 片段:
public class Live extends Fragment {
public TextView tv;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.live, container,false);
tv = (TextView) view.findViewById(R.id.status);
return view;
}
}
Activity : 活动:
@Override
protected void onResume() {
super.onResume();
Live frag = (Live) pagerAdapter.getItem(0);
frag.tv.setText("Test 2");
}
But it gives me a nullPointerException on tv
, probably because the fragment is not yet inflated. 但它在
tv
上给了我一个nullPointerException,可能是因为片段还没有膨胀。
I already read the following questions without success : 我已经阅读了以下问题而没有成功:
In your MyPagerAdapter
class, Android calls getItem(...)
only if it wants to create new Fragments. 在
MyPagerAdapter
类中,Android仅在想要创建新片段时才调用getItem(...)
。 So it's problematic to use references there instead you should instatiate the fragments. 因此,在那里使用引用代替你应该实例化片段是有问题的。
Check this Answer support-fragmentpageradapter-holds-reference-to-old-fragments 检查此答案support-fragmentpageradapter-holds-reference-to-old-fragments
I solved this issue like this, create the adapter like this 我像这样解决了这个问题,像这样创建适配器
package com.yubraj.sample.adapter;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import com.yubaraj.sample.activities.RashiFragment;
import java.util.List;
public class PageSliderAdapter extends FragmentStatePagerAdapter {
static String[] titleList = new String[] { "today",
"this month", "this year" };
List<String> rashi;
// private static String TAG = "pagerscreenslider";
public PageSliderAdapter(FragmentManager fm, List<String> rashiList) {
super(fm);
this.rashi = rashiList;
}
public void updateData(List<String> rashisList){
rashi.clear();
this.rashi = rashisList;
notifyDataSetChanged();
}
@Override
public int getItemPosition(Object object) {
// TODO Auto-generated method stub
return POSITION_NONE;
}
@Override
public Fragment getItem(int position) {
Fragment fragment = new RashiFragment();
Bundle bundle = new Bundle();
bundle.putString("RASHI", rashi.get(position));
bundle.putInt("RASHI_TYPE", position);
fragment.setArguments(bundle);
return fragment;
}
@Override
public int getCount() {
return titleList.length;
}
@Override
public String getPageTitle(int position) {
return titleList[position];
}
}
Then from Your activity 然后从你的活动
List<String> rashiCollection = getJSONFromDb(Dates);
adapter.updateData(rashiCollection);
@Override
public int getItemPosition(Object object) {
XXXFragment fragment = (XXXFragment) object;
if(…condition){
fragment.refresh(…);
}
return super.getItemPosition(object);
}
override the above. 覆盖以上。 when you call notifyDataSetChanged(), the above method's triggered.
当你调用notifyDataSetChanged()时,上面的方法被触发了。 do your refresh as you can.
尽可能地进行刷新。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.