如何将活动更改为碎片

时间:2014-07-07 07:08:19

标签: java android android-fragments

我想将我的Activity更改为Fragment我努力尝试但我无法将活动更改为片段。任何人都可以告诉我怎么做?在哪里编辑我是Android新手。请告诉我并提前致谢

这是我的活动:

import android.app.Activity;

   import android.app.ProgressDialog;
   import android.content.Context;
  import android.content.Intent;
  import android.content.SharedPreferences;
  import android.content.SharedPreferences.Editor;
   import android.os.Bundle;
  import android.os.StrictMode;

import android.view.View;
import android.view.View.OnClickListener;
 import android.view.LayoutInflater;
import android.view.ViewGroup;
 import android.view.Window;
 import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
 import android.widget.Toast;

 import com.example.phonebook.models.UserModel;
import com.example.phonebook.services.ImageLoader;

public class Welcome extends Activity {
private ProgressDialog pDialog;
Button editprofile;
JSONParser jsonParser = new JSONParser();

ImageView Image;
UserModel user = (UserModel) getIntent().getSerializableExtra("User");
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
    .detectDiskReads().detectDiskWrites().detectNetwork()
    .penaltyLog().build());
   setContentView(R.layout.welcome);


    requestWindowFeature(Window.FEATURE_NO_TITLE);

     user = (UserModel) getIntent().getSerializableExtra("User");
Button editprofile = (Button)findViewById(R.id.btn_edit);
    ImageView image = (ImageView)findViewById(R.id.imageView1);
    TextView userfullname = (TextView) findViewById(R.id.userfullname);
    TextView tv_age = (TextView) findViewById(R.id.age);
    TextView tv_gender = (TextView) findViewById(R.id.gender);
    TextView tv_intrseted = (TextView) findViewById(R.id.intrestedin);


    int loader = R.drawable.loader;

    String UserfullName = user.getUser_Full_Name();
    String image_url = user.getUser_Image();
    String Age = user.getUser_Age();
    String Gender = user.getGender();
    String IntrestedIn = user.getIntrest_In();
    userfullname.setText(UserfullName);
    tv_age.setText(Age);
    tv_gender.setText(Gender);
    tv_intrseted.setText(IntrestedIn);
    Button logout=(Button)findViewById(R.id.logout);
    logout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            SharedPreferences sharedPreferences =getSharedPreferences(MainActivity.MyLOGINDATA, Context.MODE_PRIVATE);

                      Editor editor = sharedPreferences.edit();
                      editor.clear();
                      editor.commit();
                      moveTaskToBack(true); 
                      Intent i=new Intent(getApplicationContext(),MainActivity.class);
                      startActivity(i);
                      finish();

        }
    });

    editprofile.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent e = new Intent(getApplicationContext(),
                    EditProfile.class);
            e.putExtra("userInfo", user);
            finish();
            startActivity(e);

        }
    });

    // ImageLoader class instance
    ImageLoader imgLoader = new ImageLoader(getApplicationContext(),
            Welcome.this);

    image.setTag(image_url);

// whenever you want to load an image from url
// call DisplayImage function
// url - image url to load
// loader - loader image, will be displayed before getting image
// image - ImageView 
    imgLoader.DisplayImage(image_url, Welcome.this, image);
}

}

2 个答案:

答案 0 :(得分:5)

只需了解一些步骤,您就可以轻松地将活动转换为片段以及将来......:

首先,不要延长Activity,只需展开Fragment ..

例如:public class Welcome extends Fragment{

然后覆盖onCreateView() ..

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
.....
}

然后通过LayoutInflater对布局进行膨胀,并设置为View以进一步用于子视图inilialization ..

喜欢:View mView = inflater.inflate(R.layout.welcome, null);

然后在main view..like:

的帮助下初始化所有子视图
 ImageView image = (ImageView) mView.findViewById(R.id.imageView1);
 TextView userfullname = (TextView) mView.findViewById(R.id.userfullname);

现在完成所有与此处活动相同的任务..

重要的事情.. getActivity()的位置使用context ..

例如:Toast.maketext(getActivity(), "...", Toast.LENGTH_LONG).show();

有关详细信息,请访问开发人员中的Fragment ..

谢谢

答案 1 :(得分:0)

将Activity类更改为Fragment类需要扩展 Fragment 超类而不是 Activity ,除了实现一些特定于片段的Callback方法,例如: OnCreateView() OnActivityCreated() ...
片段可以通过其他片段,活动以编程方式启动和启动,也可以在另一个片段或活动中使用XML布局中的片段标记。 请查看android开发者网站上的片段指南:
http://developer.android.com/training/basics/fragments/creating.html

http://developer.android.com/guide/components/fragments.html