Android的观点与寻呼机页指示灯 [英] Android view pager with page indicator
问题描述
我需要与图像视图寻呼机文件页面的指标。这是我的code。
I need to get page indicator in the view pager file with images. Here is my code.
public class IndicatorActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MyPagerAdapter adapter = new MyPagerAdapter();
ViewPager myPager = (ViewPager) findViewById(R.id.pager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(0);
TitlePageIndicator indicator = (TitlePageIndicator)findViewById(R.id.indicat);
indicator.setViewPager( myPager );
}
}
在此code,我在 TitlePageIndicator指标得到一个错误=(TitlePageIndicator)findViewById(R.id.indicat);
为 TitlePageIndicator不能被解析为一个类型
。这是什么错误。我怎样才能解决这个问题?
In this code, i got an error in TitlePageIndicator indicator = (TitlePageIndicator)findViewById(R.id.indicat);
as TitlePageIndicator cannot be resolved to a type
. What is this error. How can I resolve it?
这里是我的XML code:
here is my xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<com.viewpagerindicator.TitlePageIndicator
android:id="@+id/indicat"
android:layout_height="wrap_content"
android:layout_width="fill_parent" />
</LinearLayout>
什么code,我需要在 TitlePageIndicator
写?
我要做到这一点,而无需使用片段
我还创建了一个类,如:
Me also created a class such as:
class MyPagerAdapter extends PagerAdapter {
private static Integer[] titles = new Integer[]
{
R.drawable.jk,R.drawable.lm,R.drawable.no
};
public int getCount() {
return 3;
}
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view;
ImageView iv;
view = inflater.inflate(R.layout.first, null);
((ViewPager) collection).addView(view, 0);
switch (position) {
case 0:
iv = (ImageView)view.findViewById(R.id.imageView1);
iv.setImageResource(titles[position]);
break;
case 1:
iv = (ImageView)view.findViewById(R.id.imageView1);
iv.setImageResource(titles[position]);
break;
case 2:
iv = (ImageView)view.findViewById(R.id.imageView1);
iv.setImageResource(titles[position]);
break;
}
return view;
}
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
}
难道我想要做的比这个类些什么?
Did I want to do anything more than this class?
在此先感谢帮助
推荐答案
下面有几件事情你需要做的:
Here are a few things you need to do:
1,下载库如果你还没有做到这一点。
1-Download the library if you haven't already done that.
2 - 导入到Eclipse中。
2- Import into Eclipse.
3设置你的项目使用的库: 项目 - >属性 - > Android的 - >向下滚动到库部分,点击添加... 的,并选择viewpagerindicator
3- Set you project to use the library: Project-> Properties -> Android -> Scroll down to Library section, click Add... and select viewpagerindicator.
4-现在你应该可以导入 com.viewpagerindicator.TitlePageIndicator
。
4- Now you should be able to import com.viewpagerindicator.TitlePageIndicator
.
现在有关实现这一点没有使用片段:
Now about implementing this without using fragments:
在随样品的 viewpagerindicatior 的,你可以看到,该库正在与使用 ViewPager
具有 FragmentPagerAdapter
。
In the sample that comes with viewpagerindicatior, you can see that the library is being used with a ViewPager
which has a FragmentPagerAdapter
.
但事实上,库本身就是片段
独立。它只是需要一个 ViewPager
。
因此,只要使用的 PagerAdapter
代替 FragmentPagerAdapter
,你是好去。
But in fact the library itself is Fragment
independant. It just needs a ViewPager
.
So just use a PagerAdapter
instead of a FragmentPagerAdapter
and you're good to go.
这篇关于Android的观点与寻呼机页指示灯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!