Android 按钮。setEnabled(false)不';无法在ViewPager中工作
好吧,也许我不明白,但我不能禁用ViewPager视图中的按钮, 下面是一个示例,我尝试禁用该按钮: ViewPagerAdapter:Android 按钮。setEnabled(false)不';无法在ViewPager中工作,android,android-viewpager,android-button,Android,Android Viewpager,Android Button,好吧,也许我不明白,但我不能禁用ViewPager视图中的按钮, 下面是一个示例,我尝试禁用该按钮: ViewPagerAdapter: @Override public Object instantiateItem(final ViewGroup container, int position) { if(views.size()>position&&views.get(position)!=null) {
@Override
public Object instantiateItem(final ViewGroup container, int position) {
if(views.size()>position&&views.get(position)!=null)
{
currentView=views.get(position);
}
else {
currentView = (ViewGroup) inflater.inflate(R.layout.custom_row_cardbase_viewpager, container, false);
currentView.setVisibility(View.VISIBLE);
....
Button saveCard = (Button)currentView.findViewById(R.id.save_card_button);
if(textColor == Color.BLACK){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
saveCard.setTextColor(mActivity.getResources().getColorStateList(R.color.button_color_black,mActivity.getTheme()));
}else{
saveCard.setTextColor(mActivity.getResources().getColorStateList(R.color.button_color_black));
}
}else{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
saveCard.setTextColor(mActivity.getResources().getColorStateList(R.color.button_color_white, mActivity.getTheme()));
}else{
saveCard.setTextColor(mActivity.getResources().getColorStateList(R.color.button_color_white));
}
}
if(db.checkIfCardAdded(pagerItems.get(position).getCardID())){
Log.e("Card already added",pagerItems.get(position).getCardName());
saveCard.setEnabled(false);
}
}
((ViewPager)container).addView(currentView);
return currentView;
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="200dp"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:id="@+id/card_back_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
card_view:cardCornerRadius="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp">
<Button
android:id="@+id/save_card_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Save"/>
<ImageView
android:layout_centerHorizontal="true"
android:id="@+id/card_back_logo"
android:layout_width="50dp"
android:layout_height="50dp"/>
</RelativeLayout>
<LinearLayout
android:id="@+id/test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_marginLeft="7dp"
android:layout_marginRight="7dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@color/white">
<ImageView
android:id="@+id/barcode"
android:layout_width="match_parent"
android:layout_height="75dp"
android:layout_marginTop="7dp"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:layout_gravity="center"/>
<TextView
android:paddingTop="3dp"
android:paddingBottom="1dp"
android:id="@+id/cardCode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/barcode_card_base"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:gravity="center_horizontal"
android:textColor="@color/black"
android:textStyle="bold" />
</RelativeLayout>
<TextView
android:id="@+id/cardHolder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:textColor="@color/colorPrimaryDark"
android:textSize="12sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
为什么禁用不起作用?如何正确禁用按钮?
“If”代码运行良好。此外,我还试图禁用按钮没有“如果”,但这并没有意义。
有人能解释一下我的问题吗
谢谢
编辑:
@Override
public Object instantiateItem(final ViewGroup container, int position) {
if(views.size()>position&&views.get(position)!=null)
{
currentView=views.get(position);
}
else {
currentView = (ViewGroup) inflater.inflate(R.layout.custom_row_cardbase_viewpager, container, false);
currentView.setVisibility(View.VISIBLE);
....
Button saveCard = (Button)currentView.findViewById(R.id.save_card_button);
if(textColor == Color.BLACK){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
saveCard.setTextColor(mActivity.getResources().getColorStateList(R.color.button_color_black,mActivity.getTheme()));
}else{
saveCard.setTextColor(mActivity.getResources().getColorStateList(R.color.button_color_black));
}
}else{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
saveCard.setTextColor(mActivity.getResources().getColorStateList(R.color.button_color_white, mActivity.getTheme()));
}else{
saveCard.setTextColor(mActivity.getResources().getColorStateList(R.color.button_color_white));
}
}
if(db.checkIfCardAdded(pagerItems.get(position).getCardID())){
Log.e("Card already added",pagerItems.get(position).getCardName());
saveCard.setEnabled(false);
}
}
((ViewPager)container).addView(currentView);
return currentView;
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="200dp"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:id="@+id/card_back_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
card_view:cardCornerRadius="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp">
<Button
android:id="@+id/save_card_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Save"/>
<ImageView
android:layout_centerHorizontal="true"
android:id="@+id/card_back_logo"
android:layout_width="50dp"
android:layout_height="50dp"/>
</RelativeLayout>
<LinearLayout
android:id="@+id/test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_marginLeft="7dp"
android:layout_marginRight="7dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@color/white">
<ImageView
android:id="@+id/barcode"
android:layout_width="match_parent"
android:layout_height="75dp"
android:layout_marginTop="7dp"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:layout_gravity="center"/>
<TextView
android:paddingTop="3dp"
android:paddingBottom="1dp"
android:id="@+id/cardCode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/barcode_card_base"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:gravity="center_horizontal"
android:textColor="@color/black"
android:textStyle="bold" />
</RelativeLayout>
<TextView
android:id="@+id/cardHolder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:textColor="@color/colorPrimaryDark"
android:textSize="12sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
您需要在代码的其他部分设置enable(true)。这肯定会奏效的
if (db.checkIfCardAdded(pagerItems.get(position).getCardID())) {
Log.e("Card already added", pagerItems.get(position).getCardName());
saveCard.setEnabled(false);
} else {
saveCard.setEnabled(true);
}
任何问题都让我知道 为什么您的
视图组容器是最终的?@MuchOverflow,因为我需要在AnimationListener的onClick事件中使用它。您修复了它吗@奥洛洛金。酷,按钮是否应根据条件启用或禁用,并应用于列表中的所有项目?正确吗?@RaguSwaminathan按钮应首先启用,当viewpager适配器为适配器的每个视图创建时,我检查它是否存在于数据库(Sqlite)中,如果应禁用“是”按钮,则您提供的代码将与我的代码一样工作,我还发现一个问题,当我在xml布局中禁用按钮时,它不起作用。如果不在xml中使用禁用按钮,请尝试通过编程控制它