RecycleView displaying only the first item –

Development issue/problem:

Here’s the problem: I create the simplest RecyclerView in the world, but it only shows the first element. I don’t understand why. Thank you for your help.

article_layout.xml

Active_main.xml

MainActive.java

Package com.bcit.moonlady.testrecycler;

Import android.support.v7.app.AppCompatActivity;import android.os.bundle;import android.support.v7.widget.LinearLayoutManager;import android.support.v7.widget RecyclerView;import android.view.os.Bundle;import android.support.v7.widget.LayoutManager.v7.widget.RecyclerView;import android.view.LayoutInflater;import android.view.view;import android.viewGroup;import android.widget.TextView ;

The MainActivity public class extends AppCompatActivity [
String[] data = {test1, test2, test3};

RecyclerView mRecView ;

Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState) ;
setContentView(R.layout.activity_main) ;

mRecView = (RecyclerView)findViewById(R.id.rv_details);
mRecView.setHasFixedSize(true);
mRecView.setLayoutManager(new LinearLayoutManager(getApplicationContext());
mRecView.setAdapter(new DetailAdapter());
}

The closed class DetailView extends RecyclerView.ViewHolder {
TextView mTextView ;

public DetailView(View itemView) {
super(itemView);
mTextView = (TextView)itemView.findViewById(R.id.tv_detail);
}

public invalid bindView(string) {
mTextView.setText(string);
}
}

The private DetailAdapter class extends RecyclerView.Adapter {
@Override
public DetailView onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View v = layoutInflater.inflate(R.layout.item_layout, parent, false);
returns a new DetailView(v);
}

Check
public void onBindViewHolder(DetailView holder, int position) {
String string = data [position] ;
holder.bindView(string) ;
}

@Overridepublic int getItemCount() {retourdata.length;}}

How can I solve this problem?

Solution 1:

You must change the item_layout height to wrap_content
if you are using Android Support Library v 23.2.0 and above.

Solution 2:

I’ve made that common mistake too. Just change the parent LinearLayout – the height should be wrap_content.

..
..

Solution 3:

Set the height of the wrap_content parameter in the RelativeLayout.xml file.

Solution 4:

it might help

customAdapter = new CustomRecycleradapter(arrItems) ;
recycleView.setLayoutManager(new LinearLayoutManager(mParentActivity)) ;
recycleView.setAdapter(customAdapter) ;

ArrayList mSource ;
// Class
public adapter CustomRecycleradapter(ArrayList source) {
this.mSource = source ;

@ address
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item, parent, false);
returns new CustomHolder(view);
}
@ Re-check public empty
opBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (owner instance of CustomHolder) {
((CustomHolder) holder).nameClient.setText(mSource.get(position).getCustomerNumber()) ;

}
}

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

public invalid addItem(ArrayList itemLists) {
mSource.addAll(itemLists) ;

notItemInserted(mSource.size() – 1);
}
public class CustomHolder extends RecyclerView.ViewHolder {
@Bind(R.id.customer_name)
TextView customerName;
@Bind(R.id.time_stamp)
TextView timeStamp;
@Bind(R.id.amount)
TextView amount ;

public CustomHolder(itemView) {
super(itemView) ;
ButterKnife.bind(this, itemView) ;

}
}

Solution no. 5:

I had the same problem when RecyclerView was wrapped in ScrollView. The NestedScrollView should be used instead.

Solution no. 6:

Activities :

import android.os.bundle; import android.support.design.widget.floatActionButton; import android.support.widget.SnackBar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.RecyclerView widget; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.Toolbar; import android.view.LayoutInflater; import android.view.view; import android.view.Menu; import android.view.MenuItem; import android.view.View.ViewGroup; import android.widget.TextView ;

The public MainActivity class extends AppCompatActivity {
private RecyclerView mRecycler;
String[] data = {test1, test2, test3} ;

Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState) ;
setContentView(R.layout.activity_main) ;

Toolbar = (Toolbar) findViewById(R.id.toolbar) ;
setSupportActionBar(toolbar) ;

mRecycler = (RecyclerView) findViewById(R.id.rv_details);
DetailAdapter adapter = new DetailAdapter();
LinearLayoutManager manager = new LinearLayoutManager(this);
mRecycler.setHasFixedSize(true);
mRecycler.setLayoutManager(manager);
mRecycler.setAdapter(adapter) ;

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, replace with your own action, Snackbar.LENGTH_LONG)
.setAction(Action, null).show();
}
});
}

@General Boolean menu
onCreateOptionsMenu(Menu) {
// Inflate the menu; this adds items to the action bar, if any.
getMenuInflater().inflate(R.menu.menuu_main, menu);
return true;
}

@All public boolean
selected (MenuItem element) {
// Click the action bar elements here. The action bar
// automatically handles clicks on the Home/Up button as long as
// you specify the parent activity in AndroidManifest.xml.
int id = item.getItemId() ;

// no inspection SimplifiableIfStatement
if (id == R.id.action_settings) {
returns true;
}

return super.onOptionsItemSelected(item);
}

The DetailAdapter private class extends RecyclerView.Adapter {.

Check
public void onBindViewHolder(DetailView holder, int position) {
String string = data [position] ;
holder.bindView(string) ;
}

@Check
public DetailView onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View v = layoutInflater.inflate(R.layout.item_layout, parent, false);
return new DetailView(v);
}

@Override
public int getItemCount() {
returndata.length;
}

The DetailView class extends the RecyclerView.ViewHolder class {
TextView mTextView ;

public DetailView(View itemView) {
super(itemView);
mTextView = (TextView)itemView.findViewById(R.id.tv_detail);
}

public invalid bindView(string) {
mTextView.setText(string);
}
}

}
}

Active_main.xml

 

 

 

 

 

 

content_main.xml

 

 

article_layout.xml

 

 

Solution no. 7:

First, you need to add your Recyclerview.veiwholder class to the adapter to make sure you get the same instance.

// replaces the contents of the view called by the layout manager
@ Override public void onBindViewHolder(view holder, int position) {
// retrieves a message to be displayed in the array at the specified position
// replaces the contents of the view with the new element
holder.mytextview.setText(ReceveHistory.get(position));
}

Good luck!

Related Tags:

recyclerview show only 10 items,android-recyclerview show only 3 items,recyclerview show only 5 item,recyclerview only displays one item,recyclerview get first item,android-recyclerview select first item by default,recyclerview focus first item,how to show exact number of items in recyclerview,recyclerview show only one item at a time

Leave a Reply

Your email address will not be published.