只有一件物品显示在回收处

时间:2022-06-23 12:25:33

I am having problems displaying all the items in my recyclerview. My code only shows one item (Home. I have 3 items: Home, Browse Photos and My Travels)

在我的回收视图中显示所有的项目都有问题。我的代码只显示一个项目(Home)。我有三样东西:家,浏览照片和旅行)

fragment_navigation_drawer.xml

fragment_navigation_drawer.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragment_navigation_drawer"
    android:background="#EEE"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.traveldiaries.ztir.traveldiaries.NavigationDrawerFragment">

    <!-- TODO: Update blank fragment layout -->
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="220dp"
        android:background="#16a085"
        android:id="@+id/linearLayout">

    </LinearLayout>

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

</RelativeLayout>

RecyclerAdapter.java

RecyclerAdapter.java

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

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

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

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        MenuList current=data.get(position);
        holder.title.setText(current.title);
        holder.image.setImageResource(current.icon);
    }

    @Override
    public int getItemCount() {
        return data.size();
    }
    class MyViewHolder extends RecyclerView.ViewHolder{
        TextView title;
        ImageView image;
        public MyViewHolder(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.item_list);
            image =  (ImageView) itemView.findViewById(R.id.icons);
        }
    }
}

NavigationDrawerFragment.java (posted code only related to recyclerview)

NavigationDrawerFragment。java(只发布与回收视图相关的代码)

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View layout = inflater.inflate(R.layout.fragment_navigation_drawer, container, false);
    recyclerView = (RecyclerView) layout.findViewById(R.id.drawerList);
    adapter = new RecyclerAdapter(getActivity(),getData());
    recyclerView.setAdapter(adapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    return layout;
}

public static List<MenuList> getData(){
    List<MenuList> data = new ArrayList<>();
    String[] titles={"Home","My Travels","Browse Photos"};
    int[] icons ={R.drawable.home,R.drawable.sunglasses,R.drawable.tooltip_image};

    for(int i=0;i<3;i++){
        MenuList current = new MenuList();
        current.title=titles[i];
        current.icon=icons[i];
        Log.d("Data",titles[i]);
        Log.d("Data",""+icons[i]);
        data.add(current);
    }
    return data;
}

itemlist.xml

itemlist.xml

<?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:padding="8dp"
        android:id="@+id/icons"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_menu_camera"
        android:layout_gravity="center_vertical"
        />

    <TextView
        android:id="@+id/item_list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Dummy Text"
        android:layout_gravity="center_vertical"
        android:padding="8dp"
        />
</LinearLayout>

What I was able to do is to check all the passed data (all seems to be fine). Also tried to change width to wrap content (fragment_navigation_drawer.xml) in both my relative and linear layout but I just lose my background color instead and still only one item appear.

我能做的是检查所有已通过的数据(一切看起来都很好)。还尝试在相对布局和线性布局中改变宽度来包装内容(fragment_navigation_drawer.xml),但我只是去掉了背景颜色,仍然只显示了一个条目。

2 个解决方案

#1


15  

Change height of LinearLayout to wrap_content in itemlist.xml

将线性布局的高度更改为itemlist.xml中的wrap_content

android:layout_height="wrap_content"

#2


0  

1) if your recyclerview is vertical then set height of recyclerview match_parent and row_item.xml height also match_parent

1)如果回收视图是垂直的,则设置回收视图match_parent和row_item的高度。xml的高度还match_parent

2) if your recyclerview is horizontal then set Width of recyclerview match_parent and row_item.xml Width also match_parent

2)如果回收视图是水平的,则设置回收视图match_parent和row_item的宽度。xml宽度还match_parent

for ex:- Horizontal RecyclerView

例:-水平RecyclerView

<android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="60dp"
        android:layout_marginRight="60dp" />

row_item.xml

row_item.xml

<TextView
android:layout_width="match_parent"
android:layout_height="180dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:background="@drawable/rect"
android:gravity="center"
android:maxLines="2"
android:padding="10dp"
android:textColor="@color/white"
android:textSize="25sp" />

#1


15  

Change height of LinearLayout to wrap_content in itemlist.xml

将线性布局的高度更改为itemlist.xml中的wrap_content

android:layout_height="wrap_content"

#2


0  

1) if your recyclerview is vertical then set height of recyclerview match_parent and row_item.xml height also match_parent

1)如果回收视图是垂直的,则设置回收视图match_parent和row_item的高度。xml的高度还match_parent

2) if your recyclerview is horizontal then set Width of recyclerview match_parent and row_item.xml Width also match_parent

2)如果回收视图是水平的,则设置回收视图match_parent和row_item的宽度。xml宽度还match_parent

for ex:- Horizontal RecyclerView

例:-水平RecyclerView

<android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="60dp"
        android:layout_marginRight="60dp" />

row_item.xml

row_item.xml

<TextView
android:layout_width="match_parent"
android:layout_height="180dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:background="@drawable/rect"
android:gravity="center"
android:maxLines="2"
android:padding="10dp"
android:textColor="@color/white"
android:textSize="25sp" />