问题描述
我需要在带有图像的视图寻呼机文件中获取页面指示器.这是我的代码.
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 );
}
}
在此代码中,我在 TitlePageIndicator indicator = (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 代码:
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>
TitlePageIndicator
需要写什么代码?我想在不使用片段的情况下做到这一点.
我还创建了一个类如:
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?
提前感谢您的帮助
推荐答案
你需要做以下几件事:
1-如果您还没有这样做,请下载 库.
1-Download the library if you haven't already done that.
2- 导入 Eclipse.
2- Import into Eclipse.
3- 设置您的项目以使用该库:Project-> Properties -> Android -> 向下滚动到 Library 部分,单击 Add... 并选择 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 附带的示例中,您可以看到该库正在与具有 FragmentPagerAdapter
的 ViewPager
一起使用.
In the sample that comes with viewpagerindicatior, you can see that the library is being used with a ViewPager
which has a FragmentPagerAdapter
.
但实际上库本身是 Fragment
独立的.它只需要一个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 视图寻呼机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!