如何在 Fragment class 中访问 setAdapter?
How to access setAdapter in Fragment class?
我有一个关注 Fragment
class:
public class TabFragment extends Fragment {
....
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_photos_tab, container, false);
TabelAdapter adapt = new TabelAdapter(getActivity().getApplicationContext(), R.layout.list_room_view, flowerList);
setListAdapter(adapt); // Not Working because class is not extended by ListActivity
return view;
}
TabelAdapter class
public class TabelAdapter extends ArrayAdapter<Flower> {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (null == convertView) {
convertView = inflater.inflate(R.layout.list_flower_view, parent, false);
}
Picasso
.with(context)
.load("http://i.imgur.com/rFLNqWI.jpg")
.fit()
.into((ImageView) convertView);
return convertView;
}
list_flower_view.xml
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/Flowerimg"
android:layout_width="match_parent"
android:layout_height="200dp"/>
activity_home.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" tools:context=".MainActivity">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@android:id/list"></ListView>
</RelativeLayout>
当我使用 activity
而不是 fragment
时,整个过程非常简单。我用 ListActivity
扩展 MainActivity
class 并将 setContentView(R.layout.activity_home);
添加到 MainActivity.OnCreate()
中。我可以在 ListView
.
中看到图像列表
问题
如何使 setListAdapter
在 Fragment
class 中可用?我是 Android
的新手。任何帮助将不胜感激:)
编辑-1
public class PagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
public PagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
TabFragment photo = new TabFragment();
return photo; // Line (30,24)
} ......
}
如果我使用 ListFragment
而不是 Fragment
,那么 PagerAdapter
class os 会抛出错误。
Error:(30, 24) error: incompatible types: TabFragment cannot be converted to Fragment
编辑-2
java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
将Fragment
改为ListFragment
:
public class TabFragment extends ListFragment {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
TabelAdapter adapt = new TabelAdapter(getActivity().getApplicationContext(), R.layout.list_room_view, flowerList);
setListAdapter(adapt);
}
...
}
我有一个关注 Fragment
class:
public class TabFragment extends Fragment {
....
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_photos_tab, container, false);
TabelAdapter adapt = new TabelAdapter(getActivity().getApplicationContext(), R.layout.list_room_view, flowerList);
setListAdapter(adapt); // Not Working because class is not extended by ListActivity
return view;
}
TabelAdapter class
public class TabelAdapter extends ArrayAdapter<Flower> {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (null == convertView) {
convertView = inflater.inflate(R.layout.list_flower_view, parent, false);
}
Picasso
.with(context)
.load("http://i.imgur.com/rFLNqWI.jpg")
.fit()
.into((ImageView) convertView);
return convertView;
}
list_flower_view.xml
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/Flowerimg"
android:layout_width="match_parent"
android:layout_height="200dp"/>
activity_home.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" tools:context=".MainActivity">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@android:id/list"></ListView>
</RelativeLayout>
当我使用 activity
而不是 fragment
时,整个过程非常简单。我用 ListActivity
扩展 MainActivity
class 并将 setContentView(R.layout.activity_home);
添加到 MainActivity.OnCreate()
中。我可以在 ListView
.
问题
如何使 setListAdapter
在 Fragment
class 中可用?我是 Android
的新手。任何帮助将不胜感激:)
编辑-1
public class PagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
public PagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
TabFragment photo = new TabFragment();
return photo; // Line (30,24)
} ......
}
如果我使用 ListFragment
而不是 Fragment
,那么 PagerAdapter
class os 会抛出错误。
Error:(30, 24) error: incompatible types: TabFragment cannot be converted to Fragment
编辑-2
java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
将Fragment
改为ListFragment
:
public class TabFragment extends ListFragment {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
TabelAdapter adapt = new TabelAdapter(getActivity().getApplicationContext(), R.layout.list_room_view, flowerList);
setListAdapter(adapt);
}
...
}