2015-09-28 3 views
0

У меня есть ViewPager внутри ListView в андроида экрана:Как добавить ImageView viewPager в ListFragment в android?

<?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 в Listview в layout класс?

ответ

0

В общем, я предлагаю вам перейти на RecyclerView.

Ниже вы можете найти простые решения для вашей текущей реализации.

Изменение для FoodListAdapter:

Изменить список продуктов питания на карту, где пища 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.

+0

Александр благодарит за ваш ответ, но в соответствии с вашей реализацией он дает мне возможность на основе данных в пищевой карте. Что я wan этих двух адаптеров –

+0

@AmitPal Извините, но я не понимаю ваш комментарий. Не могли бы вы это объяснить? – Oleksandr

+0

Думаю, я вас неправильно понял. Я хочу загрузить 'viewpager' в каждую строку' listview'. Свыше файла 'layout',' ListFrgment' и 'FoodListAdapter' я могу видеть данные в виде списка. После этого я попытался добавить viewpager в файл макета и теперь ищет решение для добавления 'ImageView viewpager' в каждую строку' ListItem'. –

Смежные вопросы