即使我一直在阅读有关我的问题的其他主题,但我无法弄清楚我的问题的原因。
我有一个活动,左边有一个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创建了一个Adapter:
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中的第一个项目以显示片段中的细节。 一旦我点击左侧ListView的其中一个项目,片段就不会改变。 当我在debugg mod中时,我可以看到Adapter获得了要显示的正确对象。
你知道为什么新片段没有显示吗?
我的活动代码:
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;
}
}
}
我现在真的被困了好几个小时......真的很感激任何帮助:)
答案 0 :(得分:3)
首先,由于您正在使用片段的Argument包来存储数据,因此请确保它是可正确序列化的 - 检查嵌入的questions.getQuestionList()是否为null或其他内容。
如果您每次单击listItem时都要使用新的片段,那么您最好将参数bean直接存储为片段的属性,至少作为临时措施,直到找到错误。
public static QuestionsFragment newInstance(QuestionsBean questions) {
QuestionsFragment questionFragment = new QuestionsFragment();
questionFragment.questions = questions;
return questionFragment;
}
其次,尝试协调片段的onResume()中的刷新代码和片段的refresh()中的刷新代码,也许有onResume调用refresh()。还要确保您的refresh()代码更新了适配器,以便您可以实际显示任何新值。如果你放弃使用Argument Bundle(至少在找到你的bug之前),代码会变得更加简单。
@Override
public void onResume() {
super.onResume();
refresh(this.questions);
}
public void refresh(QuestionsBean questions) {
this.questions = questions;
adapter.setQuestions(questions);
}
并且最后一步是您的活动代码 - 尝试删除在displayFragment方法之外提及QuestionsFragment的所有代码。 只需从您的活动的onResume()和listItemClick事件中调用displayFragment。
使用这种代码,一个新的QuestionFragment将由活动的onCreate的LayoutInflater分配一次,然后在onResume期间替换为默认类别问题,然后在每次单击所选类别后替换。然后,您可以更轻松地跟踪实际发生的情况,并可能从那里开始构建?
从那时起,我会亲自调整displayFragment方法以检查QuestionFragment是否存在,然后只刷新它而不是用新的替换它。
祝你好运,告诉我们它是怎么回事!答案 1 :(得分:0)
我猜这是因为你在使用newInstance方法时返回一个片段,而不是一个视图,而FragmentTransaction就是在改变视图。
以下是我将如何做到这一点......
要调用右手片段,我会将onItemClick更改为:
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
QuestionsFragment fragment = new QuestionsFragment(((QuestionsBean)QuestionsGroupListView.getItemAtPosition(position));
getFragmentManager().beginTransaction().replace(R.id.fragmentQuestions, fragment).commit();
}
然后我会更改QuestionsFragment。
删除newInstance方法并使用newInstance中的代码插入构造函数:
public QuestionsFragment(QuestionBean questions){
// Supply index input as an argument.
Bundle args = new Bundle();
args.putSerializable(QuestionsActivity.QUESTIONS, questions);
questionFragment.setArguments(args);
}
这样当你调用新片段时,它将获取你的参数并继续创建视图,然后返回它,这就是你需要的。
希望这有帮助!