gpt4 book ai didi

android - fragment 出现在 Activity 上

转载 作者:行者123 更新时间:2023-11-30 00:17:47 25 4
gpt4 key购买 nike

我想在单击 ListView 中的项目时显示一个 fragment 。 ListView 在主要 Activity 中。问题是 fragment 出现在 Activity ListView 上。你知道问题出在哪里吗?

主要 Activity :

public class MainActivity extends AppCompatActivity {

ArrayList<String> test = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ListView listView = (ListView) findViewById(R.id.listView);

test.add("item 1");
test.add("item 2");

ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, test);
listView.setAdapter(arrayAdapter);

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
EditorFragment editorFragment = new EditorFragment();

getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, editorFragment).commit();
}
});
}
**activity main xml**

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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">

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="0dp"
android:layout_height="0dp"
tools:layout_editor_absoluteY="8dp"
tools:layout_editor_absoluteX="8dp" />

<ListView
android:id="@+id/listView"
android:layout_width="368dp"
android:layout_height="495dp"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp" />
</android.support.constraint.ConstraintLayout>

fragment 编辑器xml

<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/editor"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="top|left"
android:inputType="text"
android:hint="@string/hint"/>

EditorFragment

public class EditorFragment extends Fragment {

private EditText editor=null;


@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View result=inflater.inflate(R.layout.fragment_editor, container, false);

editor=(EditText) (result.findViewById(R.id.editor));

return result;

}
}

我在 fragment 中使用了 removeAllViews 方法,但是当点击项目时它不起作用, fragment 没有出现。

public class EditorFragment extends Fragment {

private EditText editor=null;


@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View result=inflater.inflate(R.layout.fragment_editor, container, false);

if(container != null)
{
container.removeAllViews();
}

editor=(EditText) (result.findViewById(R.id.editor));

return result;

}
}

最佳答案

我不确定是否能解决问题,但也许您可以尝试在不使用任何 fragment 时将容器可见性设置为不可见,并在需要时显示它。

关于android - fragment 出现在 Activity 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46921177/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com