【发布时间】:2018-09-18 21:48:09
【问题描述】:
我目前正在 Android 中使用 Fragments 进行试验,这让我抓狂。我在一个包含项目(书籍)列表的片段中有一个 ListView。当我单击列表中的一个项目时,片段应该被换成另一个包含该项目详细信息的片段。但是片段没有出现,尽管 setOnItemClickListener() 方法确实有效(我有 logcat 消息确认 ListView 中的项目已被单击。我还将另一条 logcat 消息放入第二个片段的 onCreateView() 方法中,所以Fragment 似乎已创建,但并未膨胀...
这里是 FFBooksFragment,包含项目 ListView 的 Fragment:
package com.example.android.gamebookcollector;
public class FFBooksFragment extends Fragment {
private static final String TAG = "FFBooksFragment";
private Button ffButton;
private CustomListAdapter adapter;
private ListView booksList;
private static final int STANDARD_APPBAR = 0;
private static final int SEARCH_APPBAR = 1;
private int mAppbarState;
private AppBarLayout viewBooksToolbar, searchBar;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_ffbooks, container, false);
viewBooksToolbar = (AppBarLayout) view.findViewById(R.id.viewBooksToolbar);
searchBar = (AppBarLayout) view.findViewById(R.id.searchBooksToolbar);
booksList = (ListView) view.findViewById(R.id.booksList);
setUpGamebooksList();
ImageView searchIcon = (ImageView) view.findViewById(R.id.ivSearchIcon);
searchIcon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "Clicked search icon in standard bar");
toggleToolbarState();
}
});
ImageView ivBackArrow = (ImageView) view.findViewById(R.id.ivBackArrow);
ivBackArrow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "Clicked back arrow in search bar");
toggleToolbarState();
}
});
return view;
}
private void toggleToolbarState() {
if (mAppbarState==STANDARD_APPBAR) {
setAppbarState(SEARCH_APPBAR);
}else {
setAppbarState(STANDARD_APPBAR);
}
}
private void setAppbarState(int state) {
Log.d(TAG, "Changing app bar state to " + state);
mAppbarState = state;
if(mAppbarState==STANDARD_APPBAR){
searchBar.setVisibility(View.GONE);
viewBooksToolbar.setVisibility(View.VISIBLE);
View view = getView();
InputMethodManager inputMethodManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
try{
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}catch(NullPointerException e){
Log.d(TAG, "setAppbar state: Null pointer exception. " + e.getMessage());
}
}
else if (mAppbarState==SEARCH_APPBAR){
viewBooksToolbar.setVisibility(View.GONE);
searchBar.setVisibility(View.VISIBLE);
InputMethodManager inputMethodManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
}
private void setUpGamebooksList(){
final ArrayList<Gamebook> gameBooks = new ArrayList<>();
gameBooks.add(new Gamebook("1. Book title", "Book authors", "Publisher", "Year", R.drawable.image1));
gameBooks.add(new Gamebook("2. Book title", "Book authors", "Publisher", "Year", R.drawable.image2));
gameBooks.add(new Gamebook("3. Book title", "Book authors", "Publisher", "Year", R.drawable.image3));
gameBooks.add(new Gamebook("4. Book title", "Book authors", "Publisher", "Year", R.drawable.image4));
adapter = new CustomListAdapter(getActivity(), R.layout.layout_book_list_item, gameBooks, "drawable://");
/* Now we call the setAdapter() method on our booksList list and pass it our adapter. */
booksList.setAdapter(adapter);
// Set up an onItemClickListener for checking to see if any book in the list is clicked on.
// If so, go to the book fragment (which is blank for now).
booksList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d(TAG, "Navigating to book fragment...");
BookFragment bookFragment = new BookFragment();
FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragmentContainer, bookFragment);
// adding a string identifier string to the fragment, not really necessary right now but might as well do it
fragmentTransaction.addToBackStack(getString(R.string.book_fragment));
fragmentTransaction.commit();
}
});
}
@Override
public void onResume() {
super.onResume();
setAppbarState(STANDARD_APPBAR);
}
}
这里是BooksFragment,当点击上述Fragment的ListView中的一个项目时应该会出现:
package com.example.android.gamebookcollector;
public class BookFragment extends Fragment {
private static final String TAG = "BookFragment";
private android.support.v7.widget.Toolbar toolbar;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_book, container, false);
toolbar = (Toolbar) view.findViewById(R.id.booksToolbar);
Log.d(TAG, "Book fragment started...");
// Set up the toolbar
((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
setHasOptionsMenu(true);
ImageView ivBackArrow = (ImageView) view.findViewById(R.id.ivBackArrow);
ivBackArrow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "Back arrow clicked");
// pop the previous fragment off the back stack (i.e. navigate back)
getActivity().getSupportFragmentManager().popBackStack();
}
});
/* If the edit button is clicked, go to the edit book fragment */
ImageView ivEdit = (ImageView) view.findViewById(R.id.iv_edit);
ivEdit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "Clicked the edit button...");
EditBookFragment editBookFragment = new EditBookFragment();
FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragmentContainer, editBookFragment);
// adding a string identifier string to the fragment, not really necessary right now but might as well do it.
// We can retrieve fragments by using their string identifer.
fragmentTransaction.addToBackStack(getString(R.string.edit_book_fragment));
fragmentTransaction.commit();
}
});
return view;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.book_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menuitem_delete:
Log.d(TAG, "Delete book...");
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
}
【问题讨论】:
-
您应该尝试从将放置两个片段的父活动管理此过程。
-
您认为这可能是问题所在吗?
-
我想是的。我现在不知道该片段是否可以添加另一个片段,因为上下文在活动中。如果第二个片段在第一个片段内,情况就不一样了。
-
贴出的代码使用
getActivity().getSupportFragmentManager(),表示该fragment由activity的fragmentmanager管理。所以我怀疑通过活动会解决问题。 -
话虽如此,一般来说,更好的做法是让片段只对它的活动说话,而不是对另一个片段说话,一个片段创建另一个片段非常接近跨越这条线。
标签: android listview android-fragments