1

我有一个名为的活动,MainPage它扩展了SherlockFragmentActivity. 这个活动有标签,每个标签显示不同的片段。其中一个片段显示了SaleRow一个自定义视图(扩展类的RelativeLayout类)的视图。我也有SaleDialog扩展的类DialogFragment。我想做的是从SaleRow视图类中显示 SaleDialog 。我尝试使用此代码:

public class SaleRow extends RelativeLayout 
{   
    public SaleRow(Context context) 
    {
        super(context);

        ...

        this.setOnClickListener(new OnClickListener() 
        {
            @Override
            public void onClick(View view) 
            {
            FragmentManager fm = getFragmentManager(); //compilation error here for getFragmentManager The method getFragmentManager() is undefined for the type new View.OnClickListener()
                SaleDialog testDialog = new SaleDialog();
                testDialog.setRetainInstance(true);
                testDialog.show(fm, "fragment_name");
            }

        });

我正在寻找解决方案,但找不到相关的东西。

塔克斯

4

3 回答 3

6

尝试保留对您的context对象的引用,对其进行强制转换,然后调用getSupportFragmentManager它:

public class SaleRow extends RelativeLayout 
{
    private Context mContext;
    public SaleRow(Context context) 
    {
        super(context);
        mContext = context;
        this.setOnClickListener(new OnClickListener() 
        {
            @Override
            public void onClick(View view) 
            {
                try{
                    FragmentManager fm = ((FragmentActivity) mContext).getSupportFragmentManager();
                } catch (ClassCastException e) {
                   Log.d(TAG, "Can't get fragment manager frmom context");
                }
                SaleDialog testDialog = new SaleDialog();
                testDialog.setRetainInstance(true);
                testDialog.show(fm, "fragment_name");
            }

        });
    }
}
于 2013-05-21T23:06:10.890 回答
0

尝试SomeActivity.getFragmentManager();

于 2013-05-19T11:33:11.687 回答
0

在我看来,试图从视图影响片段管理器的状态是一个坏主意。我更喜欢做的是只允许父Activity级操作片段管理器。这样,视图与活动分离,活动可以决定它想做什么以及它想如何做。所以我会在 中监听 click 事件,然后通过回调接口Fragment将该事件传递给父级:Activity

以下内容Fragment包含SaleRow

// Step 1 - Create callback interface
Callback mCallback;

public interface Callback {

    public void showSaleDialog();
}

// Step 2 - Cast the parent Activity to the callback
@Override
public void onAttach(Activity activity) {

    try {
        mCallback = (Callback) activity;
    }
    catch (ClassCastException e) {
        throw new ClassCastException(activity.toString() + " must implement Callback");
    }
}

// Step 3 - Set the listener on the SaleRow in the onCreateView() class
saleRow.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {

        // Step 4 - call showSaleDialog() using the callback
        mCallback.showSaleDialog();
    }
});

然后在Activity

public class MyActivity extends SherlockFragmentActivity implements Callback {

    @Override
    public void showSaleDialog() {

        FragmentManager fm = getSupportFragmentManager();
        SaleDialog testDialog = new SaleDialog();
        testDialog.setRetainInstance(true);
        testDialog.show(fm, "fragment_name");
    }
于 2013-05-26T03:53:10.753 回答