简体   繁体   English

覆盖 Activity 布局的片段

[英]Fragment overlaying an Activity layout

I'm having problems working with fragments.我在处理片段时遇到问题。 The problem is: I have a layout in my activity with a ListView.问题是:我的活动中有一个带有 ListView 的布局。 When i select an item in this ListView, instantiates a fragment replacing the activity layout.当我在这个 ListView 中选择一个项目时,实例化一个替换活动布局的片段。 My result is both fragment and activity contents are showing on screen, overlaying each other.我的结果是片段和活动内容都显示在屏幕上,相互重叠。 What can I fix that (the fragment just replacing the activity content)?我能解决什么问题(片段只是替换了活动内容)? Here is the codes:这是代码:

activityLayout.xml活动布局.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" 
android:layout_height="match_parent"
android:id="@+id/activity" 
android:orientation="vertical">

   <FrameLayout android:id="@+id/fragment_to_replace"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:visibility="visible"
        android:orientation="vertical"
        android:layout_gravity="center" >

       <TextView android:id="@+id/textView"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:visibility="visible"
            android:text="@string/textView_select_table"
            android:textSize="30dp"/>

       <ListView android:layout_marginTop="20dp"
         android:layout_width="fill_parent"
         android:layout_height="200dp"
         android:id="@+id/listView"
         android:visibility="visible" 
         android:paddingTop="40dp" />

   </FrameLayout>

</LinearLayout>

Activity.java活动.java

 public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity);

    View view = findViewById(R.id.activity);    

    //Getting the ListView
    ListView listView = (ListView) view.findViewById(R.id.listView);
    listView.setTextFilterEnabled(true);

    //Setting the adapter
    listView.setAdapter(new Adapter(this));

    listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {

              //Creates a fragment 
              addFragment();
        }

    });

}

 public void addFragment(){

     MyFragment newFragment;

     FragmentTransaction newFragmentTransaction = getSupportFragmentManager().beginTransaction();

     newFragment = (MyFragment) getSupportFragmentManager()
            .findFragmentById(R.id.fragment);

      if (newFragment == null){
               newFragment = new MyFragment(this, getSupportFragmentManager());
           newFragmentTransaction.replace(R.id.fragment_to_replace, 
                new SelectDrinkFragment(this,   getSupportFragmentManager()));
           newFragmentTransaction.addToBackStack(null);
           newFragmentTransaction.commit();

      }
 }

fragmentLayout.xml片段布局.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/fragment"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical" >

   <TextView  
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:visibility="visible"
     android:text="@string/textView_two"
     android:textSize="20dp"/>

     <LinearLayout android:layout_marginTop="10dp"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:orientation="horizontal">

        <TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:visibility="visible"
        android:text="@string/textView" />

        <TextView android:id="@+id/textView_two"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:visibility="visible"/>

    </LinearLayout>

    <TextView 
      android:id="@+id/textView_three"
      android:layout_marginTop="10dp"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:visibility="visible"
      android:text="@string/textView_three"/>

     <ListView 
   android:layout_width="fill_parent"
   android:layout_height="0dp"
   android:layout_weight="1" 
   android:id="@+id/listView_two"
   android:visibility="visible" 
   android:paddingTop="40dp" />

 </LinearLayout>

Fragment.java片段.java

 public View onCreateView(final LayoutInflater inflater, final ViewGroup container,      Bundle saveInstanceState){


    View view = inflater.inflate(R.layout.fragment, container, false);  

    final ListView listView = (ListView) view.findViewById(R.id.listView_two); 

    CharSequence [] list = getResources().getStringArray(R.array.array);

    listView.setAdapter(new ListAdapter(getContext(), list));

    return view;

 }

ListAdapter.java列表适配器

 public class ListAdapter extends BaseAdapter{

      private Context context;
      private CharSequence[] drinkArraySize;

      public ListAdapter(Context context, CharSequence[] array){
          super();
          this.context = context;
          this.array= array;
      }

      @Override
      public int getCount() {
        return array.length;
      }

      @Override
      public Object getItem(int position) {
        return null;
      }

     @Override
     public long getItemId(int position) {
       return 0;
     }

     public Context getContext(){
          return context;
     }

     @Override
     public View getView(int position, View convertView, ViewGroup parent) {

        LinearLayout linearLayout = new LinearLayout(getContext());

        RadioButton radioButton = new RadioButton(getContext());
        radioButton.setText(array[position]);

        linearLayout.addView(radioButton);

        return linearLayout;
     }

}

FragmentTransaction.replace() looks like it only replaces another fragment (and not just any View), so I'm pretty sure this is expected behavior (or at least, I know I've had this happen to me before). FragmentTransaction.replace() 看起来只替换另一个片段(而不是任何视图),所以我很确定这是预期的行为(或者至少,我知道我以前遇到过这种情况)。 I suppose you have a few options:我想你有几个选择:

  1. I don't see in your code anything that would make the fragment's background opaque.我在您的代码中没有看到任何会使片段背景不透明的内容。 You could try that first and see if it helps.你可以先试试看是否有帮助。 If it does, that might be the simplest.如果是这样,那可能是最简单的。

  2. You could just set the other items' visibilities to View.GONE at the same time you perform your transaction, but that has some issues as far as backstack goes.您可以在执行交易的同时将其他项目的可见性设置为 View.GONE,但就 backstack 而言,这存在一些问题。

  3. You could replace the ListView and TextView with a ListFragment instead.您可以用 ListFragment 代替 ListView 和 TextView。 This is probably the "best" way to do it, but also is probably the most work.这可能是做到这一点的“最佳”方式,但也可能是最多的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM