如何隐藏适配器中的按钮视图?
How to Hide view of a button in Adapter?
我需要对两个不同的 Fragment 使用相同的列表视图及其项目并进行一些修改。
这里(截图),(需要在不同的片段中使用)
- 我需要在我的第一个布局和第一个片段中显示添加到愿望清单并删除
- 我需要隐藏我的第二个布局和第二个片段
中的“添加到愿望清单”和“删除”
我需要使用相同的适配器。
如何操作?
更新
cart_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical">
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/cardlist_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="12dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="@drawable/curved_shape"
android:orientation="vertical"
card_view:cardElevation="2dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:orientation="horizontal">
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_margin="14dp"
android:layout_marginRight="14dp"
android:src="@drawable/image_4" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="12dp"
android:orientation="vertical">
<TextView
android:id="@+id/cartProduct"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Fusce veh"
android:textColor="#9F9F9F" />
<TextView
android:id="@+id/cartQuantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="100 g"
android:textColor="#9F9F9F" />
<LinearLayout
android:layout_width="225dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="horizontal"
android:weightSum="1">
<TextView
android:id="@+id/symbol"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Rs "
android:textColor="#9F9F9F" />
<TextView
android:id="@+id/cartPrice"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_marginTop="8dp"
android:layout_weight="0.45"
android:text="19.99"
android:textColor="#9F9F9F" />
<TextView
android:id="@+id/cartPriceDum"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_marginTop="8dp"
android:layout_weight="0.45"
android:text="19.99"
android:textColor="#9F9F9F"
android:visibility="gone" />
<ImageView
android:id="@+id/ivDecrease"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="0.1"
android:focusable="false"
android:focusableInTouchMode="false"
android:src="@drawable/ic_action_expand" />
<TextView
android:id="@+id/cartCount"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="1dp"
android:layout_marginRight="1dp"
android:layout_weight="0.2"
android:gravity="center"
android:text="1"
android:textSize="15dp"
android:textStyle="bold" />
<ImageView
android:id="@+id/ivIncrease"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.1"
android:focusable="false"
android:focusableInTouchMode="false"
android:src="@drawable/ic_action_collapse" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dip"
android:background="#E4E4E4" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#F8F8F8"
android:descendantFocusability="blocksDescendants"
android:orientation="horizontal"
android:weightSum="1">
<Button
android:id="@+id/addTowish"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:background="#F8F8F8"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="ADD TO WISHLIST"
android:textColor="#787878" />
<View
android:layout_width="1dip"
android:layout_height="match_parent"
android:background="#E4E4E4" />
<Button
android:id="@+id/remove"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:background="#F8F8F8"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="REMOVE"
android:textColor="#787878" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
使用条件基于您必须在适配器中使用的条件:
holder.textView1.setVisibility(View.VISIBLE);
holder.textView2.setVisibility(View.GONE);
检查:
当您在交易中添加片段时,您应该使用标签。
fragTrans.replace(android.R.id.content, myFragment, "MY_FRAGMENT");
稍后如果您想检查片段是否可见:
MyFragment myFragment = (MyFragment)getFragmentManager().findFragmentByTag("MY_FRAGMENT");
if (myFragment != null && myFragment.isVisible()) {
// add your code here
}
一旦获得当前片段,就可以从调用片段适配器 class 的片段中获取。
试试这个
将您的购物车商品放在不同的数组列表中,并在您的购物车中显示该数组列表。然后当点击 Remove 然后从 arralist 中删除该项目并调用 notifyDataSetChanged()。在适配器上。
希望能帮助到你。谢谢
向自定义适配器 class 添加构造函数,它将片段名称作为字符串,以便您可以检查适配器 class 是从哪个片段
调用的
在适配器中添加构造函数为:
String fragmentName;
public CustomAdapter(String fragmentName) {
super();
this.fragmentName = fragmentName;
}
然后在 getView() 方法中使用它:
if(fragmentName.equals("Fragment1"))
{
addTowish.setVisibility(View.VISIBLE);
}
else
{
addTowish.setVisibility(View.GONE);
}
我需要对两个不同的 Fragment 使用相同的列表视图及其项目并进行一些修改。
这里(截图),(需要在不同的片段中使用)
- 我需要在我的第一个布局和第一个片段中显示添加到愿望清单并删除
- 我需要隐藏我的第二个布局和第二个片段 中的“添加到愿望清单”和“删除”
我需要使用相同的适配器。
如何操作?
更新
cart_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical">
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/cardlist_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="12dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="@drawable/curved_shape"
android:orientation="vertical"
card_view:cardElevation="2dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:orientation="horizontal">
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_margin="14dp"
android:layout_marginRight="14dp"
android:src="@drawable/image_4" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="12dp"
android:orientation="vertical">
<TextView
android:id="@+id/cartProduct"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Fusce veh"
android:textColor="#9F9F9F" />
<TextView
android:id="@+id/cartQuantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="100 g"
android:textColor="#9F9F9F" />
<LinearLayout
android:layout_width="225dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="horizontal"
android:weightSum="1">
<TextView
android:id="@+id/symbol"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Rs "
android:textColor="#9F9F9F" />
<TextView
android:id="@+id/cartPrice"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_marginTop="8dp"
android:layout_weight="0.45"
android:text="19.99"
android:textColor="#9F9F9F" />
<TextView
android:id="@+id/cartPriceDum"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_marginTop="8dp"
android:layout_weight="0.45"
android:text="19.99"
android:textColor="#9F9F9F"
android:visibility="gone" />
<ImageView
android:id="@+id/ivDecrease"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="0.1"
android:focusable="false"
android:focusableInTouchMode="false"
android:src="@drawable/ic_action_expand" />
<TextView
android:id="@+id/cartCount"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="1dp"
android:layout_marginRight="1dp"
android:layout_weight="0.2"
android:gravity="center"
android:text="1"
android:textSize="15dp"
android:textStyle="bold" />
<ImageView
android:id="@+id/ivIncrease"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.1"
android:focusable="false"
android:focusableInTouchMode="false"
android:src="@drawable/ic_action_collapse" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dip"
android:background="#E4E4E4" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#F8F8F8"
android:descendantFocusability="blocksDescendants"
android:orientation="horizontal"
android:weightSum="1">
<Button
android:id="@+id/addTowish"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:background="#F8F8F8"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="ADD TO WISHLIST"
android:textColor="#787878" />
<View
android:layout_width="1dip"
android:layout_height="match_parent"
android:background="#E4E4E4" />
<Button
android:id="@+id/remove"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:background="#F8F8F8"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="REMOVE"
android:textColor="#787878" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
使用条件基于您必须在适配器中使用的条件:
holder.textView1.setVisibility(View.VISIBLE);
holder.textView2.setVisibility(View.GONE);
检查:
当您在交易中添加片段时,您应该使用标签。
fragTrans.replace(android.R.id.content, myFragment, "MY_FRAGMENT");
稍后如果您想检查片段是否可见:
MyFragment myFragment = (MyFragment)getFragmentManager().findFragmentByTag("MY_FRAGMENT");
if (myFragment != null && myFragment.isVisible()) {
// add your code here
}
一旦获得当前片段,就可以从调用片段适配器 class 的片段中获取。
试试这个
将您的购物车商品放在不同的数组列表中,并在您的购物车中显示该数组列表。然后当点击 Remove 然后从 arralist 中删除该项目并调用 notifyDataSetChanged()。在适配器上。
希望能帮助到你。谢谢
向自定义适配器 class 添加构造函数,它将片段名称作为字符串,以便您可以检查适配器 class 是从哪个片段
调用的在适配器中添加构造函数为:
String fragmentName;
public CustomAdapter(String fragmentName) {
super();
this.fragmentName = fragmentName;
}
然后在 getView() 方法中使用它:
if(fragmentName.equals("Fragment1"))
{
addTowish.setVisibility(View.VISIBLE);
}
else
{
addTowish.setVisibility(View.GONE);
}