2017-06-15 64 views
0

蔭試圖保持一個簡單的片段僅具有一個更button.But它是未來2倍這樣片段Android中不必要地重複不止一次

This is the image

我有一個線性佈局和另一個嵌套線性佈局在it.These全部存在於activity_welcome.xml其內容如下:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#99cc00" 
    android:orientation="vertical" 
    tools:context="com.acs.AfterLogin.WelcomeActivity"> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/replacable_container" 
     android:orientation="vertical"> 
    </LinearLayout> 

</LinearLayout> 

IAM的嘗試與它的Java類的片段,以取代replacable_container(線性佈局)是這樣的:

import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.support.v4.app.Fragment; 
import android.support.v7.widget.CardView; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Button; 

import com.acs.AfterLogin.WelcomeActivity; 
import com.acs.R; 
import com.squareup.picasso.Picasso; 

import de.hdodenhof.circleimageview.CircleImageView; 



public class WelcomeFragment1 extends Fragment{ 

    View v; 
    CardView cardView1,cardView2,cardView3; 
    CircleImageView userIcon; 
    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     v=inflater.inflate(R.layout.welcome_fragment_1,container); 
     return inflater.inflate(R.layout.welcome_fragment_1,container,false); 
    } 

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

片段的XML內容是這樣的:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="#e0ebe5" 
    android:orientation="vertical"> 
    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="hello"/> 
</LinearLayout> 

像片段transcation的操作和所有的都包含在其Java代碼在MainActivity是這樣的:

import android.os.AsyncTask; 
import android.os.Bundle; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentTransaction; 
import android.support.v7.app.AppCompatActivity; 
import com.acs.AfterLogin.WelcomeFragments.WelcomeFragment1; 
import com.acs.R; 
import com.squareup.picasso.Picasso; 

import de.hdodenhof.circleimageview.CircleImageView; 


public class WelcomeActivity extends AppCompatActivity { 
    FragmentTransaction transaction; 
    WelcomeFragment1 frag1; 
    FragmentManager manager; 
    String imageUri; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_welcome); 
     (new FragTask()).execute(); 
    } 
    private class FragTask extends AsyncTask<Void, Void, Void> { 
     @Override 
     protected Void doInBackground(Void... voids) { 
      transaction = manager.beginTransaction(); 
      frag1 = new WelcomeFragment1(); 
      transaction.replace(R.id.replacable_container, frag1, "welcome_1"); 
      transaction.commit(); 
      return null; 
     } 
    } 
} 

感謝很多提前:)

+0

你不需要替換AsyncTask中的片段 –

+0

它的話說跳過了200幀!所以我恐怕 – bharath

+1

我認爲你正在兩次膨脹片段。首先你把它放在你的'v'變量和'return'中。 – Ivan

回答

1

請在返回語句中返回膨脹視圖而不是再膨脹。

@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
v=inflater.inflate(R.layout.welcome_fragment_1,container); 
return v; 
} 
+0

仍在工作.. – bharath

+0

它工作兄弟!非常感謝 – bharath

+0

與我們在評論中所說的完全相同-.- – Ivan

2

嘗試改變此行

v=inflater.inflate(R.layout.welcome_fragment_1,container); 

v=inflater.inflate(R.layout.welcome_fragment_1,container, false); 

並返回膨脹視圖。布爾值表示您不想將視圖附加到容器。檢查this。片段將在稍後自行附加。

+0

你的假也工作感謝的人 – bharath