如何实现一个ListFragment成从样本项目中? [英] How to implements a ListFragment into your project from a Sample?
问题描述
我有一个寻呼机适配器,将调用这样的ListFragment:
I have a Pager adapter that will call a ListFragment like this:
public Fragment getItem(int position) {
Fragment fragment = new ListViewFragment();
// set arguments here, if required
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
然后,我有,我想换ListViewFragment一个ListActivity。
Then I have a ListActivity that I want to change to ListViewFragment.
public class ImageListActivity extends ListActivity implements RadioGroup.OnCheckedChangeListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.imagelist);
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
radioGroup.setOnCheckedChangeListener(this);
setListAdapter(new ImageAdapter());
}
public void onCheckedChanged(RadioGroup group, int checkedId) {
ImageDownloader.Mode mode = ImageDownloader.Mode.NO_ASYNC_TASK;
if (checkedId == R.id.correctButton) {
mode = ImageDownloader.Mode.CORRECT;
}else if (checkedId == R.id.randomButton) {
mode = ImageDownloader.Mode.NO_DOWNLOADED_DRAWABLE;
}
((ImageAdapter) getListAdapter()).getImageDownloader().setMode(mode);
}
}
但我真的不能把它的工作..香港专业教育学院尝试过:
But I really cant put it to work.. Ive tried that:
public class ListViewFragment extends ListFragment {
int mNum;
Context ctx;
View v;
static int p;
/**
* Create a new instance of CountingFragment, providing "num"
* as an argument.
*/
static ListFragment newInstance(int num) {
ListFragment f = new ListFragment();
// Supply num input as an argument.
Bundle args = new Bundle();
args.putInt("num", num);
f.setArguments(args);
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
v = inflater.inflate(R.layout.imagelist, container, false);
ImageDownloader.Mode mode = ImageDownloader.Mode.NO_ASYNC_TASK;
mode = ImageDownloader.Mode.CORRECT;
((ImageAdapter) getListAdapter()).getImageDownloader().setMode(mode);
setListAdapter(new ImageAdapter());
return container;
}
}
更新: - 我只是回答我的问题,我做一个小教程解释初学者如何实现一个ListAdapter成从样本项目
UPDATE: - I just answer my own question and I make a little tutorial to explain beginners how to implements a ListAdapter into your project from a sample.
从来就得到这个ListAdapter从样品,而只是将文件复制到我的项目,如果你运行它,就会崩溃。
I´ve got this ListAdapter from a sample, and just copy the files to my project, if you run it, will crash.
所以,你需要按照我的回答,并提出了修改和机具任何 ListArrayAdapter
您在网上找到。
So, you need to follow my answer and made the changes and implements whatever ListArrayAdapter
that you found in internet.
推荐答案
最后从来就穿上它的工作。
Finally I´ve put it to work.
嗯,首先你需要更正从呼叫 PagerAdapter
。
Well, first you need to correct the call from PagerAdapter
.
public Fragment getItem(int position) {
Fragment fragment = new FragmentListArraySupport.ArrayListFragment();
// set arguments here, if required
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
在 FragmentListArraySupport
是 FragmentActivity
和 ArrayListFragment
是在 ListFragment
。
这是 FragmentListArraySupport
code:
public class FragmentListArraySupport extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(SampleList.THEME); //Used for theme switching in samples
super.onCreate(savedInstanceState);
// Create the list fragment and add it as our sole content.
if (getSupportFragmentManager().findFragmentById(android.R.id.content) == null) {
ArrayListFragment list = new ArrayListFragment();
getSupportFragmentManager().beginTransaction().add(android.R.id.content, list).commit();
}
}
public static class ArrayListFragment extends ListFragment {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ImageDownloader.Mode mode = ImageDownloader.Mode.CORRECT;
ImageAdapter imageAdapter = new ImageAdapter();
imageAdapter.getImageDownloader().setMode(mode);
setListAdapter(imageAdapter);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
Log.i("FragmentList", "Item clicked: " + id);
}
}
}
请参阅您需要更改 ListActivity
到 FragmentActivity
现在,你有你的形象适配器工作,所以无论 ListArrayAdapter
您身边的互联网找到你只需要复制类文件,调用ArraySupport在 PagerAdapter
并修改 ListActivity
到 FragmentActivity
。
See that you need to change ListActivity
to FragmentActivity
Now you have your image adapter working, so whatever ListArrayAdapter
that you found around internet you just need to copy the class files, call the ArraySupport in the PagerAdapter
and change the ListActivity
to FragmentActivity
.
这篇关于如何实现一个ListFragment成从样本项目中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!