RecyclerView问题
在热门头条中加载不出来RecyclerView问题
实现的代码如下:
声明recyclerview组件
<android.support.v7.widget.RecyclerView android:background="#000" android:layout_below="@id/find_hot_news_line" android:id="@+id/recyclerView_find_news_list" android:layout_width="match_parent" android:layout_height="wrap_content"> </android.support.v7.widget.RecyclerView>
子项布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#fff" android:orientation="horizontal"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="vertical"> <TextView android:id="@+id/news_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="从广州去到东莞仅需要半个小时,美食美景统统都在等着你" android:textColor="#000" android:layout_marginTop="15dp"/> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp"> <TextView android:id="@+id/news_author" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="源自:羊城晚报" android:textColor="#969696" android:layout_marginRight="5dp"/> <ImageView android:id="@+id/news_likesIcon" android:layout_width="20dp" android:layout_height="20dp" android:src="@mipmap/find_hot_yes" android:layout_marginLeft="50dp"/> <TextView android:id="@+id/readersNumber" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="15" android:textColor="#969696" android:layout_marginRight="5dp"/> <ImageView android:id="@+id/readersIcon" android:layout_width="20dp" android:layout_height="20dp" android:src="@mipmap/find_hot_eye" android:layout_marginRight="5dp" android:layout_marginLeft="5dp"/> <TextView android:id="@+id/news_likes_number" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="20" android:textColor="#969696" android:layout_marginLeft="5dp"/> </LinearLayout> </LinearLayout> <ImageView android:id="@+id/news_Icon" android:layout_width="100dp" android:layout_height="90dp" android:src="@mipmap/find_hot_city" /> </LinearLayout>
适配器:
package com.example.vincent.mooctravel.adapter; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import com.example.vincent.mooctravel.R; import com.example.vincent.mooctravel.entity.news; import java.util.List; public class HotNewsAdapter extends RecyclerView.Adapter<HotNewsAdapter.ViewHolder>{ private List<news> newsList; static class ViewHolder extends RecyclerView.ViewHolder { ImageView imageView_newsIcon; ImageView imageView_readersIcon; ImageView imageView_likesIcon; TextView textView_title; TextView textView_author; TextView textView_likesNumber; TextView textView_readersNumber; public ViewHolder(View itemView) { super(itemView); imageView_likesIcon=itemView.findViewById(R.id.news_likesIcon); imageView_newsIcon=itemView.findViewById(R.id.news_Icon); imageView_readersIcon=itemView.findViewById(R.id.readersIcon); textView_title=itemView.findViewById(R.id.news_title); textView_author=itemView.findViewById(R.id.news_author); textView_likesNumber=itemView.findViewById(R.id.news_likes_number); textView_readersNumber=itemView.findViewById(R.id.readersNumber); } } public HotNewsAdapter(List<news> newsList) { this.newsList = newsList; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.item_find_hot_news, parent,false); ViewHolder holder=new ViewHolder(view); return holder; } @Override public void onBindViewHolder(ViewHolder holder, int position) { news n=newsList.get(position); holder.textView_readersNumber.setText(n.getReadersNumber()); holder.textView_likesNumber.setText(n.getLikesNumber()); holder.textView_author.setText(n.getAuthor()); holder.textView_title.setText(n.getTitle()); holder.imageView_readersIcon.setImageResource(n.getReadersIcon()); holder.imageView_newsIcon.setImageResource(n.getEssayIcon()); holder.imageView_likesIcon.setImageResource(n.getLikesIcon()); } @Override public int getItemCount() { return newsList.size(); } }
发现模块fragment:
package com.example.vincent.mooctravel.fragment; import android.app.Fragment; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.example.vincent.mooctravel.R; import com.example.vincent.mooctravel.adapter.HotNewsAdapter; import com.example.vincent.mooctravel.entity.news; import com.example.vincent.mooctravel.Utils.dataUtil; import java.util.List; /** * Created by vincent on 2018/2/9/0009. */ public class FindFragment extends android.support.v4.app.Fragment { private RecyclerView recyclerView; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_find,container,false); } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); recyclerView=getView().findViewById(R.id.recyclerView_find_news_list); recyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); HotNewsAdapter adapter=new HotNewsAdapter(dataUtil.getNewsList()); recyclerView.setAdapter(adapter); } }
获取构建适配器所需的List列表的静态方法
public static List<news> getNewsList(){ String title[]={"从广州去到东莞仅需要半个小时,美食美景统统都在等着你", "旅行前最应该准备的事件事项,千万别给忽略了", "北京周边竟然藏着20个人间仙境,你绝对不能错过"}; String author[]={"源自:羊城攻略","源自:冬夏旅游","源自:北京攻略"}; int essayIcon[]={R.mipmap.find_hot_city,R.mipmap.find_hot_home,R.mipmap.find_hot_jiangnan}; int readersIcon=R.id.readersIcon; int likesIcon=R.id.news_likesIcon; String readersNumber []={"12","15","16"}; String likesNumber []={"9","2","5"}; List<news> newsList=new ArrayList(); for(int i=0;i<3;i++){ news n=new news(title[i],author[i],essayIcon[i], readersIcon,likesIcon,readersNumber[i],likesNumber[i]); newsList.add(n); } return newsList; }
14
收起
正在回答 回答被采纳积分+1
2回答
慕粉1935552329
2018-02-24 22:22:19
已找到原因 原来是静态方法中点赞图片和阅读数图片初始化出错
慕粉1935552329
2018-02-22 19:31:47
不知道什么原因 在运行的时候点击发现页 出现黑屏然后退回到闪屏页
Android零基础入门2018版
- 参与学习 人
- 提交作业 5461 份
- 解答问题 7238 个
此次推出的专题为Android攻城狮培养计划的第一部分语法与界面基础篇,将带大家从0开始学习Android开发。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星