Relative layout with fragments



I want to create RelativeLayout through the system of fragments, so that from a fragment can send three different parameters, and apply them in the relative layout. The problem I have is that I created the classe but I can not give parameters, do not know how to send an id of the particular image.


Here attached the fragment, the classe, and xml I've used since I do not add him calling the classe and I miss calling xml.



Products.xml



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:id="@+id/imgProduct"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/pollo"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"/>

<TextView
android:id="@+id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/imgProduct"
android:layout_centerInParent="true"
android:textSize="16dp"
android:text="Pollo"/>

<TextView
android:id="@+id/txtPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/txtName"
android:layout_centerInParent="true"
android:textSize="16dp"
android:text="13,00€"/>


</RelativeLayout>


this is the class



public class PanelProduct extends RelativeLayout {

public PanelProduct(Context context, String sName, String sPrice){
super(context);
//Creamos el relative layout
RelativeLayout relativeLayout = new RelativeLayout(this.getContext());
//Creamos parametros para la imagen del relative layout
RelativeLayout.LayoutParams ImageLayoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);

//Creamos un image view
ImageView ImageProduct = new ImageView(this.getContext());
ImageProduct.setLayoutParams(ImageLayoutParams);
ImageView imgView=(ImageView) findViewById(R.id.carrito);
this.addView(imgView);
//ImageProduct.setImageBitmap(bmpProduct);
//this.addView(ImageProduct);

//Creamos un text view
TextView ProductName = new TextView(this.getContext());
ProductName.setLayoutParams(ImageLayoutParams);
ProductName.setText(sName);
this.addView(ProductName);


//Creamos un text view
TextView ProductPrice = new TextView(this.getContext());
ProductPrice.setLayoutParams(ImageLayoutParams);
ProductPrice.setText(sPrice);
this.addView(ProductPrice);
}

}


And tis is the fragment where i try to call the class



public class HomeFragment extends Fragment {

public HomeFragment(){}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

PanelProduct pnlProduct = new PanelProduct(this.getActivity(), ImageView.pollo, "Carne", "13,00€");

View rootView = inflater.inflate(R.layout.products, container, false);
return rootView;
}
}


Thanks


No comments:

Post a Comment