如何从正常活动中打开listfragment

时间:2016-01-27 21:48:38

标签: android android-fragments android-listfragment

我在这里搜索,但我找不到任何符合我的代码的答案。我尝试做的是从mainActivity打开一个listfragment。我希望当用户按下操作栏中的图标时,会出现listFragment。

提前致谢

我尝试启动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;
    }

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>

listFragment列表视图的XML

<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>

这是我的listFragment类:

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;

        for (int i = 0; i < 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);
    }

}

1 个答案:

答案 0 :(得分:1)

试试这个:

1:创建一个新活动。 2:将其粘贴到OnCreate中的新活动中:

 Fragment fr = new FavouritesActionBar();
        FragmentManager fm = getFragmentManager();
        FragmentTransaction fragmentTransaction = fm.beginTransaction();
        fragmentTransaction.replace(R.id.fragment_place, fr);
        fragmentTransaction.commit();

3:在新活动的xml中:

<?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:在onOptionsItemSelected中的MainActivity中:

Intent intent = new Intent(this, yourNewActivityCreateInPoint1.class);
            startActivity(intent);