通过片段重新加载列表视图

时间:2015-01-12 15:27:39

标签: android listview android-fragments

我对使用片段相对较新,并且很难弄清楚如何重新加载我在BaseFragment中初始化的列表视图,同时我在FragmentActivity我正在使用通用图像使用listview的加载器并使其在我的项目中正常工作,我只需要弄清楚如何在不重新加载整个活动的情况下重新加载它。

这是我的 Basefragment ,我相信listview首次初始化:

public class ImageListFragment extends AbsListViewBaseFragment {

    public static final int INDEX = 0;
    String[] imageUrls = Constants2photo.IMAGES.toArray(new String[Constants2photo.IMAGES.size()]);

    DisplayImageOptions options;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.ic_stub)
                .showImageForEmptyUri(R.drawable.ic_empty)
                .showImageOnFail(R.drawable.ic_error)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .considerExifParams(true)
                .displayer(new RoundedBitmapDisplayer(20))
                .build();

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fr_image_list, container, false);
        listView = (ListView) rootView.findViewById(R.id.lister);
        ((ListView) listView).setAdapter(new ImageAdapter());
        listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                startImagePagerActivity(position);
            }
        });
        return rootView;
    }
...

这是我的片段活动 onCreate:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    int frIndex = getIntent().getIntExtra(Constants2photo.Extra.FRAGMENT_INDEX, 0);
    Fragment fr;
    String tag;
    int titleRes;
    switch (frIndex) {
        default:
        case ImageListFragment.INDEX:
            tag = ImageListFragment.class.getSimpleName();
            fr = getSupportFragmentManager().findFragmentByTag(tag);
            if (fr == null) {
                fr = new ImageListFragment();
            }
            titleRes = R.string.ac_name_image_list;
            break;
        case ImagePagerFragment.INDEX:
            tag = ImagePagerFragment.class.getSimpleName();
            fr = getSupportFragmentManager().findFragmentByTag(tag);
            if (fr == null) {
                fr = new ImagePagerFragment();
                fr.setArguments(getIntent().getExtras());
            }
            titleRes = R.string.ac_name_image_pager;
            break;

    }

    setTitle(titleRes);
    getSupportFragmentManager().beginTransaction().replace(android.R.id.content, fr, tag).commit();
}

编辑

图像适配器类:

class ImageAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();

    ImageAdapter() {
        inflater = LayoutInflater.from(getActivity());
    }

    @Override
    public int getCount() {
        return imageUrls.length;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = convertView;
        final ViewHolder holder;
        if (convertView == null) {
            view = inflater.inflate(R.layout.item_list_image, parent, false);
            holder = new ViewHolder();
            holder.text = (TextView) view.findViewById(R.id.text);
            holder.image = (ImageView) view.findViewById(R.id.image);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        holder.text.setText("Item " + (position + 1));

        ImageLoader.getInstance().displayImage(imageUrls[position], holder.image, options, animateFirstListener);

        return view;
    }
}

那么如何从片段活动中重新加载listview?让我知道如果我需要发布更多代码,真的坚持这个。

2 个答案:

答案 0 :(得分:1)

在你的片段中做这样的事情

注意似乎您正在使用来自通用图像加载器(nostra)的示例。在示例中,他们使用的AbsListView方法返回getAdapter,而ListAdapter不属于notifyDataSetChanged()。这就是您需要将适配器转换为BaseAdapter

的原因
public void refresh()
{
    ((BaseAdapter)this.listView.getAdapter()).notifyDataSetChanged();
}

并在您的活动中刷新

Fragment fr = getSupportFragmentManager().findFragmentById(R.id.content);
if(fr instanceOf ImageListFragment)
    ((ImageListFragment)fr).refresh();

答案 1 :(得分:0)

ImageListFragment中,您可以创建一个方法,例如:

public void updateList(){
  this.ListView.getAdapter().notifyDataSetChanged();
  ...other stuff you wanna do to refresh your list....

}

然后在您的活动中,您可以随时刷新片段中的列表:

fr = new ImageListFragment();

...

fr.updateList();