在运行时单击更改膨胀视图

时间:2014-01-16 12:44:50

标签: java android xml android-layout layout-inflater

我有一个gridView,其元素由textView,imageView和button组成。每当用户按下按钮时,不可见的linearLayout应该与editText一起出现,而另一个Button则在旧布局的顶部出现。我尝试过这样做,但它总是说我无法在点击时修改膨胀的元素。有什么建议?以下是我的代码示例:custom_product_list_square.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="match_parent"
    android:background="@drawable/item_border"
    android:descendantFocusability="blocksDescendants"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:paddingTop="5dp" >

    <LinearLayout
        android:id="@+id/icon_thumbnail_sq_list"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" >

        <ImageView
            android:id="@+id/product_image_icon_sq_list"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:contentDescription="@string/product_image"
            android:src="@drawable/cake1" />
    </LinearLayout>

    <TextView
        android:id="@+id/product_name_sq_list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/icon_thumbnail_sq_list"
        android:layout_centerHorizontal="true"
        android:gravity="center"
        android:lines="3"
        android:text="text1"
        android:textColor="#040404"
        android:textSize="12sp"
        android:textStyle="bold"
        android:typeface="sans" />

    <TextView
        android:id="@+id/product_price_sq_list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/product_name_sq_list"
        android:layout_below="@+id/product_name_sq_list"
        android:gravity="right"
        android:text="text2"
        android:textColor="@color/black2"
        android:visibility="visible" />

    <Button
        android:id="@+id/order_btn"
        android:layout_width="16dp"
        android:layout_height="16dp"
        android:layout_alignLeft="@+id/product_price_sq_list"
        android:layout_alignTop="@+id/product_price_sq_list"
        android:layout_marginBottom="5dp"
        android:background="@drawable/plus" />

    <LinearLayout
        android:id="@+id/order_now"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/product_price_sq_list"
        android:layout_alignParentLeft="true"
        android:background="@android:color/white"
        android:orientation="vertical"
        android:visibility="gone" >

        <EditText
            android:id="@+id/product_count"
            android:layout_width="64dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:ems="10" />

        <Button
            android:id="@+id/insert_btn"
            android:layout_width="40dp"
            android:layout_height="20dp"
            android:layout_gravity="center_horizontal"
            android:background="@drawable/button_animated"
            android:text="insert"
            android:textSize="10sp" />
    </LinearLayout>

</RelativeLayout>

和继承人活动代码:

    final class RowHolder {
    ImageView productIcon;
    TextView productName;
    TextView productDescription;
    TextView productPrice;
    Button productInsertNow;
    Button addProduct;
    LinearLayout ln;
    EditText productCount;
}
class ProductListAdapterSquare extends BaseAdapter implements IUpdateGridView{

    private final Context context;
    private final List<ProductList> productList;
    private final LayoutInflater inflater;
    private final String imageDir;

    public ProductListAdapterSquare(Context context, List<ProductList>list, String imgDir) {
        this.context = context;
        if (list == null)
            productList = new ArrayList<ProductList>();
        else
            productList = list;
        inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.imageDir = imgDir; 
        RecipiesApplication app = (RecipiesApplication)getApplication();
        app.setImageDir(imgDir);
    }

    public List<ProductList> getProductList() {
        return productList;
    }

    public int getCount() {
        int size = productList.size();
        return size;
    }

    public Object getItem(int position) {
        Object item = productList.get(position);
        return item;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        RowHolder rowHolder = null;
        ProductList product = productList.get(position);

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.custom_product_list_square, null);

            rowHolder = new RowHolder();
            rowHolder.productIcon = (ImageView)convertView.findViewById(R.id.product_image_icon_sq_list);
            rowHolder.productName = (TextView)convertView.findViewById(R.id.product_name_sq_list);
            rowHolder.productPrice = (TextView)convertView.findViewById(R.id.product_price_sq_list);
            rowHolder.ln = (LinearLayout)convertView.findViewById(R.id.order_now);
            rowHolder.addProduct = (Button)convertView.findViewById(R.id.insert_btn);
            rowHolder.productCount = (EditText)convertView.findViewById(R.id.product_count);

            rowHolder.productInsertNow = (Button)convertView.findViewById(R.id.order_btn);
            rowHolder.productInsertNow.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    rowHolder.ln.setVisibility(View.VISIBLE); // not working...

                }
            });
            convertView.setTag(rowHolder);
        }
        else
            rowHolder = (RowHolder)convertView.getTag();


        return convertView;
    }

    @Override
    public void updateGridSquare(String id) {

        GridView gridView = (GridView)findViewById(R.id.products_group_grid);
        gridView.invalidateViews();
    }   
    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
    }
}

1 个答案:

答案 0 :(得分:0)

仅当if (convertView == null)将其移至

以下时才会收到通话
rowHolder = (RowHolder)convertView.getTag();

rowHolder.productInsertNow.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    rowHolder.ln.setVisibility(LinearLayout.VISIBLE); // not working...

                }
            });

希望这可以帮助......