当另一个片段在android中打开时,如何禁用片段UI?

时间:2016-07-11 09:04:48

标签: android android-layout android-fragments

说明:            我有两个片段,我将片段替换为另一个。我的片段名称是FourFragment和OneFragment。两者都有它自己的布局。

要求:

我的要求是在工具栏上设置textView,我的MainActivity中有navigationView。当我的应用程序运行时,默认片段被加载,例如OneFragment它是正确的。现在,当我点击工具栏文本视图时,它会打开新的片段,例如FourFragment。 Fourfragment覆盖屏幕的某些部分,而fourfragment的其余部分是透明的。这样我就可以透明地看到先前的片段UI,例如OneFragment.I已经完成了所有的事情。

但是当fourfragment加载到Onefragment上时,问题就出现了问题。但是当加载了fourfragment时,onefragment的UI是可以点击的。

我想我的onefragment是可见的,而fourfragment是在onefragment上加载的,但是当fourfragment打开时,onefargment的UI是不可点击的。

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true"
    tools:context="com.millu.navidemo.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" >
            <TextView
                android:id="@+id/textView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:textSize="20sp"
                android:text="TextView"
                android:gravity="center"
                android:clickable="true"/>
        </android.support.v7.widget.Toolbar>


    </android.support.design.widget.AppBarLayout>
    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="?attr/actionBarSize">

    </FrameLayout>
    <FrameLayout
        android:id="@+id/dialog_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="?attr/actionBarSize">
    </FrameLayout>
</android.support.design.widget.CoordinatorLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    TextView textView;
    boolean flag=false;
    FrameLayout dialog_frame;
    NavigationView navigationView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        if(savedInstanceState==null){
            MenuItem item=navigationView.getMenu().getItem(0);
            onNavigationItemSelected(item);
        }
        textView=(TextView)findViewById(R.id.textView);
        dialog_frame=(FrameLayout)findViewById(R.id.dialog_frame);

        final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                FourFragment ff=null;
                if(!flag) {
                    Toast.makeText(MainActivity.this, "Clicked!!!!", Toast.LENGTH_SHORT).show();
                    ff= new FourFragment();
                    ft.replace(R.id.dialog_frame,ff);
                    ft.addToBackStack(null);
                    ft.commit();
                    dialog_frame.setVisibility(View.VISIBLE);
                    flag=true;
                }
                else{
                    Toast.makeText(MainActivity.this, "else", Toast.LENGTH_SHORT).show();
                    dialog_frame.setVisibility(View.GONE);
                    ft.remove(ff);
                    ft.commit();
                    OneFragment oneFragment=new OneFragment();
                    ft.replace(R.id.container,oneFragment);
                    ft.commit();
                    flag=false;
                }
            }
        });
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {

        Fragment fragment=null;

        switch (item.getItemId()){
            case R.id.nav_camera:
                fragment=new OneFragment();
                break;
        }
        if(fragment!=null){
            FragmentTransaction ft=getSupportFragmentManager().beginTransaction();
            ft.replace(R.id.container,fragment);
            ft.commit();
        }
        else
        {
            Toast.makeText(MainActivity.this, "Fragment is null!!", Toast.LENGTH_SHORT).show();
        }
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}

OneFragment.java

public class OneFragment extends Fragment {

    View rootView;

    TwoWayView twoWayView;
    List<Days> days_list;
    String[] str_arr={"A","B","C","D","E","F","G","H"};
    int[] img_arr={R.drawable.ic_menu_send,R.drawable.ic_menu_gallery,R.drawable.ic_drawer,R.drawable.ic_menu_camera,
    R.drawable.ic_menu_manage,R.drawable.ic_menu_share,R.drawable.ic_menu_slideshow};

    Button button;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView=inflater.inflate(R.layout.fragment_one, container, false);

        twoWayView=(TwoWayView)rootView.findViewById(R.id.twoWayView);

        button=(Button)rootView.findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getContext(), "Button clicked!!1", Toast.LENGTH_SHORT).show();
            }
        });
        days_list=new ArrayList<>();
        for(int i=0;i<str_arr.length-1;i++){
            Days d=new Days(img_arr[i],str_arr[i]);
            days_list.add(d);
        }
        DayAdapter adapter=new DayAdapter(getContext(),days_list);
        twoWayView.setAdapter(adapter);

        twoWayView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(getContext(), "One:"+days_list.get(i).getName(), Toast.LENGTH_SHORT).show();
            }
        });
        return rootView;
    }
}

FourFragment.java

public class FourFragment extends Fragment {

    View rootView;
    TwoWayView twoWayView;
    List<Days> days_list;
    String[] name={"Milan","Sagar","Mithun","Brijesh","Ravi","Vishal","Nikhil"};
    public FourFragment(){}

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView=inflater.inflate(R.layout.fragment_dialog, container, false);

        twoWayView=(TwoWayView)rootView.findViewById(R.id.twoWayView);
        days_list=new ArrayList<>();
        for(int i=0;i<name.length;i++){
            Days days=new Days();
            days.setName(name[i]);
            days_list.add(days);
        }

        DayAdapter dayAdapter=new DayAdapter(getContext(),days_list);
        twoWayView.setAdapter(dayAdapter);

        twoWayView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(getContext(), "One:"+days_list.get(i).getName(), Toast.LENGTH_SHORT).show();
            }
        });
        return rootView;
    }
}

注意:我想在单击工具栏上设置的textview后看到fourfragment可见时,会看到onefragment透明。

请帮我解决这个问题。 Thanx提前

1 个答案:

答案 0 :(得分:1)

设置FourFragment主布局clickable =“true”,这样一旦加载了fourfragment,它的父布局就是可点击的,所以隐藏的片段小部件不会点击,直到这个主布局可点击为止

FourFragment.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:clickable="true">