我正在尝试更改自定义视图组的ImageView背景,同时在视图寻呼机中滑动但不起作用,ImageView的背景无法刷新。我不知道为什么。当我想在自定义视图组中更改ImageView的背景时,会出现此问题。在活动中,我有下一个代码:
公共类Example5扩展了AppCompatActivity {
private ViewPager view_pager;
private CustomPagerAdapter pagerAdapter;
private CustomViewGroup customViewGroup
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_example5);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
view_pager = findViewById(R.id.view_pager);
view_pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
The problem here when I want to change the background of ImageView inside CustomViewGroup
customViewGroupOneImage.changeImage();
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
pagerAdapter = new CustomPagerAdapter(this);
view_pager.setAdapter(pagerAdapter);
customViewGroup = new CustomViewGroup(this);
}
}
我的自定义视图组
public class CustomViewGroup extends ViewGroup {
private Context context;
public CustomViewGroupOneImage(Context context) {
super(context);
init(context);
}
public CustomViewGroupOneImage(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public CustomViewGroupOneImage(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int width = getWidth();
int height = getHeight();
View child = getChildAt(0);
measureChild(child, width, height);
child.layout(0, 0, child.getMeasuredWidth(), child.getMeasuredHeight());
}
public void changeImage(){
ImageView child = (ImageView) getChildAt(0);
child.setBackgroundResource(R.drawable.color2);
}
private void init(Context context){
this.context = context;
ImageView imageView = new ImageView(context);
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
imageView.setLayoutParams(params);
imageView.setBackgroundResource(R.drawable.color1);
addView(imageView);
}
}
我的活动布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.wolf.examplecolor.ExampleViewPager.Example5"
tools:showIn="@layout/activity_example5">
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="200dp"/>
<com.example.wolf.examplecolor.ExampleViewPager.Example5.CustomViewGroup
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_below="@id/view_pager"/>
</RelativeLayout>
但是,如果我有下一个布局,则在滑动视图寻呼机改变ImageView的背景时这是有效的。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.wolf.examplecolor.ExampleViewPager.Example5"
tools:showIn="@layout/activity_example5">
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="200dp"/>
<ImageView
android:id="@+id/img_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/view_pager" />
</RelativeLayout>
后来改变了viewpager的监听器
public class Example5 extends AppCompatActivity {
private ViewPager view_pager;
private ImageView img_image;
private CustomPagerAdapter pagerAdapter;
private CustomViewGroup customViewGroup
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_example5);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
view_pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
img_image.setBackgroundResource(R.drawable.color2);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
img_image = findViewById(R.id.img_image);
img_image.setBackgroundResource(R.drawable.color1);
}
}
由于
Greetting。
答案 0 :(得分:0)
MainActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the content of the activity to use the activity_main.xml layout file
setContentView(R.layout.activity_main);
// Find the view pager that will allow the user to swipe between fragments
ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
// Create an adapter that knows which fragment should be shown on each page
SimpleFragmentPagerAdapter adapter = new SimpleFragmentPagerAdapter(getSupportFragmentManager());
// Set the adapter onto the view pager
viewPager.setAdapter(adapter);
}
MainActivity.xml
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/viewPager"
/>
Java Class, Image1.java
public View onCreateView(LayoutInflater layoutInflater,ViewGroup container,Bundle savedInstanceState){
return layoutInflater.inflate(R.layout.image1,container,false);
image1.xml
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/green"
android:scaleType="centerCrop"/>
Try with more java class similar to Image1.java class and xml of image1.