Abd EL Magied
Abd EL Magied

Reputation: 141

RecyclerView showing one item only

i'am working with RecyclerView but when items viewed in the recyclerview only the first item in the list is shown here's the RecyclerView Adapter

 package com.example.abdelmagied.myapplication;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.util.ArrayList;

/**
 * Created by AbdELMagied on 7/19/2017.
 */
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder>{
    private ArrayList<items> myitems;
    public LayoutInflater inflater;
    public RecyclerAdapter(Context context, ArrayList<items> myitems) {
        this.myitems = myitems;
        this.inflater = LayoutInflater.from(context);
    }

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

    @Override
    public void onBindViewHolder(RecyclerAdapter.MyViewHolder holder, int position) {
           holder.name.setText(myitems.get(position).name);
        holder.price.setText(myitems.get(position).price);
    }

    @Override
    public int getItemCount() {
        return myitems.size();
    }

    public static class MyViewHolder extends  RecyclerView.ViewHolder{
         TextView name;
         TextView price;
        public MyViewHolder(View itemView) {
           super(itemView);
            name = (TextView) itemView.findViewById(R.id.recyclerviewname);
            price = (TextView)itemView.findViewById(R.id.recyclerviewprice);
        }
    }
}

here's the layout which named recyclerviewrow.xml

   <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:text="TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/recyclerviewname" />

    <TextView
        android:text="TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/recyclerviewprice" />
</LinearLayout>

here's the Main3Activity.xml

 package com.example.abdelmagied.myapplication;

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.support.v7.widget.LinearLayoutManager;
    import android.support.v7.widget.RecyclerView;
    import android.widget.Toast;

    import java.util.ArrayList;

    public class Main3Activity extends AppCompatActivity {
       public RecyclerView mRecyclerView;
        public RecyclerView.LayoutManager mymanager;
        public RecyclerView.Adapter recyclerViewAdapter;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main3);
            Bundle bundle =  getIntent().getExtras();
            ArrayList<items> myitems = bundle.getParcelableArrayList("myarray");
            ArrayList<items> go = new ArrayList<items>();
             go.add(new items("mobile" , " blablabl" , "655"));
            go.add(new items("labtop" , " blablabl" , "655"));
            go.add(new items("sony" , "blablbl" , "655"));
            go.add(new items("mobile" , " blablabla" , "655"));
            go.add(new items("mobile" , "blablabla" , "655"));
            mRecyclerView = (RecyclerView) findViewById(R.id.RecyclerId);
            mymanager = new LinearLayoutManager(this);
            mRecyclerView.setHasFixedSize(true);
            mRecyclerView.setLayoutManager(mymanager);
            recyclerViewAdapter = new RecyclerAdapter(this , go);
            mRecyclerView.setAdapter(recyclerViewAdapter);

        }
    }

Upvotes: 3

Views: 4296

Answers (3)

You can also give you LinearLayout a minWidth and set the width to wrap_content

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="wrap_content"
    android:layout_height="match_parent">

    <TextView
        android:text="TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/recyclerviewname" />

    <TextView
        android:text="TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/recyclerviewprice" />
</LinearLayout>

Upvotes: 0

andiisfh
andiisfh

Reputation: 21

In your recyclerviewrow.xml, change the LinearLayout height to wrap_content.

Upvotes: 2

Muthukrishnan Rajendran
Muthukrishnan Rajendran

Reputation: 11622

Problem is match_parent, change your parent item android:layout_height to wrap_content.

Now also you will have item but it will be at the bottom, if you scroll you can see the second item.

So change like this,

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="wrap_content">
</LinearLayout>

Upvotes: 9

Related Questions