Android 更改ImageView可绘制内部ViewPager
我有一个碎片,上面有一个。我想通过片段更改ViewPager内部布局的图像 为此,我编写了以下代码:Android 更改ImageView可绘制内部ViewPager,android,android-layout,android-fragments,android-viewpager,Android,Android Layout,Android Fragments,Android Viewpager,我有一个碎片,上面有一个。我想通过片段更改ViewPager内部布局的图像 为此,我编写了以下代码: public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { v = inflater.inflate(R.layout.fragment_tab,container,false); //fragment layout imageLayout
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
v = inflater.inflate(R.layout.fragment_tab,container,false); //fragment layout
imageLayout = inflater.inflate(R.layout.view_image, null, false); //layout that's used in viewPager
theImage = (ImageView) imageLayout .findViewById(R.id.image); //getting the ImageView
ViewPager viewPager = (ViewPager) v.findViewById(R.id.resultImagesPager);
viewPager.setAdapter(new ResultImageAdapter(getActivity()));
}
代码本身不会使我的应用程序崩溃,但无论我写什么,我都无法更改ImageView的位图。在这一行中,您只需创建R.layout.view\u image的新实例,并且创建的视图不会添加到任何容器中
imageLayout = inflater.inflate(R.layout.view_image, null, false);
您应该更改可视寻呼机适配器的内部可绘制部分。
在这种情况下,您的适配器可能看起来像:
private List<YourObject> mList;
// pass list of items into constructor
public CustomPagerAdapter(Context context, List<YourObject> list) {
mContext = context;
mList = list;
}
@Override
public Object instantiateItem(ViewGroup collection, int position) {
CustomPagerEnum customPagerEnum = CustomPagerEnum.values()[position];
LayoutInflater inflater = LayoutInflater.from(mContext);
ViewGroup layout = (ViewGroup) inflater.inflate(customPagerEnum.getLayoutResId(), collection, false);
// get corresponding object and retrieve needed drawable
YourObject yourObject = mList.get(position);
ImageView theImage = (ImageView) layout.findViewById(R.id.image);
theImage.setImageDrawable(yourObject.getYourDravwable());
collection.addView(layout);
return layout;
}
@Override
public int getCount() {
return mList.size();
}
私有列表mList;
//将项目列表传递给构造函数
公共CustomPagerAdapter(上下文,列表){
mContext=上下文;
mList=列表;
}
@凌驾
公共对象实例化项(视图组集合,int位置){
CustomPagerEnum CustomPagerEnum=CustomPagerEnum.values()[position];
LayoutFlater充气机=LayoutFlater.from(mContext);
视图组布局=(视图组)充气器。充气(customPagerEnum.getLayoutResId(),集合,false);
//获取相应的对象并检索所需的可绘制对象
YourObject YourObject=mList.get(位置);
ImageView theImageView=(ImageView)layout.findViewById(R.id.image);
setImageDrawable(yourObject.getYourDrawable());
collection.addView(布局);
返回布局;
}
@凌驾
public int getCount(){
返回mList.size();
}
在你的片段中:
List<YourObject> list = new ArrayList<>();
// add your objects or whatever
list.add(new YourObject(name, drawable);
viewPager.setAdapter(new ResultImageAdapter(getActivity(), list));
List List=new ArrayList();
//添加您的对象或其他内容
添加(新对象(名称,可绘制);
setAdapter(新的ResultImageAdapter(getActivity(),list));
那么,你是说我在错误的地方写了代码?我的意思是,现在,我的代码应该是这样的:ViewPager包含view\u image.xml
with default image->Fragment.java访问view\u image.xml
并更改其中的图像->ViewPager现在有view\u image.xml
with changed image.v=inflater.inflate(R.layout.fragment_选项卡,container,false);在此充气器上放置container(第二个参数),当充气器将从xml创建布局时,它会将此布局添加到容器中,但您创建的imageLayout没有任何容器,此布局仅在内存中创建,但未显示在屏幕上try:theImageView=(ImageView)v.findViewById(R.id.image);这是行不通的,因为R.id.image
是在view\u image.xml
中声明的,而不是fragment\u tab.xml
,但是正如Marlen所写的,您在这里没有膨胀ViewPager。您在哪里膨胀它?我在片段中膨胀它-我将行添加到代码片段中。然后更改适配器内部的图像