gpt4 book ai didi

android - 无法用另一个 fragment 替换一个 fragment

转载 作者:行者123 更新时间:2023-11-29 18:12:27 24 4
gpt4 key购买 nike

即使我一直在阅读有关我的问题的其他主题,我也无法找出我的问题的原因。

我有一个 Activity ,左边有一个 ListView。右侧是一个 ListFragment,您可以在 activity_question.xml 中看到它:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:orientation="horizontal" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3"
android:orientation="vertical" >

<TextView
android:id="@+id/textViewCategoryTitle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="10"
android:background="@drawable/in_progress_background"
android:gravity="center" />

<ListView
android:id="@+id/listViewQuestionsGroup"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>

<fragment
android:id="@+id/fragmentQuestions"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
class="com.firm.bonjour.visite.QuestionsFragment" />
</LinearLayout>

<Button
android:id="@+id/buttonValidate"
style="@style/ButtonsValidation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:text="@string/validate" />

</LinearLayout>

我已经为 fragment 的 ListView 创建了一个适配器:

public class QuestionsAdapter extends BaseAdapter {

private QuestionsBean questions = null;
private LayoutInflater inflater = null;

public QuestionsAdapter(LayoutInflater inflater) {
this.inflater = inflater;
}

@Override
public int getCount() {
return (questions != null && questions.getQuestionList() != null) ? questions.getQuestionList().size() : 0;
}

@Override
public Object getItem(int position) {
return (questions != null && questions.getQuestionList() != null) ? questions.getQuestionList().get(position) : null;
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;

if (convertView == null) {
holder = new ViewHolder();

convertView = inflater.inflate(R.layout.item_fragment_question, null);

holder.question = (TextView) convertView.findViewById(R.id.questionsLayout);

convertView.setTag(holder);

} else {
holder = (ViewHolder) convertView.getTag();
}

holder.question.setText(((QuestionsBean) questions).getQuestionList().get(position).getQuestion());

return convertView;
}

private class ViewHolder {
TextView question;
}

public void setQuestions(QuestionsBean questions) {
this.questions = questions;
notifyDataSetChanged();
}

}

当我启动 Activity 时,我选择左侧 ListView 的第一项以在 Fragment 中显示详细信息。单击左侧 ListView 的其中一项后, fragment 不会更改。当我在 Debug模式时,我可以看到适配器获得了正确的对象来显示。

您知道为什么新 fragment 没有显示吗?

我的 Activity 代码:

public class QuestionsActivity extends Activity implements OnItemClickListener {

public static final String QUESTIONS_LIST = "questionsList";
public static final String QUESTIONS = "questions";

private CategoryBean category;

private ListView questionsGroupListView;

private Fragment questionsFragment;

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

category = (CategoryBean) getIntent().getExtras().get(MainActivity.CATEGORY);

questionsGroupListView = (ListView) findViewById(R.id.listViewQuestionsGroup);
QuestionListAdapter adapter = new QuestionListAdapter(this, category);
questionsGroupListView.setAdapter(adapter);
questionsGroupListView.setOnItemClickListener(this);

if (getFragmentManager().findFragmentById(R.id.fragmentQuestions) == null) {
questionsFragment = new QuestionsFragment();
getFragmentManager().beginTransaction().add(R.layout.activity_questions, questionsFragment).commit();
} else {
questionsFragment = getFragmentManager().findFragmentById(R.id.fragmentQuestions);
}
}

/**
* Called when a number gets clicked
*/
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
displayFragment((QuestionsBean)questionsGroupListView.getItemAtPosition(position));
}

@Override
protected void onResume() {
super.onResume();
if(questionsFragment != null && category.getCategoryList() != null && !category.getCategoryList().isEmpty()) {
((QuestionsFragment)questionsFragment).refresh((QuestionsBean)category.getCategoryList().get(0));
} else {
Log.v("BUGGGGG", "PAS NORMAL !!!");
}
}

/**
* Add a Fragment to our stack with n Androids in it
*/
private void displayFragment(QuestionsBean questions) {
Fragment fragment = QuestionsFragment.newInstance(questions);

FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragmentQuestions, fragment);
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
fragmentTransaction.commit();
}

}

我的 ListFragment 代码:

public class QuestionsFragment extends ListFragment {

private LayoutInflater inflater;
private QuestionsAdapter adapter;
private QuestionsBean questions;

/**
* Create a new instance of DetailsFragment, initialized to show the text at 'index'.
*/
public static QuestionsFragment newInstance(QuestionsBean questions) {
QuestionsFragment questionFragment = new QuestionsFragment();

// Supply index input as an argument.
Bundle args = new Bundle();
args.putSerializable(QuestionsActivity.QUESTIONS, questions);
questionFragment.setArguments(args);

return questionFragment;
}

public int getShownIndex() {
return getArguments() != null ? getArguments().getInt("index", 0) : 0;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
this.inflater = inflater;
this.adapter = new QuestionsAdapter(inflater);

View questionsView = (LinearLayout) inflater.inflate(R.layout.layout_fragment_question, null);
setListAdapter(adapter);

return questionsView;
}

@Override
public void onResume() {
super.onResume();
if (getArguments() != null && getArguments().getSerializable(QuestionsActivity.QUESTIONS) != null) {
this.questions = (QuestionsBean) getArguments().getSerializable(QuestionsActivity.QUESTIONS);
adapter.setQuestions((QuestionsBean) getArguments().getSerializable(QuestionsActivity.QUESTIONS));
} else {
adapter.setQuestions(questions);
}
}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);

}

public void refresh(QuestionsBean questions) {
if (getArguments() != null && getArguments().getSerializable(QuestionsActivity.QUESTIONS) != null) {
this.questions = (QuestionsBean) getArguments().getSerializable(QuestionsActivity.QUESTIONS);
} else {
this.questions = questions;
}
}

}

我现在真的被困了好几个小时......非常感谢任何帮助:)

最佳答案

首先,由于您正在使用 fragment 的 Argument 包来存储数据,因此请确保它可以正确序列化 - 检查嵌入的 questions.getQuestionList() 是否不为 null 或其他内容。

如果每次单击 listItem 时都需要一个新的 fragment ,那么您最好将参数 bean 直接存储为 fragment 的一个属性,至少作为一种临时措施,直到您发现问题所在。

public static QuestionsFragment newInstance(QuestionsBean questions) {
QuestionsFragment questionFragment = new QuestionsFragment();
questionFragment.questions = questions;
return questionFragment;
}

其次,尝试协调 fragment 的 onResume() 中的刷新代码和 fragment 的 refresh() 中的刷新代码,也许让 onResume 调用 refresh()。还要确保您的 refresh() 代码更新了您的适配器,以便您可以实际显示任何新值。如果您放弃使用 Argument Bundle(至少在您找到错误之前),代码会变得简单得多。

@Override
public void onResume() {
super.onResume();
refresh(this.questions);
}

public void refresh(QuestionsBean questions) {
this.questions = questions;
adapter.setQuestions(questions);
}

最后一步是您的 Activity 代码 - 尝试删除所有在 displayFragment 方法之外提及 QuestionsFragment 的代码。只需从 Activity 的 onResume() 和 listItemClick 事件中调用 displayFragment。

使用这种代码,新的 QuestionFragment 将由 Activity 的 onCreate 的 LayoutInflater 分配一次,然后在 onResume 期间替换为默认类别问题,然后在每次单击后替换为所选类别。然后,您可以更轻松地跟踪真正发生的事情,并可能从那里开始构建?

从那时起,我将亲自调整 displayFragment 方法以检查 QuestionFragment 是否存在,然后刷新它而不是用新的替换它。

祝你好运,告诉我们进展如何!

关于android - 无法用另一个 fragment 替换一个 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9960530/

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