-1

There is my problematic Custom Adapter with HorizontalScroll for ListView:

public class CNewsAdapter extends BaseAdapter {
    private static ArrayList<News> listNews;
    private LayoutInflater layoutInflater;
    Context context;

    public CNewsAdapter(Context fragment, ArrayList<News> results) {
        listNews = results;
        layoutInflater = LayoutInflater.from(fragment);
        context = couponFragment;
    }

    @Override
    public int getCount() {
        return listNews.size();
    }

    @Override
    public Object getItem(int position) {
        return listNews.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = layoutInflater.inflate(R.layout.item_news, null);
            holder.title = (TextView) convertView.findViewById(R.id.tv_news_title);
            holder.text = (TextView) convertView.findViewById(R.id.tv_news_text);
            holder.dateNTime = (TextView) convertView.findViewById(R.id.tv_news_time_difference);
            holder.likesCount = (TextView) convertView.findViewById(R.id.tv_news_likes_count);
            holder.newsLike = (AppCompatImageButton) convertView.findViewById(R.id.ib_news_heart_btn);
            HorizontalScrollView hr = (HorizontalScrollView) convertView.findViewById(R.id.hs_news_scroll_photo);
           // LinearLayout layout = (LinearLayout) convertView.findViewById(R.id.ll_news_photos);
            LinearLayout layout = new LinearLayout(context);
            for (int i = 0; i < listNews.get(position).getUrlPhotos().size(); i++) {
                holder.image = new ImageView(context);
                holder.image.setLayoutParams(new LinearLayoutCompat.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

                layout.addView(holder.image);
                Ion.with(holder.image).placeholder(R.drawable.ic_waiting).error(R.drawable.ic_error).load(listNews.get(position).getUrlPhotos().get(i).toString());
            }
            hr.addView(layout);
            convertView = hr;

            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.title.setText(listNews.get(position).getTitle());
        holder.text.setText(listNews.get(position).getText());
        holder.dateNTime.setText(listNews.get(position).getDifferenceTime());
        holder.likesCount.setText(Integer.toString(listNews.get(position).getLikeCount()));
        holder.newsLike.setImageResource(R.drawable.icon_heart_filled_20);


        return convertView;
    }


    static class ViewHolder {
        ImageView image;
        public TextView title;
        public TextView text;
        public TextView dateNTime;
        public TextView likesCount;
        public AppCompatImageButton newsLike;
    }

... And XML-layout for it (Relative Layout):

> <?xml version="1.0" encoding="utf-8"?> <RelativeLayout
> xmlns:android="http://schemas.android.com/apk/res/android"
>     android:layout_width="match_parent" android:layout_height="match_parent">
> 
>     <TextView
>         android:layout_width="wrap_content"
>         android:layout_height="wrap_content"
>         android:textAppearance="?android:attr/textAppearanceMedium"
>         android:text="News!"
>         android:id="@+id/tv_news_title"
>         android:layout_alignParentTop="true"
>         android:layout_alignParentLeft="true"
>         android:layout_alignParentStart="true" />
> 
>     <TextView
>         android:layout_width="wrap_content"
>         android:layout_height="wrap_content"
>         android:textAppearance="?android:attr/textAppearanceSmall"
>         android:text="News description"
>         android:id="@+id/tv_news_text"
>         android:layout_below="@+id/tv_news_title"
>         android:layout_alignParentLeft="true"
>         android:layout_alignParentStart="true" />
>     <HorizontalScrollView
>         android:id="@+id/hs_news_scroll_photo"
>         android:layout_width="match_parent"
>         android:layout_height="match_parent"
>         android:layout_below="@+id/tv_news_text"
>         android:layout_above="@+id/tv_news_likes_count"
>         >
> <!--        <LinearLayout
>       android:id="@+id/ll_news_photos"
>       android:isScrollContainer="true"
>       android:orientation="horizontal"
>       android:layout_width="match_parent"
>       android:layout_height="match_parent" />-->
>     </HorizontalScrollView>
>     <TextView
>         android:layout_width="wrap_content"
>         android:layout_height="wrap_content"
>         android:textAppearance="?android:attr/textAppearanceMedium"
>         android:text="Medium Text"
>         android:id="@+id/tv_news_likes_count"
>         android:layout_alignParentBottom="true"
>         android:layout_toLeftOf="@+id/ib_news_heart_btn"
>         android:layout_toStartOf="@+id/ib_news_heart_btn"
>         android:layout_alignTop="@+id/ib_news_heart_btn"
>         android:gravity="center_vertical|center_horizontal" />
> 
>     <android.support.v7.widget.AppCompatImageButton
>         android:layout_width="40dp"
>         android:layout_height="40dp"
>         android:id="@+id/ib_news_heart_btn"
>         android:layout_alignParentBottom="true"
>         android:layout_alignParentRight="true"
>         android:layout_alignParentEnd="true"
>         android:contentDescription="Like it!" />
> 
>     <TextView
>         android:layout_width="wrap_content"
>         android:layout_height="wrap_content"
>         android:textAppearance="?android:attr/textAppearanceSmall"
>         android:text="How many time later"
>         android:id="@+id/tv_news_time_difference"
>         android:layout_alignParentTop="true"
>         android:layout_alignParentRight="true"
>         android:layout_alignParentEnd="true" /> </RelativeLayout>

There is Support Fragment contains my List View:

class CNewsFragment extends Fragment {
ListView lv;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fr_c_news_layout, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        lv = (ListView) view.findViewById(R.id.lv_news_main_list);
        CNewsAdapter newsAdapter = new CNewsAdapter(view.getContext(), API.getListNews());
        lv.setAdapter(newsAdapter);
        newsAdapter.notifyDataSetChanged();

    }
private class API {
  public static ArrayList<News> getListNews() {
        ArrayList<News> result = new ArrayList<>();
        LinkedList<String> urlPhotos = new LinkedList<>();
        urlPhotos.add("https://pp.vk.me/c625321/v625321630/40940/mrKdWrYpMYQ.jpg");
        urlPhotos.add("https://pp.vk.me/c625321/v625321630/408ee/El-e0P99_jI.jpg");
        urlPhotos.add("https://pp.vk.me/c625321/v625321630/40939/asXUtRC4r_8.jpg");
        urlPhotos.add("https://pp.vk.me/c625321/v625321630/40940/mrKdWrYpMYQ.jpg");
        News news = new News("uid1", "News 1!", "Description1", "2016-07-20T16:30:00.580", 103, true, urlPhotos);
        result.add(news);
        urlPhotos = new LinkedList<>();
        urlPhotos.add("https://pp.vk.me/c625321/v625321630/40940/mrKdWrYpMYQ.jpg");
        urlPhotos.add("https://pp.vk.me/c625321/v625321630/408ee/El-e0P99_jI.jpg");
        urlPhotos.add("https://pp.vk.me/c625321/v625321630/40939/asXUtRC4r_8.jpg");
        urlPhotos.add("https://pp.vk.me/c625321/v625321630/40940/mrKdWrYpMYQ.jpg");
        news = new News("uid2", "News2!", "News Descript", "2016-07-20T16:30:00.580", 1000500, true, urlPhotos);
        result.add(news);
        return result;
       }
   }
}

In case one I catch next Exception:

E/AndroidRuntime: FATAL EXCEPTION: main java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams

In case two (when makes LinearLayout is visible in xml-file, and makes it possible to find by ID) I catch:

E/AndroidRuntime: FATAL EXCEPTION: main java.lang.IllegalStateException: HorizontalScrollView can host only one direct child at android.widget.HorizontalScrollView.addView(HorizontalScrollView.java:211) at CNewsAdapter.getView(CNewsAdapter.java:68)

What I have to do to make it work?

  • What is the case 1 and case 2?? – curiousMind Dec 07 '15 at 06:30
  • honestly, I happened 3 case in this problem: 1. Exception when was detected another-one "child" 2. Exception when : android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams 3. When project creating with all elements, with the exception of H.S.V. - it in invisible, i didn't see it. :( There is all code, what I can provide for simulate it – Lvov Dmitry Dec 07 '15 at 07:43

2 Answers2

0

Problem :

In XML file you have added LinearLayout with the id of ll_news_photos to H.S.V.

Now again in getView() method you'r trying to add another layout to H.S.V. Here

LinearLayout layout = new LinearLayout(context);
hr.addView(layout);

which throws

E/AndroidRuntime: FATAL EXCEPTION: main java.lang.IllegalStateException: HorizontalScrollView can host only one direct child at android.widget.HorizontalScrollView.addView(HorizontalScrollView.java:211) at CNewsAdapter.getView(CNewsAdapter.java:68)

Solution :

You should add your dynamic LinearLayout to

  <LinearLayout
    android:id="@+id/ll_news_photos"
    android:isScrollContainer="true"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />-->

So in code it will

ll_news_photos.addView(layout);

instead of

hr.addView(layout);
Piyush
  • 18,895
  • 5
  • 32
  • 63
0

Solution. My problem were in attribute

android:layout_above="@+id/tv_news_likes_count"

...of HorizontalScrollView. In case 3 it initialized, but i did'nt see that.