片段未在适配器类中替换

时间:2018-08-05 07:26:39

标签: android android-fragments

我正在尝试用适配器类中的一个按钮单击侦听器替换另一个片段。编译器将转到onClickListener,但该片段不会替换。

@OnClick(R.id.ib_view)
        public void gotoTranscationDetailsFragment() {
            Toast.makeText(mParent, "Hi", Toast.LENGTH_SHORT).show();
            Fragment viewSuspendedTransactionFragment = new ViewSuspendedTransactionFragment();
            FragmentManager manager = mParent.getSupportFragmentManager();
            FragmentTransaction transaction = manager.beginTransaction();
            transaction.replace(R.id.fragment_suspend_transaction, viewSuspendedTransactionFragment);
            transaction.commit();
        }

完整代码

public class SuspendedTransactionListAdapter extends RecyclerView.Adapter<SuspendedTransactionListAdapter.SuspendedTransactionListHolder> {

    private SuspendTransactionActivity mParent;
    private List<SuspendTransactionFragment.SampleDataSuspendTransactionActivity> sampleList;
    private Fragment fragment;

    public SuspendedTransactionListAdapter(SuspendTransactionActivity mParent, List<SuspendTransactionFragment.SampleDataSuspendTransactionActivity> sampleList, Fragment fragment) {
        this.mParent = mParent;
        this.sampleList = sampleList;
        this.fragment = fragment;
    }

    @Override
    public SuspendedTransactionListHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_suspended_transaction_list, parent, false);
        return new SuspendedTransactionListHolder(itemView);
    }

    @Override
    public void onBindViewHolder(SuspendedTransactionListHolder holder, int position) {
        SuspendTransactionFragment.SampleDataSuspendTransactionActivity sampleDataSuspendTransactionActivity = sampleList.get(position);
        holder.mTransactionNoTextView.setText(sampleDataSuspendTransactionActivity.getTransactionNo());
    }

    @Override
    public int getItemCount() {
        return sampleList.size();
    }

    class SuspendedTransactionListHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.ib_view)
        ImageButton mViewImageButton;

        public SuspendedTransactionListHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }

        @OnClick(R.id.ib_view)
        public void gotoTranscationDetailsFragment() {
            Toast.makeText(mParent, "Hi", Toast.LENGTH_SHORT).show();
            Fragment viewSuspendedTransactionFragment = new ViewSuspendedTransactionFragment();
            FragmentManager manager = mParent.getSupportFragmentManager();
            FragmentTransaction transaction = manager.beginTransaction();
            transaction.replace(R.id.fragment_suspend_transaction, viewSuspendedTransactionFragment);
            transaction.commit();
        }
    }

XML-活动

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".transaction.activities.SuspendTransactionActivity">

    <fragment
        android:id="@+id/fragment_suspend_transaction"
        class="com.sahasram.siripos.fragments.SuspendTransactionFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/actionbar"
        tools:layout="@layout/fragment_suspend_transaction" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

如果使用标记Fragment在xml中定义<fragment>,则它是View层次结构的固定组成部分,您将无法删除/替换它。如果要替换Fragment,则应定义可替换的布局(用作Fragment的容器)。因此,您的Activity布局可能如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".transaction.activities.SuspendTransactionActivity">

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/actionbar" />
</RelativeLayout>

然后您可以将其替换为:

@OnClick(R.id.ib_view)
public void gotoTranscationDetailsFragment() {
    Toast.makeText(mParent, "Hi", Toast.LENGTH_SHORT).show();
    Fragment viewSuspendedTransactionFragment = new ViewSuspendedTransactionFragment();
    FragmentManager manager = mParent.getSupportFragmentManager();
    FragmentTransaction transaction = manager.beginTransaction();
    transaction.replace(R.id.fragment_container, viewSuspendedTransactionFragment);
    transaction.commit();
}

有关更多信息,请查看here