HorizontalScrollView as part of ListView: CustomAdapter error
-
Hey! I have a problem with the implementation of the ListView, one of whose elements is horizontal photos in an unknown number (in the JSON line from the server). CustomAdapter:
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;
I'd like to see you.
<?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>
Well, actually, it's a fragment:
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;
}
}
I've tried to play with the code, three things happen:
- The mistake where the compiler argues that I add "child" to H.S.V., and he can only have one heir.
- Mistake : android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
He also sold LinearLayout as the primary for xml-fail, not helping - the same mistake. - The project is successfully launched, but it does not display HorizontalScrollView. I wanted to solve the problem with the flow of fragments (in the swing, item'a in ListView), but I realized it wasn't welcomed. Can you tell me how to decide?
-
(1) Better show me the wrong code. Why don't you run the picture cycle where all the staters are called, but only if convertViewyn null? (2) Why do you create LinearLayout every time? It is understandable that HorizontalScrollView requires internal layout-a, but is it not easier to place it in the mark through xml? Then put it in the adapter in ViewHolder, and you'll be able to bury your pictures.