我试图通过ListFragments实现子类别,并且我没有清除前一个片段列表的问题,并且onbackpress没有显示父列表片段。
示例这是我的父ListFragment:
package com.best.photo.now;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.best.photo.now.R;
import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;
public class CategoryListFragment extends ListFragment {
private static final String TAG = CategoryListFragment.class.getSimpleName();
private Context mContext = null;
private String[] mCategories = null;
private CategoryListAdapter mAdapter = null;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//send screen name to google
Tracker t = ((WallpaperApplication) getActivity().getApplication()).getTracker();
t.setScreenName(MainActivity.CATEGORY);
t.send(new HitBuilders.AppViewBuilder().build());
GoogleAnalytics.getInstance(getActivity()).dispatchLocalHits();
mContext = getActivity();
mCategories = getActivity().getResources().getStringArray(R.array.category_list);
mAdapter = new CategoryListAdapter();
setListAdapter(mAdapter);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
System.out.println("Position onClick: "+position);
if(position==2)
{
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragment_container, new AbstractListFragment());
ft.commit();
}
else
{
super.onListItemClick(l, v, position, id);
((OnCategoryListItemClickListern)getActivity()).onCategoryListItemClick(v);
}
}
private class CategoryListAdapter extends BaseAdapter{
@Override
public int getCount() {
return mCategories.length;
}
@Override
public Object getItem(int position) {
return mCategories[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
if(view == null)
view = View.inflate(mContext, R.layout.category_list_item, null);
((TextView)(view.findViewById(R.id.category_name))).setText(mCategories[position]);
((ImageView)(view.findViewById(R.id.category_image))).setImageResource(R.drawable.ic_launcher);
view.setTag(mCategories[position]);
return view;
}
}
public static interface OnCategoryListItemClickListern{
public abstract void onCategoryListItemClick(View v);
}
}
列表是 所有类别 最爱 抽象 动物
等......
在onListItemClick上,打开下一个片段
与第一个片段相同,除了它的onListItemClick有父母点击监听器。
package com.best.photo.now;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.best.photo.now.R;
import com.best.photo.now.CategoryListFragment.OnCategoryListItemClickListern;
import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;
public class AbstractListFragment extends ListFragment {
private static final String TAG = AbstractListFragment.class.getSimpleName();
private Context mContext = null;
private String[] mAbstract = null;
private AbstractListAdapter mAdapter = null;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//send screen name to google
Tracker t = ((WallpaperApplication) getActivity().getApplication()).getTracker();
t.setScreenName(MainActivity.CATEGORY);
t.send(new HitBuilders.AppViewBuilder().build());
GoogleAnalytics.getInstance(getActivity()).dispatchLocalHits();
mContext = getActivity();
mAbstract = getActivity().getResources().getStringArray(R.array.abstract_list);
mAdapter = new AbstractListAdapter();
setListAdapter(mAdapter);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
((OnCategoryListItemClickListern)getActivity()).onCategoryListItemClick(v);
}
private class AbstractListAdapter extends BaseAdapter{
@Override
public int getCount() {
return mAbstract.length;
}
@Override
public Object getItem(int position) {
return mAbstract[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
if(view == null)
view = View.inflate(mContext, R.layout.abstract_list_item, null);
((TextView)(view.findViewById(R.id.abstract_name))).setText(mAbstract[position]);
((ImageView)(view.findViewById(R.id.abstract_image))).setImageResource(R.drawable.ic_launcher);
view.setTag(mAbstract[position]);
return view;
}
}
public static interface OnAbstractListItemClickListern{
public abstract void onAbstractListItemClick(View v);
}
}
点击列表中的任何项目,查询来自flickr的搜索api并获取相关结果。哪个工作正常,但第二个片段列表未清除,并显示在图像结果的背景中。
此外,在背压时,我无法访问父列表碎片。
我在调用第二个片段时尝试使用addToBackStack(null),但它对我没用。
我在这里缺少什么?
此外,还有其他方法可以实现ListFragments的子类别,而不是为每个子类别类型创建多个ListFragments类/布局吗?
非常感谢任何帮助!!
谢谢, ARNAB
答案 0 :(得分:0)
而不是替换尝试调用add function:
ft.add(R.id.fragment_container, new AbstractListFragment());
ft.addToBackStack(null);
ft.commit();