嵌套片段并未完全被子片段取代

时间:2017-12-02 02:01:39

标签: android android-fragments

美好的一天。当用户点击任何卡片视图元素时,我想用新片段替换现有片段。但是当用户从第一个片段中点击一张卡时,它只会显示在现有片段的顶部。希望你能给我更正或指导,以便我能达到预期的效果。

此致 新手:)

MainActivity.java

else if (id == R.id.nav_receivable) {
                // Handle the Loan application();
                ReceivableFragment receivable= new ReceivableFragment();
                FragmentManager manager= getSupportFragmentManager();
                manager.beginTransaction()
                        .replace(R.id.content_main_relativelayout_for_fragment, receivable) //content_main.xml
                        .commit();

                Toast.makeText(MainActivity.this, "AR and Loan Accounts", Toast.LENGTH_SHORT).show();

            }

MyAdapter.java

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

private List<Datas> mDataset;
private ClickListener mListener;

public interface ClickListener {
    void onItemClicked(Datas datas);
}

// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public static class MyViewHolder extends RecyclerView.ViewHolder {

    public CardView mCardView;
    public TextView account_type;
    public TextView accnt_description;
    public TextView balance_label;
    public TextView account_balance;



    public MyViewHolder(View v) {
        super(v);

        mCardView = (CardView) v.findViewById(R.id.account_card_view);

        account_type = (TextView) v.findViewById(R.id.lblShareCapital);
        balance_label = (TextView) v.findViewById(R.id.lblAvailableBalance);

        accnt_description = (TextView) v.findViewById(R.id.sl_desc);
        account_balance = (TextView) v.findViewById(R.id.actual_balance);
    }
}
// Provide a suitable constructor (depends on the kind of dataset)
public MyAdapter(List<Datas> myDataset, ClickListener listener) {
    mDataset = myDataset;
    mListener = listener;
}

// Create new views (invoked by the layout manager)
@Override
public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,
                                                 int viewType) {
    // create a new view
    View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.card_item, parent, false);
    // set the view's size, margins, paddings and layout parameters
        MyViewHolder vh = new MyViewHolder(v);
    return vh;
}

@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
    Datas datas= mDataset.get(position);

    holder.accnt_description.setText(datas.getSL_DESC());
    holder.account_balance.setText(datas.getACTUAL_BALANCE());

    holder.mCardView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Datas datas= mDataset.get(position);

            holder.accnt_description.setText(datas.getSL_DESC());
            holder.account_balance.setText(datas.getACTUAL_BALANCE());

            String sle = mDataset.get(position).getSLE();
            String slc = mDataset.get(position).getSLC();
            String slt = mDataset.get(position).getSLT();
            String ref_no = mDataset.get(position).getREF();
            String tr_date= mDataset.get(position).getTR_DATE();

            Log.d("MyAdapter", "Cardview Elements" );
            Log.d("CardView Clicked", "sle code: " + sle);
            Log.d("CardView Clicked", "slc code: " + slc);
            Log.d("CardView Clicked", "slt code: " + slt);
            Log.d("CardView Clicked", "ref num:  "  + ref_no);

            Toast.makeText(view.getContext(), "Transaction date: " +tr_date, Toast.LENGTH_SHORT).show();

            mListener.onItemClicked(datas);
        }
    });
}

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

ReceivableFragment

public class ReceivableFragment extends Fragment {

public ReceivableFragment() {
    // Required empty public constructor
}

MyAdapter.ClickListener listener= new MyAdapter.ClickListener() {
    @Override
    public void onItemClicked(Datas datas) {
        //datas.getTR_DATE();

        Log.d("MyAdapter.ClickListener", "Ref No: "+datas.getREF() );
        Log.d("MyAdapter.ClickListener", "TR DATE: "+datas.getTR_DATE() );

        FragmentTransaction ft= getChildFragmentManager().beginTransaction();
        SLDTLFragment sldtl= new SLDTLFragment();
        ft.replace(R.id.account_card_view, sldtl);
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        ft.addToBackStack(null);
        ft.commit();


    }
};

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootView = inflater.inflate(R.layout.fragment_receivable, container, false);
    RecyclerView rv = (RecyclerView) rootView.findViewById(R.id.rv_recycler_view_fragment_receivable); //fragment_receivable.xml
    rv.setHasFixedSize(true);
    SQLiteHandler db = new SQLiteHandler(getActivity());

    MyAdapter adapter = new MyAdapter(db.getUserSLARLoans(), listener);
    rv.setAdapter(adapter);

    LinearLayoutManager llm = new LinearLayoutManager(getActivity());
    rv.setLayoutManager(llm);

    return rootView;
}

}

SLDTLFragment.java

public class SLDTLFragment extends Fragment {


public SLDTLFragment() {
    // Required empty public constructor
}

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_sldtl, container, false);
}

}

当用户点击其中一个卡片视图元素时,应该打开一个新的片段的样本日历视图。

这是我的代码

的结果

enter image description here

1 个答案:

答案 0 :(得分:0)

片段需要在活动中出现的容器用其他片段替换自己。所以你可以用单个容器替换片段。

尝试1:
替换此行

SLDTLFragment sldtl= new SLDTLFragment();
ft.replace(R.id.account_card_view, sldtl);

SLDTLFragment sldtl= new SLDTLFragment();
ft.replace(R.id.content_main_relativelayout_for_fragment, sldtl); //content_main_relativelayout_for_fragment is your fragment container in activity.

尝试2:

替换此行

SLDTLFragment sldtl= new SLDTLFragment();
ft.replace(R.id.account_card_view, sldtl);

SLDTLFragment sldtl= new SLDTLFragment();
ft.replace(R.id.R.id.fragment_receivable, sldtl); //fragment_receivable is your receivable fragment rootview.

<强> EDITED /更新

您可以在android中使用bundle在片段之间传递数据。使用此

活动类(放置值)

Bundle bundle=new Bundle();
bundle.putString("key", "value");
SLDTLFragment sldtl= new SLDTLFragment();
sldtl.setArguments(bundle);
ft.replace(R.id.content_main_relativelayout_for_fragment, sldtl); 

片段类(获取值)

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
  String data = getArguments().getString("key");

return inflater.inflate(R.layout.fragment, container, false);
}

希望它有所帮助。!