说明:
我有两个片段,其中一个片段替换另一个片段。片段名称为FourFragment和OneFragment。两者都有自己的布局。
需求:
我的要求是我在工具栏上设置了textView,并且在MainActivity中有navigationView。运行我的应用程序时,默认片段会被加载,例如OneFragment是正确的。现在,当我单击工具栏上的textview时,将打开新的片段,例如FourFragment。 Fourfragment覆盖了屏幕的某些部分,而其余部分则是透明的。这样我就可以透明地看到以前的片段UI,例如OneFragment。我已经做了所有事情。
但是在Fourfragment加载到Onefragment上时出现了问题。但是在加载fourfragment时,可以单击onefragment的UI。
我希望在将onefragment加载到onefragment上时,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。
请帮我解决这个问题。
提前感谢
最佳答案
设置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">