gpt4 book ai didi

java - PageAdapter 不输出正常数据

转载 作者:行者123 更新时间:2023-11-29 18:58:30 25 4
gpt4 key购买 nike

我仍在学习并尝试实现 PageAdapter,但有些事情我不明白。

public class Card
{
public Card(final Context iContext, final Class<?> iNextActivity, int iDrawable)
{
drawable = iDrawable;
onClick = new View.OnClickListener()
{
@Override
public void onClick(View view)
{
//this just opens an activity using Intent
ActivityManipulator.openActivity(iContext, iNextActivity);
}
};
}
public View.OnClickListener onClick;
public int drawable;
}


public class InfiniteCycleViewPagerAdapter extends PagerAdapter {

public InfiniteCycleViewPagerAdapter(Context iContext, List<Card> iCards)
{
cards = iCards;
context = iContext;
layoutInflater = LayoutInflater.from(iContext);
}
@Override
public int getCount() {
return cards.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view.equals(object);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object)
{
container.removeView((View) object);
}
@Override
public Object instantiateItem(ViewGroup container, final int position)
{
Log.d("instantiateItem", Integer.toString(position));
View view = layoutInflater.inflate(R.layout.card_item, container, false);
ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
Card card = cards.get(position);
imageView.setOnClickListener(card.onClick);
imageView.setImageResource(card.drawable);
container.addView(view);
return view;
}
Context context;
LayoutInflater layoutInflater;
List<Card> cards;
}

初始化:

public class MyActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
List<Card> cards = new ArrayList<>();
cards.add(new Card(this, ActivityProductList.class, R.drawable.product));
cards.add(new Card(this, ActivityEmployeeList.class, R.drawable.employee));
cards.add(new Card(this, ActivityCustomerList.class, R.drawable.customer));

HorizontalInfiniteCycleViewPager pager = findViewById(R.id.cycle);

pager.setAdapter(new InfiniteCycleViewPagerAdapter(this, cards));

Log.d("dashboard::oncreate", "dashboard");
}

}
  1. 如您所见,我将 3 张卡片馈送到 PageAdapter 对象,为什么我的 Log.d("instantiateItem", Integer.toString(position));输出这个:


实例化项目:0
实例化项目:2
实例化项目:1
实例化项目:1
实例化项目:2

我不明白为什么它是 02112,为什么它被调用了 5 次?

  1. 为什么 Log.d("dashboard::oncreate", "dashboard"); 在我的 InfiniteCycleViewPagerAdapter 中的 instantiateItem 方法之前调用?

最佳答案

您看到对 instantiateItem 的多次调用很可能与 Horizo​​ntalInfiniteCycleViewPager 的内部实现有关。

我会猜测并推测它可能实例化当前“页面”,并将其他“页面”外推到两个“方向”。这样,如果向左滑动,您将转到第 1 页;如果向右滑动,则转到第 2 页。

您会在 instantiateItem 之前看到对 Log.d("dashboard::oncreate", "dashboard"); 的调用,因为 Android UI 线程使用所谓的 Looper(循环线程是 GUI 实现中非常常见的方法)。

大致情况如下:

  1. onCreate 由 UI Looper 在 UI 线程上运行
  2. onCreate 期间,调用 pager.setAdapter(new InfiniteCycleViewPagerAdapter(this, cards))
  3. 在您的 ViewPager 中的某处有处理初始化的调用。这个调用不是直接调用方法,而是在UI Looper中添加“初始化”Runnable对象(添加Runnables的机制叫做处理程序)
  4. “初始化”Runnable 正在 UI Looper 中等待在它完成之前添加的所有其他工作。这项工作包括上述对 onCreate
  5. 的调用
  6. 一旦 UI Looper 上的所有其他工作完成,“初始化”Runnable 将在 UI 线程上运行

如果您想深入了解 LooperHandler 和事件线程,我推荐 this YouTube video by Douglas Schmidt .

关于java - PageAdapter 不输出正常数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49476369/

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