执行片段后,onitemclicklistener不起作用

时间:2015-04-15 23:18:25

标签: android android-fragments

我的问题是,只要我在主布局中调用片段,就会在帧中调用片段,但在此之后,onitemclick侦听器将停止工作。我错过了什么? main.java

import java.util.Locale;

import android.annotation.TargetApi;
import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.app.SearchManager;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;


public class Main extends FragmentActivity {
    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private ActionBarDrawerToggle mDrawerToggle;

    private CharSequence mDrawerTitle;
    private CharSequence mTitle;
    private String[] mDrawerMenu;

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

        mTitle = mDrawerTitle = getTitle();
        mDrawerMenu = getResources().getStringArray(R.array.menu);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.list_view);

        // set a custom shadow that overlays the main content when the drawer opens
        //mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
        // set up the drawer's list view with items and click listener
        mDrawerList.setAdapter(new ArrayAdapter<String>(this,
                R.layout.custom_menu_list, mDrawerMenu));
        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu items for use in the action bar
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_activity_actions, menu);
        return super.onCreateOptionsMenu(menu);
    }


    //Action bar on item click events
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action buttons
        switch(item.getItemId()) {
            case R.id.menu_icon:
                // create intent to perform web search for this planet
                Log.d("Hello","Menu clicked");
                mDrawerLayout.openDrawer(mDrawerList);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    /* The click listener for ListView in the navigation drawer */
    private class DrawerItemClickListener implements ListView.OnItemClickListener {
        @TargetApi(Build.VERSION_CODES.HONEYCOMB)
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
            Log.d("Hello", String.valueOf(position));
            //Get the option name and compare
            TextView text = (TextView) arg1.findViewById(android.R.id.text1);
            String MenuName = text.getText().toString();
            Log.d("Click", MenuName);
            if(position == 0){
                Log.d("Click","Home Clicked");
                // update the main content by replacing fragments

                android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
                android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                MenuFragment fragment = new MenuFragment();
                fragmentTransaction.replace(R.id.drawer_layout, fragment, String.valueOf(position));
                fragmentTransaction.commit();

            }
            else if(position == 1){
                Log.d("Click","News Clicked 2");
            }


            mDrawerLayout.closeDrawer(mDrawerList);
        }
    }

}

这是main.xml

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF">
    <!-- The main content view -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:descendantFocusability="blocksDescendants"/>
    <!-- The navigation drawer -->
    <ListView android:id="@+id/list_view"
        android:layout_width="320dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#000000"/>



</android.support.v4.widget.DrawerLayout>

这是调用布局的菜单程序。

public class MenuFragment extends Fragment {


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        return inflater.inflate(R.layout.sports, container, false);
    }

}

现在,因为它在main中的框架中被替换(当位置为0时,调用片段),之后如果调用1,则onitemclicklistener不起作用。那是为什么?

谢谢

1 个答案:

答案 0 :(得分:1)

替换

fragmentTransaction.replace(R.id.drawer_layout, fragment, String.valueOf(position));

fragmentTransaction.replace(R.id.content_frame, fragment, String.valueOf(position));

您应该替换contents_frame而不是drawer_layout