gpt4 book ai didi

android ViewPager 重新加载后在错误的随机位置启动

转载 作者:行者123 更新时间:2023-11-30 00:28:52 26 4
gpt4 key购买 nike

我正在编写一个简单的 View 寻呼机测试应用程序,它从网站加载大量数据并创建页面来显示它们。在大多数情况下,显示几乎都是有效的。但是,当我加载要显示的一组新数据时,它会分崩离析。当我得到一个新集时,它会很好地加载所有数据,而不是从 View 0 开始,而是从随机位置开始。它可以从末尾或中间的某个地方开始。我希望所有数据都从第 1 页加载,就像一本书一样。如果您加载一本新书,该书应该从第 1 页开始,而不是随机页面。

重新加载调用

我试过使用 GotCampDetails() 来设置类中的局部变量。

viewPager.setCurrentItem(0);

在我的 fragment 中,但它不起作用。在我的适配器上也调用了 notifydatasetchanged(),但仍然没有用。

我错过了什么?

public class ViewPagerFragment extends Fragment implements WebReadResultsListener {

private ArrayList<HashMap<String, String>> campDetails;
CustomPagerAdapter adapter;

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

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

return inflater.inflate(R.layout.camp_viewpager, container, false);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {

if (campDetails != null && ! campDetails.isEmpty()) {
ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewpager);
adapter = new CustomPagerAdapter(getActivity(), campDetails, viewPager);
viewPager.setAdapter(adapter);
viewPager.setPageTransformer(true, new ZoomOutPageTransformer());
}
}

@Override
public boolean GotCampDetails(ArrayList<HashMap<String, String>> campDetails) {
this.campDetails = campDetails;
return true;
}

@Override
public boolean GotCampList(ArrayList<HashMap<String, String>> campDetails) {
return false;
}

@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
getFragmentManager().putFragment(outState,"fragmentInstanceSaved",getFragmentManager().findFragmentById(R.id.content));
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Handle orientation changes here
}
}

适配器:

public class CustomPagerAdapter extends PagerAdapter implements ColorChangeListener {
private ArrayList<HashMap<String, String>> campDetails;
private ViewGroup collection;
private Context mContext;
private boolean isRunning = false;
private ColorChangeListener colorChangeListener;
private int color= pink;
private int currentposition;
private ViewPager viewPager;

public CustomPagerAdapter(Context context, final ArrayList<HashMap<String, String>> campDetails, ViewPager viewPager) {
this.campDetails=campDetails;
this.mContext = context;
this.viewPager = viewPager;
colorChangeListener = (ColorChangeListener) mContext;

viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int pos) {
currentposition = pos;
if (campDetails.get(currentposition).get("color").equals("green")) {
color = green;
} else {
color = pink;
}
ColorAppBar(color);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}

@Override
public Object instantiateItem(ViewGroup collection, final int position) {
LayoutInflater inflater = LayoutInflater.from(mContext);
ViewGroup layout = (ViewGroup) inflater.inflate(R.layout.child_item, collection, false);

if (campDetails.size() <= 0) {
Toast.makeText(mContext, "Error in getting camp details", Toast.LENGTH_SHORT).show();
return null;
}

if (campDetails.get(position).get("description").equals("RD") ) {
layout.setBackgroundColor(mContext.getResources().getColor(green));
} else {
layout.setBackgroundColor(mContext.getResources().getColor(pink));
}

collection.addView(layout);
this.collection = collection;

TextView eName = (TextView) layout.findViewById(R.id.en);
TextView nEName= (TextView) layout.findViewById(R.id.nen);
TextView textView6 = (TextView) layout.findViewById(R.id.textView6);
// ImageView nextImg = (ImageView) groupLayout.get(index).findViewById(R.id.nextImg);

eName.setText(campDetails.get(position).get("name"));
if (position+1 < campDetails.size()) {
nEName.setText(campDetails.get(position+1).get("name"));
textView6.setVisibility(View.VISIBLE);
} else {
nEName.setText("You are DONE!!");
textView6.setVisibility(View.INVISIBLE);
}


return layout;
}

@Override
public void destroyItem(ViewGroup collection, int position, Object view) {
collection.removeView((View) view);
}

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

@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}

@Override
public CharSequence getPageTitle(int position) {
return campDetails.get(position).get("name");
}

@Override
public void ColorAppBar(int color) {
colorChangeListener.ColorAppBar(color);
}
}

最佳答案

我终于成功了。

我不得不将函数调用 setCurrentItem 延迟几毫秒。比赛条件?我不知道为什么会这样,但现在它工作得很好。这是我添加的内容,以防其他人发现它有用:

    pager.postDelayed(new Runnable() {

@Override
public void run() {
pager.setCurrentItem(pos);
}
}, 100);

关于android ViewPager 重新加载后在错误的随机位置启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44863951/

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