27 android activity-indicator android-view android-viewpager viewpagerindicator
我需要在视图分页器文件中获取包含图像的页面指示符.这是我的代码.
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 );
}
}
Run Code Online (Sandbox Code Playgroud)
在这段代码中,我得到一个错误 TitlePageIndicator indicator = (TitlePageIndicator)findViewById(R.id.indicat);的TitlePageIndicator cannot be resolved to a type.这是什么错误.我该如何解决?
这是我的xml代码:
<?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>
Run Code Online (Sandbox Code Playgroud)
我需要写什么代码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);
}
}
Run Code Online (Sandbox Code Playgroud)
我想做除了这堂课以外的其他事吗?
在此先感谢您的帮助
vuh*_*990 53
更新: 2017年3月22日
主片段布局:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<RadioGroup
android:id="@+id/page_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|bottom"
android:layout_marginBottom="@dimen/margin_help_container"
android:orientation="horizontal">
<RadioButton
android:id="@+id/page1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true" />
<RadioButton
android:id="@+id/page2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:id="@+id/page3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RadioGroup>
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)
像这样在你的片段上设置视图和事件:
mViewPaper = (ViewPager) view.findViewById(R.id.viewpager);
mViewPaper.setAdapter(adapder);
mPageGroup = (RadioGroup) view.findViewById(R.id.page_group);
mPageGroup.setOnCheckedChangeListener(this);
mViewPaper.addOnPageChangeListener(this);
*************************************************
*************************************************
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
// when current page change -> update radio button state
int radioButtonId = mPageGroup.getChildAt(position).getId();
mPageGroup.check(radioButtonId);
}
@Override
public void onPageScrollStateChanged(int state) {
}
@Override
public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
// when checked radio button -> update current page
RadioButton checkedRadioButton = (RadioButton)radioGroup.findViewById(checkedId);
// get index of checked radio button
int index = radioGroup.indexOfChild(checkedRadioButton);
// update current page
mViewPaper.setCurrentItem(index), true);
}
Run Code Online (Sandbox Code Playgroud)
自定义复选框状态:自定义复选框图像android
Viewpager教程:http://architects.dzone.com/articles/android-tutorial-using

Ben*_*oli 12
以下是您需要做的一些事情:
1 - 如果您还没有这样做,请下载该库.
2-导入Eclipse.
3-设置项目以使用库:项目 - >属性 - > Android - >向下滚动到库部分,单击添加...并选择viewpagerindicator.
4-现在你应该可以导入了com.viewpagerindicator.TitlePageIndicator.
现在关于实现这个而不使用片段:
在viewpagerindicatior附带的示例中,您可以看到该库正在与一个ViewPager具有的库一起使用FragmentPagerAdapter.
但实际上图书馆本身是Fragment独立的.它只需要一个ViewPager.因此,只需使用一个PagerAdapter而不是一个FragmentPagerAdapter,你很高兴.
我知道这已经得到了解答,但对于任何寻找ViewPager指标简单,简洁实现的人来说,我已经实现了一个我开源的实现.对于任何发现Jake Wharton版本有点复杂的人来说,请查看 https://github.com/jarrodrobins/SimpleViewPagerIndicator.
小智 5
我还使用了@JROD的SimpleViewPagerIndicator.它也像@manuelJ所描述的那样崩溃.
根据他的文件:
SimpleViewPagerIndicator pageIndicator = (SimpleViewPagerIndicator) findViewById(R.id.page_indicator);
pageIndicator.setViewPager(pager);
Run Code Online (Sandbox Code Playgroud)
确保你也添加这一行:
pageIndicator.notifyDataSetChanged();
Run Code Online (Sandbox Code Playgroud)
它与数组超出范围异常崩溃,因为SimpleViewPagerIndicator未正确实例化且项目为空.调用notifyDataSetChanged会导致正确设置所有值,或者正确地重置.
| 归档时间: |
|
| 查看次数: |
95606 次 |
| 最近记录: |