I want to show a list of records using RecyclerView.
But when i run the application, it shows just one record.
I verified my code, and everything seems fine.
Adapter Code:
public class AdapterData extends RecyclerView.Adapter<AdapterData.DummyHolder> {
private LayoutInflater layoutInflater;
private ArrayList<String> mItems = new ArrayList<>();
public Context ThisContext;
public AdapterData(Context context)
{
layoutInflater = LayoutInflater.from(context);
mItems = generateValues();
ThisContext = context;
}
public static ArrayList<String> generateValues(){
ArrayList<String> Dummy = new ArrayList<>();
for(int i=1; i<100; i++)
{
Dummy.add("Item"+i);
Log.d("MTS", String.valueOf(i));
}
return Dummy;
}
@Override
public DummyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view= layoutInflater.inflate(R.layout.row_layout,parent,false);
DummyHolder holder=new DummyHolder(view);
return holder;
}
@Override
public void onBindViewHolder(DummyHolder holder, int position) {
holder.txt_name.setText(mItems.get(position));
Log.d("MAN=",mItems.get(position));
}
@Override
public int getItemCount() {
return 100;
}
public static class DummyHolder extends RecyclerView.ViewHolder{
TextView txt_name;
public DummyHolder(View itemView) {
super(itemView);
txt_name = (TextView) itemView.findViewById(R.id.tx_name);
}
}
}
Row Code:
<?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="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/tx_name"
android:hint="Hello"/>
</LinearLayout>
Main Actiivity XML :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.hogwarts.harrypotter.recyclerdemo.MainActivity">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rv_list">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
MainActivity Code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.rv_list);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setAdapter(new AdapterData(this));
}
Take a look at item layout. Root layout has android:layout_height="match_parent". It mean item take full screen height. Change to wrap_content and let party. Say oh yeah
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With