gpt4 book ai didi

Android 刷新 fragment 内的 ViewPager

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:18:49 24 4
gpt4 key购买 nike

我试图在发布之前在 SO 内部进行搜索,但我无法理解如何刷新 fragment 中托管的 ViewPager 的内容。你能帮帮我吗?

谢谢你的时间

这是我的代码。我有在其中托管 ViewPager 的 fragment Activity 。它显示不同的小部件,ViewPager 显示不同的图像,我在代码中调用从数据库“carosello”返回的图像列表。当我执行搜索时,我调用 refresh() 方法作为托管此 fragment 的主要 Activity 的回调,然后我再次设置所有 View 。适用于 fragment 中的所有信息,但不适用于 ViewPager:始终显示最旧(首次创建)的图像。

public class Page2FamigliaFragment extends Fragment {

//... variable declaration

@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {

// fragment not when container null
if (container == null) {
return null;
}
// inflate view from layout
View view = (LinearLayout)inflater.inflate(R.layout.famiglia_page2,container,false);

//retrieve my object from the main view that host this fragment
info = ((MainFamiglia)this.getActivity()).getMyObject();

//run the setup of the view.
setUpMyPage2View(view);

return view;
}

public void setUpMyPage2View(View view)
{
//... inizialization of some other objects and view - i do not show only because is out of topic for this matter
//retrive the image used in the page viewer
carosello = info.getCarosello();
parts = carosello.split(",");

//how many page ??
int i = parts.length / 3;
int resto = parts.length % 3;
int pages = 0;
if (resto!=0) {
pages = i + 1;
} else {
pages = i;
}

List<Fragment> fragments = new ArrayList<Fragment>();

String[] tempArray;
int startIndex = 0;
int endIndex = 3;


if (parts.length <= 3) {

fragments.add(myCarosello.newInstance(parts));

} else {


tempArray = Arrays.copyOfRange(parts, startIndex, endIndex);

for (int p=0; p < pages; p++) {

fragments.add(myCarosello.newInstance(tempArray));
startIndex = endIndex;
endIndex = ((endIndex+3) > parts.length) ? parts.length : (endIndex+3);
tempArray = Arrays.copyOfRange(parts, startIndex, endIndex);

}

}

pageAdapter = new MyPageAdapter(getChildFragmentManager(), fragments);

pager = (ViewPager) view.findViewById(R.id.viewpager);
pager.setAdapter(pageAdapter);
pager.setOnTouchListener(new OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
if(
event.getAction() == MotionEvent.ACTION_DOWN &&
v instanceof ViewGroup
) {
((ViewGroup) v).requestDisallowInterceptTouchEvent(true);
}
return false;
}
});

}

//i call this method from a callback from the main view for refresh this fragment
public void refresh(){
//recupero oggetto dalla activity principale
info = ((MainFamiglia)this.getActivity()).getMyObject();
pager.getAdapter().notifyDataSetChanged();
pager.removeAllViews();
setUpMyPage2View(this.getView());
}
}

这是 pageAdapter 的类

public class MyPageAdapter extends FragmentPagerAdapter {

// fragments to instantiate in the viewpager
private List<Fragment> fragments;

// constructor
public MyPageAdapter(FragmentManager fm,List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}

// return access to fragment from position, required override
@Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}

// number of fragments in list, required override
@Override
public int getCount() {
return this.fragments.size();
}

public int getItemPosition(Object object) {
return POSITION_NONE;
}

这是页面查看器中 ImageView 的类

public class myCarosello extends Fragment {

public static final String EXTRA_MESSAGE = "EXTRA_MESSAGE";
private int numeroImmagini = 0;

public static final myCarosello newInstance(String[] pictures)
{
myCarosello f = new myCarosello();
Bundle bdl = new Bundle(1);
bdl.putStringArray(EXTRA_MESSAGE, pictures);
f.setArguments(bdl);

return f;
}

@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {

String[] pictures = getArguments().getStringArray(EXTRA_MESSAGE);
numeroImmagini = pictures.length;

View view = (LinearLayout)inflater.inflate(R.layout.carosello,container,false);

ImageView imageView1 = (ImageView) view.findViewById(R.id.carosello1);
ImageView imageView2 = (ImageView) view.findViewById(R.id.carosello2);
ImageView imageView3 = (ImageView) view.findViewById(R.id.carosello3);

switch (numeroImmagini) {
case 1:
imageView1.setImageResource(returnImageResource(pictures[0]));
imageView2.setVisibility(View.GONE);
imageView3.setVisibility(View.GONE);
break;
case 2:
imageView1.setImageResource(returnImageResource(pictures[0]));
imageView2.setImageResource(returnImageResource(pictures[1]));
imageView3.setVisibility(View.GONE);
break;
case 3:
imageView1.setImageResource(returnImageResource(pictures[0]));
imageView2.setImageResource(returnImageResource(pictures[1]));
imageView3.setImageResource(returnImageResource(pictures[2]));
break;

}

return view;

}

//return the correct resource name
private int returnImageResource(String image)
{
image = image.replace(" ", "_");
String filenameArray[] = image.split("\\.");
image = "img_" + filenameArray[0].toLowerCase();
int drawableImageId = getActivity().getResources().getIdentifier(image, "drawable", getActivity().getPackageName());
return drawableImageId;
}
}

最佳答案

感谢this,我已经解决了邮政将 MyPageAdapter extends FragmentPagerAdapter 更改为 MyPageAdapter extends FragmentStatePagerAdapter

关于Android 刷新 fragment 内的 ViewPager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17549903/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com