RecyclerView运行良好但没有结果

时间:2015-05-13 09:41:51

标签: android

我按照youtube abt <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/img_item" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher"/> <TextView android:id="@+id/title_item" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Dummy Text" android:paddingTop="5dp" android:paddingLeft="15dp" android:textColor="#ff281c9b" /> </LinearLayout> 上的教程编写了代码和适配器,该代码和适配器运行时没有错误但无法按预期显示列表任何帮助请求此区域我的代码

XML

custom_list_row.xml

<?xml version="1.0" encoding="utf-8"?>

    <android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/swipe_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="56dp">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/feed_list"
        android:layout_height="match_parent"
        android:layout_width="wrap_content">

    </android.support.v7.widget.RecyclerView>

    </android.support.v4.widget.SwipeRefreshLayout>

vanguard_news.xml

public class ViewAdapter extends RecyclerView.Adapter <ViewAdapter.MyViewHolder>{
    LayoutInflater inflater;
    List<ReViewInformation> data= Collections.emptyList();

    public ViewAdapter(Context context, List<ReViewInformation> data){
        inflater=LayoutInflater.from(context);
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view=inflater.inflate(R.layout.custom_list_row,parent, false);
        MyViewHolder holder = new MyViewHolder(view);

        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        ReViewInformation currObj = data.get(position);
        holder.title.setText(currObj.Title);
        holder.image.setImageResource(currObj.id);
    }

    @Override
    public int getItemCount() {
        return 0;
    }

    class MyViewHolder extends RecyclerView.ViewHolder{
        TextView title;
        ImageView image;

        public MyViewHolder(View itemView) {
            super(itemView);
            title= (TextView) itemView.findViewById(R.id.title_item);
            image = (ImageView) itemView.findViewById(R.id.img_item);

        }
    }
}

类代码

ViewAdapter.class

public class ReViewInformation {
    int id;
    String Title;
}

ReViewInformation.class

public class _1Vanguard_News extends Fragment implements OnRefreshListener{
    SwipeRefreshLayout swipeLayout;
    RecyclerView recyclerView;
    private ViewAdapter  adapter;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view;
        view = inflater.inflate(R.layout.vanguard_news, container, false);

        //view = inflater.inflate(R.layout.principal, container, false);
        //Recycle list view
        recyclerView = (RecyclerView) view.findViewById(R.id.feed_list);
        adapter = new ViewAdapter(getActivity(),getData());
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        //Swipe to refresh view
        swipeLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_container);
        swipeLayout.setColorSchemeResources(R.color.orange, R.color.green, R.color.blue);
        swipeLayout.setOnRefreshListener(this);
        return view;
    }

    public static List<ReViewInformation> getData(){
        List<ReViewInformation> data= new ArrayList();
        int []icons={R.drawable.france,R.drawable.brazil,R.drawable.ic_launcher};
        String []titles={"hgjhgbn","hKHk","hgdjgj"};
        for(int i = 0; i <3; i++){
            ReViewInformation curr = new ReViewInformation();
            curr.id = icons[i];
            curr.Title = titles[i];
            data.add(curr);
        }
        return  data;
    }


    @Override
    public void onRefresh() {
        // TODO Auto-generated method stu
    }

}

_1Vanguard_News.class

<plugin>
    <groupId>com.smartbear.soapui</groupId>
    <artifactId>soapui-maven-plugin</artifactId>
    <version>5.1.3</version>
    <configuration>
        <projectFile>simple-test-soapui-project.xml</projectFile>
    </configuration>
</plugin>

屏幕截图

enter image description here

我有什么遗漏吗?

1 个答案:

答案 0 :(得分:1)

getItemCount()返回适配器保留的数据集中的项目总数。如果您return 0,则不会调用其他方法。变化

List<ReViewInformation> data= Collections.emptyList();

public ViewAdapter(Context context, List<ReViewInformation> data){
    inflater=LayoutInflater.from(context);
}

List<ReViewInformation> data;

public ViewAdapter(Context context, List<ReViewInformation> data){
    inflater=LayoutInflater.from(context);
    this.data = data;
}

@Override
public int getItemCount() {
    if (data == null) {
       return 0;
    }
    return data.size();
}