您好我正在使用Android应用程序。我使用视图寻呼机创建了一个可滑动的视图,其中只包含右端的图像。现在,我想在每个视图的同一页面查看器中添加一些文本视图和按钮。如何添加文本视图和按钮以及页面查看器??? 这是我的代码
Swipe_adapter.java
public class Swipe_adapter extends PagerAdapter {
Context context;
private int[] GalImages = new int[] {
R.drawable.app1,
R.drawable.app2,
R.drawable.gosms
};
ArrayList<HashMap<String,String>> list=new ArrayList<HashMap<String, String>>();
Map map=new HashMap ();
String[] str=new String[]
{
"app1","app2","app3"
};
Swipe_adapter(Context context){
this.context=context;
}
@Override
public int getCount() {
return GalImages.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(context);
int padding = context.getResources().getDimensionPixelSize(R.dimen.padding_medium);
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.FIT_END);
imageView.setImageResource(GalImages[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
这是我的片段类
public class FeaturedFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_featured, container, false);
ViewPager viewPager = (ViewPager)rootView.findViewById(R.id.view_pager);
Swipe_adapter adapter = new Swipe_adapter(getActivity());
viewPager.setAdapter(adapter);
return rootView;
}
}
答案 0 :(得分:0)
您可以创建包含图像,按钮,文本的布局。然后在寻呼机适配器instantiateItem
中膨胀该布局mylayout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/tv_myText"
style="?android:textAppearanceMedium"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/imgMap"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="center" />
<Button
android:id="@+id/btnTopup"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
在PagerAdapter中使用
public Object instantiateItem(ViewGroup container, int position) {
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
View myView = inflater.inflate(R.layout.mylayout, null);
//Access yout textView, ImageView, Button like below
TextView myText = (TextView)myView.findViewById(R.id.tv_myText);
return myView;
}
context可以通过从调用活动传递给PagerAdapter构造函数来获得。
希望这有帮助。
答案 1 :(得分:0)
我在ViewPager
中使用Mike Ortiz的TouchImageView
(用于缩放功能)时遇到了同样的问题(我只能采用一个主视图,在这种情况下是扩展的{{1} }),并制定了一个简洁的解决方案。我想将一些动态文本添加到用户刷过的一系列地图中,添加“Map 3/13”等作为叠加层。我通过在ImageView
适配器之外实例化额外视图(在我的情况下为TextView
),然后在更新textView的viewPager对象中添加ViewPager
来解决这个问题。
首先,我设置XML以同时采用两种视图(onPageChangeListener
允许文本覆盖图像):
RelativeLayout
我的ViewPager适配器<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayoutPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<org.mypackagename.ExtendedViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true" />
<TextView
android:id="@+id/myTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/viewpager"
android:layout_alignLeft="@id/viewpager"
android:layout_alignRight="@id/viewpager"
android:layout_alignTop="@id/viewpager"
android:layout_gravity="right"
android:layout_weight="1.0"
android:gravity="right"
android:paddingRight="5dip"
android:paddingTop="5dp"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
</RelativeLayout>
方法仅处理TouchImageView视图:
instantiateItem
最后,在设置public View instantiateItem(ViewGroup container, int pos) {
TouchImageView img = new TouchImageView(container.getContext());
img.setImageDrawable((Drawable) imageHolder.get(pos));
container.addView(img, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
return img;
}
和TextView
视图后,我在onCreate方法中创建了监听器:
ViewPager
现在,当我浏览地图时,TextView也会更新。您无法在适配器vPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
private int currentPage;
@Override
public void onPageSelected(int pos) {
currentPage = pos;
StringBuilder sb = new StringBuilder();
sb.append("Map ");
sb.append((pos + 1));
sb.append("/");
sb.append(allMaps.size());
myTextView.setText(sb);
}
public final int getCurrentPage() {
return currentPage;
}
});
方法中执行此操作,因为位置似乎不会反映您通过滑动更新时所处的页面(不明白为什么!)...