我试图制作一个图像滑块,但是在模拟器上运行时没有出现imageview,我尝试了另一个项目,它运行得很好。我尝试更改imageView的id,它似乎工作正常,但是当我运行应用程序时没有显示任何内容。在底部有我的xml文件
适配器类:
public class CustomSwipeAdapter extends PagerAdapter {
private int[] image_resources = {R.drawable.mario, R.drawable.dice, R.drawable.camera};
private Context ctx;
private LayoutInflater layoutInflater;
public CustomSwipeAdapter(Context ctx){
this.ctx =ctx;
}
@Override
public int getCount() {
return ScrollingActivity.image.size();
}
@Override
public boolean isViewFromObject(View view, Object o) {
return (view ==(NestedScrollView)o);
}
@Override
public Object instantiateItem(ViewGroup container, int position){
layoutInflater = (LayoutInflater)ctx.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
View item_view = layoutInflater.inflate( R.layout.swipe_layout, container, false );
ImageView imageView = (ImageView) item_view.findViewById( R.id.img);
imageView.setImageResource( image_resources[position] );
container.addView( item_view );
return item_view;
}
@Override
public void destroyItem (ViewGroup container, int position, Object object){
container.removeView( (NestedScrollView)object );
}
}
活动类:
public class ItemsDetailsActivity extends AppCompatActivity {
ViewPager viewPager;
CustomSwipeAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_items_details );
viewPager = findViewById( R.id.view_pager );
adapter = new CustomSwipeAdapter( this );
viewPager.setAdapter( adapter );
}
}
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_height="match_parent">
<LinearLayout
android:id="@+id/testLay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/img"
android:layout_width="match_parent"
android:layout_height="276dp"
android:layout_marginTop="25dp"
android:layout_weight="1"
android:visibility="visible" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>