android - How can I open a listfragment from normal activity -
i search here don't find answer work code. i'm trying open listfragment mainactivity. want listfragment appear when user press icon in action bar.
thanks in advance
code try launch listfragment:
if (id == r.id.action_favourites) { listfragment newfragment = new listfragment(); fragmentmanager fragmentmanager = getfragmentmanager(); fragmenttransaction fragmenttransaction = fragmentmanager.begintransaction(); fragmenttransaction.replace(r.id.favourites_fragment, newfragment); fragmenttransaction.addtobackstack(null); fragmenttransaction.commit(); return true; } custom row in listfragment:
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="45dp" android:padding="5dp" android:id="@+id/favourites_fragment"> <textview android:id="@+id/newspapernamefavourite" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_vertical" android:textsize="20sp" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancesmall" android:id="@+id/passurl" android:layout_centervertical="true" android:layout_alignparentright="true" android:layout_alignparentend="true" android:layout_marginright="135dp" android:layout_marginend="135dp" android:visibility="gone"/> </relativelayout> xml of listview of listfragment
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:ads="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <listview android:id="@id/android:list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1.0" android:layout_margintop="50dp"/> </linearlayout> this listfragment class:
public class favouritesactionbar extends listfragment { private static final string background_color = "color"; private static final string index = "index"; private string title; private int page; private adview madview; private adrequest adrequest; private databasewrapper database; public static favourites newinstance(int color, string title) { favourites fragment = new favourites(); bundle bundle = new bundle(); bundle.putint(background_color, color); bundle.putstring("sometitle", title); fragment.setarguments(bundle); fragment.setretaininstance(true); return fragment; } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); page = getarguments().getint("someint", 0); title = getarguments().getstring("sometitle"); database = new databasewrapper(getcontext()); } @override public void onactivitycreated(bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { viewgroup rootview = null; final string[] name; final string[] url; database.getalldata(); name = new string[database.getalldata().size()]; url = new string[database.getalldata().size()]; list<map<string, string>> list = new arraylist<map<string, string>>(); map<string, string> map; (int = 0; < database.getalldata().size(); i++) { name[i] = database.getalldata().get(i).getname(); url[i] = database.getalldata().get(i).geturl(); rootview = (viewgroup) inflater.inflate(r.layout.fragment_listview, container, false); map = new hashmap<string, string>(); map.put("name", name[i]); map.put("url", url[i]); list.add(map); simpleadapter adapter = new simpleadapter(getactivity(), list, r.layout.favourites, new string[] { "name", "url" }, new int[] { r.id.newspapernamefavourite, r.id.passurl }); setlistadapter(adapter); setretaininstance(true); } return rootview; } @override public void onlistitemclick(listview l, view view, int position, long id){ textview tv2=(textview)view.findviewbyid(r.id.passurl); intent myintent = new intent(getactivity(), webview.class); myintent.putextra("_url", tv2.gettext().tostring()); startactivity(myintent); } }
try this:
1: create new activity. 2: paste in new activity in oncreate:
fragment fr = new favouritesactionbar(); fragmentmanager fm = getfragmentmanager(); fragmenttransaction fragmenttransaction = fm.begintransaction(); fragmenttransaction.replace(r.id.fragment_place, fr); fragmenttransaction.commit(); 3:in xml of new activity:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <fragment android:name="com.yourpackage.favouritesactionbar" android:id="@+id/fragment_place" android:layout_width="match_parent" android:layout_height="match_parent"/> </linearlayout> 4: in mainactivity in onoptionsitemselected:
intent intent = new intent(this, yournewactivitycreateinpoint1.class); startactivity(intent);
Comments
Post a Comment