从FragmentActivity打开新的Fragment

时间:2015-09-09 09:01:36

标签: android android-fragments android-fragmentactivity fragmenttransaction

我有扩展FragmentActivity的MainActivity,我也有包含连接ID列表的ConnectionListFragment。当用户点击连接ID时,我想打开新的LogFragment(扩展Fragment)并提供有关该连接的更多详细信息。但问题是当用户选择连接ID片段时从不显示。这是我的代码 我的MainActiviy

public class MainActivity extends FragmentActivity implements LogTextUpdater, LogDetail{

    private PagerAdapter mPagerAdapter;
    private static int id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        UiFragment.setLog(this);
        ConnectionListFragment.setLogDetail(this);

        initialisePaging();
    }

    private void initialisePaging() {
        List<Fragment> fragments = new Vector<>();
        fragments.add(Fragment.instantiate(this, UiFragment.class.getName()));
        fragments.add(Fragment.instantiate(this, ConnectionListFragment.class.getName()));
        mPagerAdapter = new PagerAdapter(getSupportFragmentManager(), fragments);
        ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
        pager.setAdapter(mPagerAdapter);
    }

    @Override
    public void updateText(String sender) {
        //LogFragment.readFromDatabase(sender);
        ConnectionListFragment.updateList();

        Toast.makeText(this, sender, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void startFragment(String connectionID) {
        Toast.makeText(this, "Start " + connectionID, Toast.LENGTH_SHORT).show();

        LogFragment logFrag = new LogFragment();
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        logFrag.setConnectionID(connectionID);
        ft.replace(R.id.sample_content_fragment, logFrag);
        ft.addToBackStack(null);
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        ft.commit();
    }
}

我的main_activity.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        />
    <FrameLayout
        android:id="@+id/sample_content_fragment"
        android:layout_weight="2"
        android:layout_width="match_parent"
        android:layout_height="0px" />
</LinearLayout>

这是我的ConnectionListFragment

public class ConnectionListFragment extends Fragment{

    private static SQLiteDatabase db;
    private static SQLiteOpenHelper helper;
    private static Context context;
    private static ListView listView;
    private static SimpleCursorAdapter adapter;
    private static LogDetail logDetail;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getActivity();

        adapter = new SimpleCursorAdapter(context, R.layout.connection_list, null,
                new String[]{Constants.SESSION_ID}, new int[]{R.id.connectID}, 0);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_connection_list, container, false);
    }

    public static void setLogDetail(LogDetail logTextUpdater){
        logDetail = logTextUpdater;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        listView = (ListView) view.findViewById(R.id.listView);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String selectedID = ((TextView) view.findViewById(R.id.connectID)).getText().toString();
                //Toast.makeText(getActivity(), selectedID, Toast.LENGTH_SHORT).show();
                logDetail.startFragment(selectedID);
            }
        });
    }

    public static void updateList(){
        new ConnectionListID().execute();
    }

    public static class ConnectionListID extends AsyncTask<Void, Void, Cursor> {

        @Override
        protected Cursor doInBackground(Void... params) {
            helper = new Database(context);
            db = helper.getReadableDatabase();
            return db.query(true, Constants.TABLE_NAME, new String[]{"_id", Constants.SESSION_ID},
                    null, null, Constants.SESSION_ID, null, null, null);
        }

        @Override
        protected void onPostExecute(Cursor cursor) {
            super.onPostExecute(cursor);

            adapter.changeCursor(cursor);
            listView.setAdapter(adapter);
        }
    }
}

所以我的代码有什么问题。我保证当用户点击其中一个列表项时调用startFragment方法,因为我在设备上看到Toast。

0 个答案:

没有答案