gpt4 book ai didi

java - 重影效应 : Two views on top of each other. 为什么?

转载 作者:行者123 更新时间:2023-12-01 12:26:52 25 4
gpt4 key购买 nike

我希望能够单击按钮在多个 View 中向前和向后导航,以及在 View 之间向左或向右滑动。

因此我决定实现 ViewPager 以在多个 View 之间滑动。

这是我的代码:

布局 XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white"
>

<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/viewPager"/>

<ImageView
android:id="@+id/apple"
android:layout_width="200sp"
android:layout_height="150sp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/apple"
android:contentDescription="apple"/>

<TextView
android:id="@+id/number"
android:layout_width="100sp"
android:layout_height="55sp"
android:layout_marginTop="47dp"
android:layout_below="@+id/apple" android:layout_alignStart="@+id/apple"/>

<Button
android:id="@+id/save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/save"
android:layout_alignTop="@+id/ignore" android:layout_toStartOf="@+id/apple"/>

<Button
android:id="@+id/ignore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Ignore"
android:layout_alignParentBottom="true" android:layout_toEndOf="@+id/apple"/>

<ImageView
android:id="@+id/back_nav_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_action_back"
android:contentDescription="back">
</ImageView>


<ImageView
android:id="@+id/forward_nav_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_action_forward"
android:layout_alignParentTop="true" android:layout_alignParentEnd="true"
android:contentDescription="forward">

</ImageView>

</RelativeLayout>

这是我的 Activity :

public class CollectionPager extends Activity {

private PagerAdapter pagerAdapter;
ActionBar actionbar;
MyAdapter myAdapter;
private Context context;
private TextView textView;
private int currentPage;
ViewPager viewPager;
int progressChanged = 0;

public static final String TAG = "CollectionPager";

public CollectionPager() {
context = this;
}

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

super.setContentView(R.layout.collection);
viewPager = (ViewPager) findViewById(R.id.viewPager);

myAdapter = new MyAdapter();
viewPager.setAdapter(myAdapter);

ActionBar actionBar = getActionBar();
if (actionBar != null) {
actionBar.hide();
}

//Initialize the back button and add an onClick event listener to the button
final ImageView back_button = (ImageView) findViewById(R.id.back_nav_arrow);
back_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {

//it doesn't matter if you're already in the first item
viewPager.setCurrentItem(viewPager.getCurrentItem() - 1);
}
});

//Initialize the forward button and add an onClick event listener to the button
final ImageView forward_button = (ImageView) findViewById(R.id.forward_nav_arrow);
forward_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {

//it doesn't matter if you're already in the last item
viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);
}
});

final Button save_button = (Button) findViewById(R.id.save);
save_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {

//save
}
});

final Button ignore_button = (Button) findViewById(R.id.ignore);
ignore_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {

//ignore

}
});

//Attach the page change listener inside the activity
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

// This method will be invoked when the current page is scrolled
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

}

//This method will be invoked when a new page becomes selected
@Override
public void onPageSelected(int position) {
//get position
currentPage = position;

}

// Called when the scroll state changes:
// SCROLL_STATE_IDLE, SCROLL_STATE_DRAGGING, SCROLL_STATE_SETTLING
@Override
public void onPageScrollStateChanged(int i) {

//get state

}
});

}

private class MyAdapter extends PagerAdapter {


int NumberOfPages = 10;

LayoutInflater inflater = (LayoutInflater) CollectionPager.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

@Override
public int getCount() {
return NumberOfPages;
}

@Override
public Object instantiateItem(ViewGroup parent, int position) {

View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.collection, parent, false);

ImageView imageView = (ImageView) view
.findViewById(R.id.apple);
imageView.setImageResource(R.drawable.apple);
parent.addView(view,0);


return view;
}

@Override
public void destroyItem(ViewGroup parent, int position, Object object) {
((ViewPager) parent).removeView((View) object);
}

@Override
public boolean isViewFromObject(View parent, Object object) {
return parent== ((View) object);
}

@Override
public Parcelable saveState() {
return null;
}
}
}

检测到 onClickEvent,但下面是 View 所发生情况的屏幕截图。两个 View 彼此重叠。一个 View 固定在屏幕上,另一个 View 正确滚动。

enter image description here

我不知道为什么会发生这种情况。是什么导致我的代码中出现这种情况?

编辑:这是突出显示该问题的视频:https://www.dropbox.com/s/6x5qa16xyttzrwa/VIDEO0041.mp4?dl=0

最佳答案

改变

   @Override
public boolean isViewFromObject(View parent, Object object) {
return parent== ((View) object);
}

@Override
public boolean isViewFromObject(View v, Object o) {
return parent == object;
}

更新:

看完电影后,您的问题是您在 View 页面顶部放置了一些静态楔 block ,因此将其删除,这意味着您的布局将变成这样:

这是您的 main_activity.xml,您必须通过函数 setContentView 将其分配给您的 activity

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white"
>

<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/viewPager"/>

</RelativeLayout>

然后在 instantiateItem 使用以下布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white"
>

<ImageView
android:id="@+id/apple"
android:layout_width="200sp"
android:layout_height="150sp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/apple"
android:contentDescription="apple"/>

<TextView
android:id="@+id/number"
android:layout_width="100sp"
android:layout_height="55sp"
android:layout_marginTop="47dp"
android:layout_below="@+id/apple" android:layout_alignStart="@+id/apple"/>

<Button
android:id="@+id/save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/save"
android:layout_alignTop="@+id/ignore" android:layout_toStartOf="@+id/apple"/>

<Button
android:id="@+id/ignore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Ignore"
android:layout_alignParentBottom="true" android:layout_toEndOf="@+id/apple"/>

<ImageView
android:id="@+id/back_nav_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_action_back"
android:contentDescription="back">
</ImageView>


<ImageView
android:id="@+id/forward_nav_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_action_forward"
android:layout_alignParentTop="true" android:layout_alignParentEnd="true"
android:contentDescription="forward">

</ImageView>

您的主要 Activity :

 protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

super.setContentView(R.layout.collection);
viewPager = (ViewPager) findViewById(R.id.viewPager);

myAdapter = new MyAdapter();
viewPager.setAdapter(myAdapter);

ActionBar actionBar = getActionBar();
if (actionBar != null) {
actionBar.hide();
}

//Attach the page change listener inside the activity
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

// This method will be invoked when the current page is scrolled
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

}

//This method will be invoked when a new page becomes selected
@Override
public void onPageSelected(int position) {
//get position
currentPage = position;

}

// Called when the scroll state changes:
// SCROLL_STATE_IDLE, SCROLL_STATE_DRAGGING, SCROLL_STATE_SETTLING
@Override
public void onPageScrollStateChanged(int i) {

//get state

}
});

}

然后在此函数中分配主要 Activity 的点击监听器

        @Override
public Object instantiateItem(ViewGroup parent, int position) {

View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.collection, parent, false);

ImageView imageView = (ImageView) view
.findViewById(R.id.apple);
imageView.setImageResource(R.drawable.apple);

final ImageView back_button = (ImageView) view.findViewById(R.id.back_nav_arrow);
back_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {

//it doesn't matter if you're already in the first item
viewPager.setCurrentItem(viewPager.getCurrentItem() - 1);
}
});



parent.addView(view,0);


return view;
}

关于java - 重影效应 : Two views on top of each other. 为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26262806/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com