【问题标题】:Fragments getting added on top of each other?片段相互叠加?
【发布时间】:2016-12-30 16:52:48
【问题描述】:

这可能是重复的,但坦率地说,其他问题都没有帮助我。

所以我有一个问题。我正在制作一个联系人 Android 应用程序。当我按联系人查看其号码时,只显示一个号码。通常这很好,因为大多数人没有一个联系人的多个号码。但是当你这样做时,你想看到所有的数字。这是我的问题。我想显示多个数字,但它不起作用。

只显示一个数字,但通过调试,我知道实际数字变量存在,只是没有正确显示。所以这让我相信这两个片段是相互叠加的。是的,它们被添加到的布局是 LinearLayout。

那么,为什么我的片段会被叠加在一起呢?我正在使用 Support Fragments 和 Support Fragment Manager 顺便说一句。几天来,我一直试图弄清楚这一点。老实说,我讨厌直接问,但我不知道这次我做错了什么。因此,如果有人可以在这里帮助我,那就太好了。

代码如下:

ContactActivity.java

//Create an intent
Intent intent = getIntent();
Bundle extras = intent.getExtras();

//Get extras
String contact_title = extras.getString("contact_title");
String[] contact_numbers = extras.getStringArray("contact_numbers");

...
...

//Add numbers
if(contact_numbers != null) {
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction trans = fragmentManager.beginTransaction();

    //Add fragments
    for(int i = 0; i < contact_numbers.length; i++) {
        trans.add(R.id.contact_numbers, ContactNumberFragment.newInstance("contact_number", contact_numbers[i]), "ID: " + Integer.toString(i));
    }

    trans.commit();
}

ContactNumberFragment.java

//Adds an extra
public static ContactNumberFragment newInstance(String name, String text) {
    ContactNumberFragment fragment = new ContactNumberFragment();

    //Add extras
    Bundle bundle = new Bundle();
    bundle.putString(name, text);
    fragment.setArguments(bundle);

    return fragment;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_contact_number, container, false);
    Bundle extras = getArguments();

    String number = extras.getString("contact_number");
    Log.i("NUMBER", number);

    contactNumber_textView = (TextView)v.findViewById(R.id.contactNumber_textView);

    //Set number text
    if(contactNumber_textView != null) {
        contactNumber_textView.setText(number);

        Log.i("TEXT VIEW TEXT", contactNumber_textView.getText().toString());
    }

    return v;
}

activity_contact.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">

    <include
        layout="@layout/toolbar"
        android:id="@+id/toolbar" />

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/contact_numbers"
        android:layout_below="@id/toolbar">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/contactNumberTitle_title"
            android:textColor="@color/textDarkLight"
            android:textSize="@dimen/contactNumberTitle_textSize"
            android:paddingStart="@dimen/mediumPadding"
            android:paddingLeft="@dimen/mediumPadding"
            android:paddingRight="@dimen/mediumPadding"
            android:paddingTop="@dimen/smallPadding"
            android:paddingBottom="@dimen/smallPadding" />
    </LinearLayout>
</RelativeLayout>

fragment_contact_number.xml

<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"
    tools:context="ContactNumberFragment">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/contactNumber_textView"
        android:text=""
        android:textColor="@color/textDark"
        android:textSize="@dimen/contactNumber_textSize"
        android:padding="@dimen/smallPadding" />
</LinearLayout>

【问题讨论】:

  • 正如下面的答案所暗示的,我想知道为什么您需要片段并且不能只创建一个类来处理膨胀视图和视图的逻辑
  • 同意,这里绝对不要使用 Fragments。总是问“我需要一个片段吗?”唯一的答案是肯定的,如果您需要封装需要生命周期回调的逻辑。在 99% 的情况下,View/ViewGroup 会更好并避免头痛。这里你只需要new TextView()
  • 您可以使用片段的替换功能而不是添加功能
  • 好吧,我想在号码旁边添加一个小电话/通话图标,您可以轻松拨打该号码。所以我需要的不仅仅是一个 TextView 添加到数字。但是一个ViewGroup。我从来没想过这点。我将研究 ViewGroups,因为我从未使用过它们。我对 Android 开发很陌生,哈哈。
  • 如果要添加图标,请查看 TextView 的 drawableLeft/Right 属性。

标签: java android android-layout android-fragments


【解决方案1】:

我建议不要在这里使用 Fragment,而是使用自定义 ViewGroup。例如,我使用了 LinearLayout:

public class PhoneView extends LinearLayout {

    private TextView numberView;
    private ImageView phoneIcon;

    public PhoneView(Context context) {this(context, null);}
    public PhoneView(Context context, AttributeSet attrs) {this(context, attrs, 0);}
    public PhoneView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {

        setOrientation(HORIZONTAL);

        LayoutInflater inflater = LayoutInflater.from(getContext());
        View view = inflater.inflate(R.layout.view_phone, this, true);

        numberView = (TextView) view.findViewById(R.id.contact_number);
        phoneIcon = (ImageView) view.findViewById(R.id.phone_icon);

        phoneIcon.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                callNumber();
            }
        });
    }

    public void setContactNumber(String number) {
        numberView.setText(number);
    }

    private void callNumber() {
        //...
    }
}

制作匹配的布局文件view_phone.xml:

<?xml version="1.0" encoding="utf-8"?>
<merge
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/contact_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

    <ImageView
        android:id="@+id/phone_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/icon_phone"
        />

</merge>

然后在您的活动中,您只需要执行以下操作:

public void addNumbers(List<String> contactNumbers) {

    LinearLayout numberLayout = (LinearLayout) findViewById(R.id.contact_numbers);

    for (String number : contactNumbers) {

        PhoneView phoneView = new PhoneView(this);
        phoneView.setContactNumber(number);
        numberLayout.addView(phoneView);
    }
}

【讨论】:

  • 这对我来说是完美的!非常感谢你。虽然,我不太明白这里发生了什么。我将研究代码并进行一些研究,以便在遇到此类问题时再次使用此解决方案。我相信我会大声笑的。再次感谢!
【解决方案2】:

你是对的,你是在旧片段的基础上添加新片段。 trans.add 将片段插入到指定的视图中。 trans.replace 将插入片段替换之前的任何内容,但我猜这不是你想要在这里做的。从字里行间看,您似乎想显示一个数字列表?如果是这种情况,您总是可以按照其他答案的建议将视图添加到现有布局中。 This 问题将为您指明正确的方向。如果没有一个好的旧 ListView 也能很好地完成这项工作。

【讨论】:

    猜你喜欢
    • 2013-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多