回收者视图未显示在片段中

时间:2018-09-25 09:05:35

标签: java android android-fragments android-recyclerview

我有三个片段:片段用户片段首页片段进度

我正在尝试在家庭碎片中实现一个recycer视图,该视图可以在回收器视图中回收图像,但是什么也没有发生,并且logcat中没有错误显示,我不知道自己在做什么。

1。这是我的自定义适配器:

import android.content.Context;
import android.support.annotation.NonNull;
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 java.util.Collections;
import java.util.List;

public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder>{
@NonNull

private LayoutInflater inflator;
List<Information> data= Collections.emptyList();

public CustomAdapter(Context context, List<Information> data){
    inflator= LayoutInflater.from(context);
}
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view=inflator.inflate(R.layout.customlayout, parent,false);
    MyViewHolder holder=new MyViewHolder(view);
    return holder;
}

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {

    Information current=data.get(position);
    holder.icon.setImageResource(current.images);
    holder.title.setText(current.title);

}

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

class MyViewHolder extends RecyclerView.ViewHolder{

    ImageView icon;
    TextView title;
    public MyViewHolder(View itemView) {
        super(itemView);

        icon = (ImageView) itemView.findViewById(R.id.listicon);
        title = (TextView) itemView.findViewById(R.id.textView);
    }
}
}
  1. 这是我的片段

    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    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 android.widget.ArrayAdapter;
    import android.widget.BaseAdapter;
    import android.widget.ImageView;
    import android.widget.ListView;
    import android.widget.SimpleAdapter;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    public class HomeFragment extends Fragment  {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private CustomAdapter adapter;
    int[] IMAGES = {R.drawable.abs,R.drawable.arms,R.drawable.back,R.drawable.chest,R.drawable.full,R.drawable.legs};
    private RecyclerView recyclerView;
    
    
    ArrayList<HashMap<String, String>> data;
    
    
    private String mParam1;
    private String mParam2;
    
    
    public HomeFragment() {
    
      }
    public static HomeFragment newInstance(String param1, String param2) {                   
    HomeFragment fragment = new HomeFragment();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
        }
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }
        }
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    
    View layout = inflater.inflate(R.layout.fragment_home, container, false);
     recyclerView =(RecyclerView) layout.findViewById(R.id.drawerList);
     adapter = new CustomAdapter(getActivity(), getData());
     recyclerView.setAdapter(adapter);
     recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    return layout;
      }
    
    public static List<Information> getData(){
    List<Information> data = new ArrayList<>();
    int [] icons = {R.drawable.abs,R.drawable.arms,R.drawable.back,R.drawable.chest,R.drawable.full,R.drawable.legs};
    String[] titles={"Abs","Arms","Back","Chest","full","legs"};
    for (int i=0; i<icons.length && i<titles.length;i++){
    
        Information current = new Information();
        current.images=icons[i];
        current.title=titles[i];
        data.add(current);
    }
    return data;
    
    }
    
    
    
    
    
    }
    

我看不到我在做错什么。

  1. fragmenthome.xml

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".HomeFragment">
    
    
    
    <android.support.v7.widget.RecyclerView
    android:id="@+id/drawerList"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true">
    
     </android.support.v7.widget.RecyclerView>
    
         </FrameLayout>
    

4.Custom_layout.xml

    <RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">


<ImageView
    android:id="@+id/listicon"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    app:srcCompat="@mipmap/ic_launcher" />

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="62dp"
    android:text="TextView" />

1 个答案:

答案 0 :(得分:0)

您没有在适配器的构造函数中分配数据

this.data = data;

那么您的商品数应该是

  data.size(); 

不应为0。