如何添加的ImageView viewPager在ListFragment在android系统? [英] How to add ImageView viewPager in ListFragment in android?

查看:166
本文介绍了如何添加的ImageView viewPager在ListFragment在android系统?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ViewPager 的ListView android的屏幕:

I have a ViewPager inside ListView in android screen:

<?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>

我ListFragment类

本类调用 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);
}
}

FoodListAdapter

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;
}

SlidingImageAdapter

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 列表视图布局类?

Question

How can I merge slidingImageAdapter int FoodListAdapter. So that I can add viewpager in Listview in layout class?

推荐答案

在一般我会建议你移动到的 RecyclerView

In general I would suggest you to move to RecyclerView.

下面你可以找到你的当前实现简单的解决方案。

Below you can find simple solution for your current implementation.

更改为 FoodListAdapter

改变食物的清单来图,其中食物是标识和相关SlidingImageAdapter就是价值。

Change list of food to map, where Food is id and related SlidingImageAdapter is value.

private Map<Food, SlidingImageAdapter> foodMap;

这将避免适配器的再创造每个getView调用。

This will avoid recreating of adapter every getView call.

@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。不要忘了覆盖散code()等于(对象o)食物项。

这篇关于如何添加的ImageView viewPager在ListFragment在android系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆