mardi 15 avril 2014

Android App Development Master-Detail Flow custom detail fragment layouts


Vote count:

0




I have just been faced with an assignment to build a tablet app via Fragments for Android and I have chosen to be using the Eclipse Master-Detail Flow Template in for this.


Unfortunality it seems that I cannot fathom how I can manipulate the detail fragment layouts view components like button to do actions at all.


I have set out to have 3-5 list alternatives;



  • 1 about-info for company (simple TextView),

  • 1 webView for company website preview,

  • 1 for phonecall layout for the company (I was thinking custom Fragment XML Layout here),

  • 1 for email-sending layout for the company, and

  • 1 for Google Maps GeoLocation for the company.


The first two was fairly easy to add since it was just a matter of adjusting the DummyContent.java constructor I realized, although when it came to the others I realized that I was going to need actions to add too instead of just adding extra content parameters in the DummyContent.java constructor- which made my brain kinda confused ;/.


I figured I could create XML Fragment Layouts and then inflate those in my "ItemDetailFragment.java" in a if-statement determining if "mItem.id" matches the specific menu alternatives.


But what about the actions for, lets say the phone calling layout ? Where do I put those? Havent quite been able to figure that one out yet??


My current solution is probably extremely crude and maybe even incorrect in some ways, but from what I know from making Activity-based basic Android apps this was the best I could come up with.


P.S. I'm kind of a beginner on this...


You can see the code I've got so far below:


Master-Detail Flow: ItemDetailFragment.java



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

View rootView;

// Show the dummy content as text in a TextView.
if (mItem != null) {
if(mItem.id.equals("1"))
{
rootView = inflater.inflate(R.layout.fragment_item_detail, container, false);
//Om företaget
((TextView) rootView.findViewById(R.id.item_detail)).setText(mItem.aboutUsContent);

}else if(mItem.id.equals("2"))
{
rootView = inflater.inflate(R.layout.fragment_item_detail, container, false);

//Företagets hemsida - nyttja item_detail fragment container för företagets hemsida såväl som textView,
//ev. även för Google Maps GeoLocation - för resten ladda helt nya fragment layouter. och gör actions baserade på dem.
((WebView) rootView.findViewById(R.id.item_detail)).loadUrl(mItem.website_url);

}else if(mItem.id.equals("3"))
{
rootView = inflater.inflate(R.layout.callphone_item_detail, container, false);
//Telefonnummer - ladda en helt ny Fragment layout med telefonnumret och knapp för att ringa
final TextView phoneNumberTextView = (TextView)this.getView().findViewById(R.id.phoneNmbrTextView);

final String phoneNmbr = mItem.phoneNumber;

//Place proper values in respective outputField
phoneNumberTextView.setText(phoneNmbr);


/*
* Create a button and link it to our android button view
* */
Button callButton = (Button)this.getView().findViewById(R.id.callButton);

/* callButton:
* Attach our button views onClickListener to each and every one of our buttons
* */
callButton.setOnClickListener(new View.OnClickListener() {

//For the dialing to occur, create Action dial intent
Intent callIntent = new Intent(Intent.ACTION_CALL);

/*
* Declare what to happen once button has been clicked
* */
public void onClick(View v)
{

callIntent.setData(Uri.parse("tel:" + phoneNmbr));
startActivity(callIntent);
}
});


}else if(mItem.id.equals("4"))
{
rootView = inflater.inflate(R.layout.email_item_detail, container, false);
//Epost - ladda en helt ny Fragment layout med epost och möjligheten att skicka epost


}else if(mItem.id.equals("5"))
{
rootView = inflater.inflate(R.layout.gmaps_item_detail, container, false);
//GeoLocation Google Maps karta


}
}

return rootView;
}


DummyContent.java relevant code:



public class DummyContent {

/**
* An array of sample (dummy) items.
*/
public static List<DummyItem> ITEMS = new ArrayList<DummyItem>();

/**
* A map of sample (dummy) items, by ID.
*/
public static Map<String, DummyItem> ITEM_MAP = new HashMap<String, DummyItem>();

//public String companyInfo = getString(R.string.company_info);

static {
// Add 3 sample items.
addItem(new DummyItem("1", "Om företaget", getString(R.string.company_info)));
addItem(new DummyItem("2", "Företagets (FPD's) hemsida", "http://www.fpd.se"));
addItem(new DummyItem("3", "Ringa företaget", "042130040"));
addItem(new DummyItem("4", "Maila företaget", "asw@fpd.se"));
}

private static void addItem(DummyItem item) {
ITEMS.add(item);
ITEM_MAP.put(item.id, item);
}

/**
* A dummy item representing a piece of content.
*/
public static class DummyItem {
public String id;
public String menuAlt;
//public String detailContent;

public String aboutUsContent;

//public String website_name;
public String website_url;

public String phoneNumber;

public String emailAddress;

public DummyItem(String id, String menuAlt, String detailContent) {
this.id = id;
this.menuAlt = menuAlt;

if(id.equals("1"))
{
this.aboutUsContent = detailContent;

}else if(id.equals("2"))
{
this.website_url = detailContent;

}else if(id.equals("3"))
{
this.phoneNumber = detailContent;

}else if(id.equals("4"))
{
this.emailAddress = detailContent;
}
}


Have watched several tutorials online for Fragments, as well as the Master-Detail Flow Template but with little luck to find examples or making a tad more "advanced" customizations of the DetailFragment Layouts with possibility of adding actual actions.


Insights, links to useful easily explained tutorials or just simply advice is very much welcome since I've been stuck on this exact problem for a very long time now and it's kinda getting quite frustrating to be honest :/


I appreciate all support and help :)! Thanks in advance.


With Best of Regards ;)



asked 1 min ago






Aucun commentaire:

Enregistrer un commentaire