PageAdapter 不输出正常数据
PageAdapter is not outputting normal datas
我仍在学习并尝试实现 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");
}
}
- 如您所见,我将 3 张卡片送入
PageAdapter
对象,为什么我的 Log.d("instantiateItem", Integer.toString(position));
输出如下:
instantiateItem: 0
instantiateItem: 2
instantiateItem: 1
instantiateItem: 1
instantiateItem: 2
我不明白为什么它是 02112,为什么它被调用了 5 次?
- 为什么
Log.d("dashboard::oncreate", "dashboard");
在我的 InfiniteCycleViewPagerAdapter 中的 instantiateItem 方法之前被调用?
您看到多次调用 instantiateItem
的事实很可能与 HorizontalInfiniteCycleViewPager
的内部实现有关。
我会猜测并推测它可能实例化当前 "page",并将其他 "pages" 外推到两个 "directions"。这样,如果您向左滑动,您将进入第 1 页,如果您向右滑动,您将进入第 2 页。
您在 instantiateItem
之前看到对 Log.d("dashboard::oncreate", "dashboard");
的调用,因为 Android UI 线程使用 so-called Looper
(循环线程很常见GUI 实现的方法)。
大致情况如下:
onCreate
是 UI Looper
在 UI 线程 上的 运行
- 在
onCreate
期间,调用 pager.setAdapter(new InfiniteCycleViewPagerAdapter(this, cards))
- 在您的
ViewPager
中某处有处理初始化的调用。这个调用不是直接的方法调用,而是"initialization" Runnable
object to UI Looper
的adding(添加Runnables
的机制叫做Handler
)
- "initialization"
Runnable
正在 UI Looper
中等待在它完成之前添加的所有其他工作。这项工作包括上述对 onCreate
的调用
- 在 UI
Looper
上的所有其他工作完成后,"initialization" Runnable
在 UI 线程 上 运行
如果您想深入了解 Looper
、Handler
和事件线程,我推荐 this YouTube video by Douglas Schmidt。
我仍在学习并尝试实现 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");
}
}
- 如您所见,我将 3 张卡片送入
PageAdapter
对象,为什么我的Log.d("instantiateItem", Integer.toString(position));
输出如下:
instantiateItem: 0
instantiateItem: 2
instantiateItem: 1
instantiateItem: 1
instantiateItem: 2
我不明白为什么它是 02112,为什么它被调用了 5 次?
- 为什么
Log.d("dashboard::oncreate", "dashboard");
在我的 InfiniteCycleViewPagerAdapter 中的 instantiateItem 方法之前被调用?
您看到多次调用 instantiateItem
的事实很可能与 HorizontalInfiniteCycleViewPager
的内部实现有关。
我会猜测并推测它可能实例化当前 "page",并将其他 "pages" 外推到两个 "directions"。这样,如果您向左滑动,您将进入第 1 页,如果您向右滑动,您将进入第 2 页。
您在 instantiateItem
之前看到对 Log.d("dashboard::oncreate", "dashboard");
的调用,因为 Android UI 线程使用 so-called Looper
(循环线程很常见GUI 实现的方法)。
大致情况如下:
onCreate
是 UILooper
在 UI 线程 上的 运行
- 在
onCreate
期间,调用pager.setAdapter(new InfiniteCycleViewPagerAdapter(this, cards))
- 在您的
ViewPager
中某处有处理初始化的调用。这个调用不是直接的方法调用,而是"initialization"Runnable
object to UILooper
的adding(添加Runnables
的机制叫做Handler
) - "initialization"
Runnable
正在 UILooper
中等待在它完成之前添加的所有其他工作。这项工作包括上述对onCreate
的调用
- 在 UI
Looper
上的所有其他工作完成后,"initialization"Runnable
在 UI 线程 上 运行
如果您想深入了解 Looper
、Handler
和事件线程,我推荐 this YouTube video by Douglas Schmidt。