【发布时间】:2016-05-30 20:24:52
【问题描述】:
好的,可能是我不明白某些内容,但我无法禁用 ViewPager 视图中的按钮, 这是一个示例,我尝试禁用按钮:
ViewPagerAdapter:
@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;
}
为什么禁用不起作用?我应该如何正确禁用按钮? “如果”代码运行良好。我也尝试禁用没有 "if" 的按钮,但这没有任何意义。 谁能解释一下我的问题?
谢谢。
编辑:
<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>
【问题讨论】:
-
为什么你的
ViewGroup container是最终的? -
@MuchOverflow 因为我需要在animationListener的onClick事件中使用它
-
你修好了吗? ? @Oloking
-
哦。很酷,是否应该根据条件启用或禁用按钮,并且它适用于列表中的所有项目?正确吗?
-
@RaguSwaminathan 按钮应首先启用,当 viewpager 适配器为适配器的每个视图创建时,我检查它是否存在于数据库(Sqlite)中,如果是,则应禁用按钮
标签: android android-viewpager android-button