Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我想在两种不同类型的子活动中重用父活动的大部分可见部分_Java_Android - Fatal编程技术网

Java 我想在两种不同类型的子活动中重用父活动的大部分可见部分

Java 我想在两种不同类型的子活动中重用父活动的大部分可见部分,java,android,Java,Android,在我的应用程序中,用户可以查看自己的配置文件,也可以查看其他用户的配置文件。它们80%相同,所以我想使用一个基本活动,这两个活动可以扩展,并且只实现差异 子活动位于父活动之上,隐藏我设置的所有数据——文本和用户化身。我不能像使用主题那样使其透明/半透明/对话,因为出于某些原因,我被迫使用不支持这些主题的SherlockActivity:( 也许我使用的整个模式都是错误的,那么我该怎么办呢?我不想回到两个不同的活动中,它们有80%的MyProfile和UserProfile代码重复 我当前的实现如

在我的应用程序中,用户可以查看自己的配置文件,也可以查看其他用户的配置文件。它们80%相同,所以我想使用一个基本活动,这两个活动可以扩展,并且只实现差异

子活动位于父活动之上,隐藏我设置的所有数据——文本和用户化身。我不能像使用主题那样使其透明/半透明/对话,因为出于某些原因,我被迫使用不支持这些主题的SherlockActivity:(

也许我使用的整个模式都是错误的,那么我该怎么办呢?我不想回到两个不同的活动中,它们有80%的MyProfile和UserProfile代码重复

我当前的实现如下

在某些活动中,用户单击我的个人资料:

Intent goToUserProfile = new Intent(this, MyProfile.class);
goToUserProfile.putExtra("currentUser", true);
startActivity(goToUserProfile);
public class MyProfile extends PersonProfile implements OnClickListener, OnChangeUserQuoteListener {

    // Change user quote dialog and its XML components (only the additional controls are added)
    private Dialog changeUserQuoteDialog;
    private EditText etChangeUserQuoteContent;
    private Button bChangeUserQuoteSubmit;
    private Button bChangeUserQuoteCancel;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            identifyUser(extras); //identifyUser is a method in PersonProfile, the superclass (I will add it below)
        }

        // Set OnClick Listeners - these are the ones that are different in MyProfile compared to UserProfile
        bUserProfileActionButtonOne.setOnClickListener(this);
        bUserProfileActionButtonTwo.setOnClickListener(this);
        tvUserProfileStatsAreVisibleNote.setOnClickListener(this);
        iUserAvatar.setOnClickListener(this);


    } // End of onCreate Method
在我的个人资料中:

Intent goToUserProfile = new Intent(this, MyProfile.class);
goToUserProfile.putExtra("currentUser", true);
startActivity(goToUserProfile);
public class MyProfile extends PersonProfile implements OnClickListener, OnChangeUserQuoteListener {

    // Change user quote dialog and its XML components (only the additional controls are added)
    private Dialog changeUserQuoteDialog;
    private EditText etChangeUserQuoteContent;
    private Button bChangeUserQuoteSubmit;
    private Button bChangeUserQuoteCancel;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            identifyUser(extras); //identifyUser is a method in PersonProfile, the superclass (I will add it below)
        }

        // Set OnClick Listeners - these are the ones that are different in MyProfile compared to UserProfile
        bUserProfileActionButtonOne.setOnClickListener(this);
        bUserProfileActionButtonTwo.setOnClickListener(this);
        tvUserProfileStatsAreVisibleNote.setOnClickListener(this);
        iUserAvatar.setOnClickListener(this);


    } // End of onCreate Method
在PersonProfile(超类)中


为什么不为您的用户配置文件创建一个视图布局,包含您的用户和其他用户所需的所有小部件

然后,您有一个类MyProfile,该类会扩大布局,显示对MyUser重要的所有小部件并隐藏其他小部件。然后创建另一个类OtherUserProfile,该类会加载相同的布局,但只显示对OtherUser重要的小部件并隐藏对MyUser重要的小部件

您应该/可以将这些视图包装成一个片段,并根据单击的用户显示片段

编辑:

基类:

public abstract class Profile extends RelativeLayout {

public Profile(Context context, AttributeSet attrs) {
    super(context, attrs);
    initImpl();
}

final private void initImpl() {
    ((LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.view_profile, this, true);
    init();
}

protected abstract void init();
}

用户类:

public class UserProfile extends Profile {

public UserProfile(Context context, AttributeSet attrs) {
    super(context, attrs);
}

@Override
protected void init() {
    findViewById(R.id.tvOther).setVisibility(View.GONE);
}
public class OtherProfile extends Profile {

public OtherProfile(Context context, AttributeSet attrs) {
    super(context, attrs);
}

@Override
protected void init() {
    findViewById(R.id.tvUser).setVisibility(View.GONE);
}
}

其他用户类:

public class UserProfile extends Profile {

public UserProfile(Context context, AttributeSet attrs) {
    super(context, attrs);
}

@Override
protected void init() {
    findViewById(R.id.tvOther).setVisibility(View.GONE);
}
public class OtherProfile extends Profile {

public OtherProfile(Context context, AttributeSet attrs) {
    super(context, attrs);
}

@Override
protected void init() {
    findViewById(R.id.tvUser).setVisibility(View.GONE);
}
}


现在你知道我的意思了吗?

一个活动需要用户id,如果它是-1,加载当前用户如果不加载id。然后如果你需要两个不同的布局,添加两个SetContentView并尝试保持id相同。那么80%(甚至更多)的布局是相同的?如果它是一个活动,我应该做很多检查,如果(当前用户)为什么?你可以使用“一般变量”我的意思是..它们之间的区别是什么?如果它们显示相同的内容,则将所有内容概括化,只需更改来源我的配置文件有“设置”和“点”按钮。用户配置文件有“添加朋友”和“消息”按钮“。我的个人资料有能力更改用户的头像。用户个人资料有能力查看和评论头像。等等……有功能上的不同。这样我就必须两次查找DVIEWBYID,两次拥有用户名、用户性别等。我有没有办法重用活动的大部分代码