从列表视图项中的按钮调用片段

时间:2018-04-25 13:32:23

标签: android fragment android-dialogfragment

我在ListView中有一个Fragment,在我的ListView项目中,我有Button如何从另一个Fragment调用自定义DialogFragment点击按钮并保留项目信息?

2 个答案:

答案 0 :(得分:1)

<强>逻辑/步骤

在列表项中设置Click Listener。

步骤1: - 在DialogFragment / CustomDialog中创建构造函数以从ListItem的OnClick传递信息。

第2步: - 在Dialog Class中想要你想要的。

答案 1 :(得分:1)

你可以使用界面, 在Adapter类中创建一个接口,然后从owner片段和用户单击Button调用接口Method

进行设置

适配器类:

public class TestAdapter extends RecyclerView.Adapter<TestAdapter.ViewHolder> {


private ArrayList<AddressModel> dataset;
private WeakReference<Activity> activity;
private onChecked listener;

public TestAdapter(Activity activity, ArrayList<AddressModel> datasett) {
    this.dataset = datasett;
    this.activity = new WeakReference<Activity>(activity);
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View rootView = LayoutInflater.from(activity.get()).inflate(R.layout.item_popup_change_address_step_two, null);
    TextView tv = (TextView) rootView.findViewById(R.id.title_itemPopUp_ChangeAddress_TV);
    tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
    tv.setBackground(ContextCompat.getDrawable(parent.getContext(), R.drawable.edittext_style));
    RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    rootView.setLayoutParams(lp);

    return new ViewHolder(rootView);
}

@Override
public void onBindViewHolder(ViewHolder holder, final int position) {

    holder.title.get().setText(dataset.get(position).getTitle());

    holder.rootView.get().setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (listener != null) //call interface 
                listener.chekedListener(dataset.get(position));
        }
    });
}


@Override
public int getItemCount() {
    if (dataset == null)
        return 0;
    return dataset.size();
}

public void setListener(onChecked listener) {
    this.listener = listener;
}

public interface onChecked {
    void chekedListener(AddressModel addressClass_serialized);
}

public class ViewHolder extends RecyclerView.ViewHolder {

    private WeakReference<TextView> title;
    private WeakReference<LinearLayout> rootView;

    public ViewHolder(View itemView) {
        super(itemView);

        title = new WeakReference<TextView>((TextView) itemView.findViewById(R.id.title_itemPopUp_ChangeAddress_TV));
        rootView = new WeakReference<LinearLayout>((LinearLayout) itemView.findViewById(R.id.rootCardView_addressItemPopUP_cd));
    }
}

}

片段中的

adapter.setListener(new TestAdapter.onChecked() {
        @Override
        public void chekedListener(AddressModel addressClass_serialized) {
            //Call Dialog Here
        }
    });