列表片段和导航抽屉不工作togheter

时间:2016-02-28 00:56:39

标签: android android-fragments

我有问题。我制作了一个列表片段,按照我的意图显示我的数据,当我点击某个项目时,它会将我带到显示其内容的活动。现在我制作了一个带有两个项目的导航抽屉,这两个项目应该带我到他们指定的活动。相反,应该显示两个项目的抽屉是空白的,我列表中的项目将我带到分配给我的抽屉列表的活动中。

这是我的XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:showIn="@layout/activity_main">

        <fragment
            android:id="@+id/titles"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            class="utility.AirplaneListFragment"
             />


    </RelativeLayout>

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    <ListView
        android:id="@android:id/list"
        android:background="@android:color/white"
        android:layout_width="305dp"
        android:dividerHeight="3dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"/>
</android.support.v4.widget.DrawerLayout>

这是我的ListFragment

package utility;

import android.app.DialogFragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ListFragment;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.example.windows.vliegtuigspotted.R;
import com.example.windows.vliegtuigspotted.activity.AirplaneContentActivity;
import com.example.windows.vliegtuigspotted.activity.EditAirplaneActivity;

import adapter.CustomCursorAdapter;
import database.DataSource;


/**
 * Created by hort on 27-2-2016.
 */

public class AirplaneListFragment extends ListFragment implements ConfirmDeleteDialog.ConfirmDeleteDialogListener{
    boolean mDualPane;
    int mCurCheckPosition = 0;
    private long itemId;
    CustomCursorAdapter cursorAdapter;
    DataSource datasource;

    final int Edit = 0;
    final int Delete = 1;


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        datasource = new DataSource(this.getActivity().getApplicationContext());
        cursorAdapter = new CustomCursorAdapter(this.getActivity(), datasource.getAllAirplanesCursor(), 0);
        setListAdapter(cursorAdapter);

        registerForContextMenu(getListView());

        View detailsFrame = getActivity().findViewById(R.id.details);

        mDualPane = detailsFrame != null
                && detailsFrame.getVisibility() == View.VISIBLE;


        if (savedInstanceState != null) {
            // Restore last state for checked position.
            mCurCheckPosition = savedInstanceState.getInt("curChoice", 0);
        }

        if (mDualPane) {

            getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);

            showDetails(mCurCheckPosition);
        } else {

            getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
            getListView().setItemChecked(mCurCheckPosition, true);
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Toast.makeText(getActivity(), "onSaveInstanceState",
                Toast.LENGTH_LONG).show();

        outState.putInt("curChoice", mCurCheckPosition);
    }


    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        showDetails(position);
    }

    void showDetails(int index) {
        mCurCheckPosition = index;


        if (mDualPane) {

            getListView().setItemChecked(index, true);

            AirplaneContentFragment details = (AirplaneContentFragment) getFragmentManager().findFragmentById(R.id.details);
            if (details == null || details.getShownIndex() != index) {


                details = AirplaneContentFragment.newInstance(index);

                Toast.makeText(getActivity(),
                        "showDetails dual-pane: create and relplace fragment",
                        Toast.LENGTH_LONG).show();


                FragmentTransaction ft = getFragmentManager().beginTransaction();
                ft.replace(R.id.details, details);
                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                ft.commit();
            }

        } else {

            Intent intent = new Intent();

            intent.setClass(getActivity(), AirplaneContentActivity.class);
            intent.putExtra("index", cursorAdapter.getItemId(index));

            startActivity(intent);
        }
    }

    @Override

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1) {
            if(resultCode == getActivity().RESULT_OK)  {
                cursorAdapter.changeCursor(datasource.getAllAirplanesCursor());
            }
        }
        if (requestCode == 2) {
            if (resultCode == getActivity().RESULT_OK) {
                cursorAdapter = new CustomCursorAdapter(this.getActivity(), datasource.getAllAirplanesCursor(), 0);
                setListAdapter(cursorAdapter);
            }
        }
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);

        menu.add(0, Edit, 0, "Edit");
        menu.add(0, Delete, 0, "Delete");
    }

    @Override

    public boolean onContextItemSelected(MenuItem item) {
        AdapterView.AdapterContextMenuInfo itemInfo = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        itemId = cursorAdapter.getItemId(itemInfo.position);

        switch(item.getItemId()){
            case Edit:
                Intent intent = new Intent();
                intent.setClass(this.getActivity(), EditAirplaneActivity.class);
                intent.putExtra("itemId", itemId);
                startActivityForResult(intent, 2);
                break;
            case Delete:
                DialogFragment dialog = new ConfirmDeleteDialog();
                dialog.show(this.getActivity().getFragmentManager(), "ConfirmDeleteDialog");

        }
        return super.onContextItemSelected(item);

    }

    @Override
    public void onDialogPositiveClick(DialogFragment dialog) {

        datasource.deleteAirplane(itemId);
        cursorAdapter.changeCursor(datasource.getAllAirplanesCursor());

    }

    @Override
    public void onDialogNegativeClick(DialogFragment dialog) {
        //Do nothing, Dialog will disappear
    }

}

这是我的MainActivity

package utility;

import android.app.Activity;
import android.app.DialogFragment;


import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ListFragment;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.example.windows.vliegtuigspotted.R;
import com.example.windows.vliegtuigspotted.activity.AddAirplaneActivity;
import com.example.windows.vliegtuigspotted.activity.AirplaneEventPlannerActivity;

import java.util.Calendar;
import java.util.Locale;

import adapter.CustomCursorAdapter;
import adapter.NavDrawerListAdapter;
import database.DataSource;
import model.DrawerItem;


// Demonstration of using fragments to implement different activity layouts.
// This sample provides a different layout (and activity flow) when run in
// landscape.

public class FragmentLayout extends AppCompatActivity {
    CustomCursorAdapter cursorAdapter;
    DataSource datasource;
    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mDrawerToggle;
    private String[] mDrawerListItems;

    ViewHolder holder = new ViewHolder();

    static class ViewHolder{
        private ListView mDrawerList;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        datasource = new DataSource(this);

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer);
        holder.mDrawerList = (ListView) findViewById(android.R.id.list);
        mDrawerListItems = getResources().getStringArray(R.array.drawer_list);

        DrawerItem drawerItems[] = new DrawerItem[]
                {
                        new DrawerItem(R.drawable.ic_home_black_24dp, "Home"),
                        new DrawerItem(R.drawable.ic_event_note_black_24dp, "Events")

                };

        NavDrawerListAdapter adapter = new NavDrawerListAdapter(this,
                R.layout.drawer_list_item, drawerItems);

        holder.mDrawerList.setAdapter(adapter);

        holder.mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
                        Intent activityHome = new Intent(FragmentLayout.this, FragmentLayout.class);
                        startActivity(activityHome);

                    case 1:
                        Intent activityCalendar = new Intent(FragmentLayout.this, AirplaneEventPlannerActivity.class);
                        startActivity(activityCalendar);
                }
                mDrawerLayout.closeDrawer(holder.mDrawerList);
            }
        });

        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) {
            public void onDrawerClosed(View v) {
                super.onDrawerClosed(v);
                invalidateOptionsMenu();
                syncState();
            }

            public void onDrawerOpened(View v) {
                super.onDrawerOpened(v);
                invalidateOptionsMenu();
                syncState();
            }
        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        mDrawerToggle.syncState();



        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setBackgroundTintList(getResources().getColorStateList(R.color.lightBlue));
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                startActivityForResult(new Intent(FragmentLayout.this, AddAirplaneActivity.class), 1);
            }
        });
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState){
        super.onPostCreate(savedInstanceState);
        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig){
        super.onConfigurationChanged(newConfig);
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        switch (item.getItemId()){
            case android.R.id.home: {
                if (mDrawerLayout.isDrawerOpen(holder.mDrawerList)){
                    mDrawerLayout.closeDrawer(holder.mDrawerList);
                } else {
                    mDrawerLayout.openDrawer(holder.mDrawerList);
                }
                return true;
            }
            default: return super.onOptionsItemSelected(item);
        }
    }

}

我对Android很新,我第一次使用片段时,我不确定我现在做错了什么。

0 个答案:

没有答案