首页 文章

使用HorizontalScroll列表的自定义适配器以及Android中的其他适配器

提问于
浏览
-1

我有问题 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;
    }

...和它的XML布局(相对布局):

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

支持片段包含我的列表视图:

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

如果我 grab 下一个例外:

E / AndroidRuntime:FATAL EXCEPTION:main java.lang.ClassCastException:android.widget.RelativeLayout $ LayoutParams无法强制转换为android.widget.AbsListView $ LayoutParams

在两个情况下(当在xml文件中使LinearLayout可见,并且可以通过ID查找时)我 grab :

E / AndroidRuntime:FATAL EXCEPTION:main java.lang.IllegalStateException:HorizontalScrollView只能在CNewsAdapter.getView(CNewsAdapter.java:68)的android.widget.HorizontalScrollView.addView(HorizontalScrollView.java:211)中托管一个直接子节点 .

What I have to do to make it work?

2 回答

  • 0

    解 . 我的问题在于属性

    android:layout_above =“@ id / tv_news_likes_count”

    ... HorizontalScrollView . 在案例3中,它初始化,但我没有看到 .

  • 0

    Problem :

    在XML文件中,您已将 LinearLayout 添加了 ll_news_photosH.S.V .

    现在再次在 getView() 方法中尝试将另一个布局添加到 H.S.V . 这里

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

    扔了

    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 :

    您应该将动态 LinearLayout 添加到

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

    所以在代码中它会

    ll_news_photos.addView(layout);
    

    代替

    hr.addView(layout);
    

相关问题