如何找到没有 id 的 fragment (framelayout)?
这是我的代码:
private void setRect(int _x, int _y, int _w, int _h){
final Rect rect = new Rect(_x,_y,_w,_h);
final PSPDFConfiguration configuration = new PSPDFConfiguration.Builder(BuildConfig.PSPDFKIT_LICENSE_KEY)
.scrollDirection(PageScrollDirection.HORIZONTAL)
.build();
final Uri assetFile = Uri.parse("file:///android_asset/psp.pdf");
final FrameLayout frameLayout = new FrameLayout(thisContext);
runOnUiThread(new Thread(new Runnable() {
@Override
public void run() {
frameLayout.setId(View.generateViewId());
FrameLayout.LayoutParams params;
params = new FrameLayout.LayoutParams(rect.right-rect.left,rect.bottom-rect.top);
params.leftMargin = rect.left;
params.topMargin = rect.top;
params.gravity = 0;
frameLayout.setLayoutParams(params);
viewer_one.setLayoutParams(params);
viewer_one.getSettings().setBuiltInZoomControls(true);
viewer_one.setBackgroundColor(Color.BLUE);
thisActivity.addContentView(viewer_one,params);
PSPDFFragment fragment;
FragmentManager manager = getSupportFragmentManager();
fragment = (PSPDFFragment) manager.findFragmentById(frameLayout.getId());
if(fragment == null){
fragment = PSPDFFragment.newInstance(assetFile,configuration);
getSupportFragmentManager()
.beginTransaction()
.replace(frameLayout.getId(), fragment)
.commit();
}
}
}));
}
我的布局 xml 中没有使用框架布局。
更新:
我已更新代码并收到此错误 -> 找不到 fragment PSPDFFragment 的 ID 0x1(未知)的 View {373f9470
最佳答案
Set an ID to a view programatically
通过代码分配 ID(以编程方式)
使用someView.setId(int);手动设置id
int 必须是正数,但在其他方面是任意的 - 它可以是你想要的任何值(如果这很可怕,请继续阅读。)
例如,如果创建代表项目的多个 View 并对其进行编号,您可以使用它们的项目编号。
这应该可以做到。现在您有了一个 ID,将其用于 fragment 管理器。希望这会有所帮助。
关于java - 如何在android中使用没有id的Fragment?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42246539/