我在android屏幕中ViewPager
内有ListView
:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/Base.TextAppearance.AppCompat.Menu"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true" />
</RelativeLayout>
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="55dip"
android:layout_alignParentEnd="false"
android:layout_alignParentStart="false"
android:layout_below="@+id/imageView"
android:autoText="false"
android:background="#FFFFFF"
android:gravity="center_vertical"
android:padding="10dp"
android:text="08/01 MidTown"
android:textColor="#21252D"
android:textSize="20sp" />
</RelativeLayout>
此课程正在调用ArrayAdapter
将数据加载到上面Textview
。
public class FoodTabFragment extends ListFragment {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getListView().setDivider(null);
final AppDelegate contextScope = (AppDelegate) getActivity().getApplicationContext();
FoodListAdapter adapt = new FoodListAdapter(getActivity().getApplicationContext(), R.layout.layout_listing_food, contextScope.foodList);
setListAdapter(adapt);
}
}
public class FoodListAdapter extends ArrayAdapter<Food> {
private Context context;
private List<Food> foodList;
public FoodListAdapter(Context context, int resource, List<Food> objects) {
super(context, resource, objects);
this.context = context;
this.roomList = objects;
}
@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.layout_listing_food, parent, false);
}
return convertView;
}
public class SlidingImageAdapter extends PagerAdapter {
private ArrayList<Integer> IMAGES;
private LayoutInflater inflater;
private Context context;
public SlidingImageAdapter(Context context, ArrayList<Integer> IMAGES) {
this.context = context;
this.IMAGES=IMAGES;
inflater = LayoutInflater.from(context);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public int getCount() {
return IMAGES.size();
}
@Override
public Object instantiateItem(ViewGroup view, int position) {
View imageLayout = inflater.inflate(R.layout.framelayout_listing_image_slider, view, false);
assert imageLayout != null;
final ImageView imageView = (ImageView) imageLayout
.findViewById(R.id.image);
imageView.setImageResource(IMAGES.get(position));
view.addView(imageLayout, 0);
return imageLayout;
}
如何合并slidingImageAdapter
int FoodListAdapter
。这样我就可以在viewpager
类的Listview
中添加layout
吗?
答案 0 :(得分:0)
一般情况下,我建议您转到RecyclerView。
您可以在下面找到当前实施的简单解决方案。
FoodListAdapter
的更改:
更改要映射的食物列表,其中Food是id且相关的SlidingImageAdapter是值。
private Map<Food, SlidingImageAdapter> foodMap;
这将避免每次getView调用重新创建适配器。
@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.layout_listing_food, parent, false);
}
Food food = getItem(position);
SlidingImageAdapter adapter;
// adapter creation or reuse
if (foodMap.contains(food)) {
adapter = foodMap.get(food);
} else {
adapter = new SlidingImageAdapter(...);
foodMap.put(food, adapter);
}
convertView.findViewById(R.id.pager).setAdapter(adapter);
return convertView;
}
P.S。不要忘记在hashCode()
条目中覆盖equals(Object o)
和Food
。