【问题标题】:How to identify a fragment from it's button's xml onClick view?如何从按钮的 xml onClick 视图中识别片段?
【发布时间】:2013-11-07 18:50:46
【问题描述】:

我有一些片段正在以不同的标签以编程方式添加。每个都有一个带有 xml onClick 的按钮,该按钮被传递一个视图。如何识别从该视图中单击了哪个片段?代码如下。

NewFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.new_fragment, container, false);

    TextView tv = (TextView) view.findViewById(R.id.name);
    Bundle bundle = this.getArguments();
    int index = bundle.getInt("index");
    tv.setText(Integer.toString(index));

    return view;
}

Activity.java

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity);

    FragmentManager fm = getSupportFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();

    for (int loop = 0; loop < 4; loop++) {
        NewFragment fragment = new NewFragment();
        Bundle args = new Bundle();
        args.putInt("index", loop);
        fragment.setArguments(args);
    //tag set to value of loop here
        ft.add(R.id.new_fragment, fragment, Integer.toString(loop));
    }
    ft.commit();
}

public void toggleEdit(View view) {
  //How do I find clicked fragment?
  }

new_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/test">
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Name"
            android:layout_marginLeft="5dp"
            android:padding="5dp"
            />
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/name"
            android:text="Test"
        />
    </LinearLayout>
    <!-- Button in question is below -->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:text="@string/edit"
        android:onClick="toggleEdit"
        android:id="@+id/edit_button"
    />

</LinearLayout>

【问题讨论】:

    标签: java android android-fragments android-view android-button


    【解决方案1】:

    让您的 Fragment 定义一个您的 Activity 实现的接口。给那个接口一个带字符串参数的方法,onClick,传入当前Fragment的标签。

    有关更多详细信息,请参阅此片段通信指南http://developer.android.com/training/basics/fragments/communicating.html

    【讨论】:

    • 谢谢。我现在正在通读它。我希望答案会更简单一些。例如,我试图在 NewFragment 中设置 ID 并从 onClick 的视图中恢复它。但我无法让它工作。
    • 是的。接口是正确的通信方式。任何其他方式都将是相当的黑客,我不希望它运作良好或容易。
    • 接口实际上非常容易做到。
    • 我仍然坚持这一点。如何找到当前片段的标签来传递它?
    • 在片段的一个方法内,调用String tag = this.getTag();如果您在像 OnClickListener 这样的匿名类中,则可能需要调用 String tag = MyFragment.this.getTag();
    猜你喜欢
    • 1970-01-01
    • 2020-01-08
    • 2016-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多