Reputation: 27
I want to make an app which shows list (RecyclerView). I want to include a single TextView in the Viewholder of this RecyclerView.
MainActivity code:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private CustomAdapter customAdapter;
ListView listView;
public Cursor cursor;
public StudentRepo studentRepo;
private final static String TAG = MainActivity.class.getName().toString();
private static final String TAG_BOOKMARKS="bookmarks";
private static final String TAG_ABOUT_US="about";
//recyclerView implementation
private List<TopSample> wordlist=new ArrayList<>();
private RecyclerView recyclerView;
private StudentAdapter studentAdapter;
//recyclerView implementation done
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
studentRepo = new StudentRepo(this);
//recyclerView implement
recyclerView=(RecyclerView)findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
DatabaseAccess databaseAccess = DatabaseAccess.getInstance(this);
databaseAccess.open();
Cursor cursor= databaseAccess.getInfo();
cursor.moveToFirst();
do{
TopSample topSample=new TopSample(cursor.getString(0));
wordlist.add(topSample);
}while (cursor.moveToNext());
databaseAccess.close();
studentAdapter=new StudentAdapter(wordlist);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
//recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(studentAdapter);
StudentAdapter class
public class StudentAdapter extends RecyclerView.Adapter<StudentAdapter.MyViewHolder> {
private List<TopSample> wordslist= Collections.emptyList();
public class MyViewHolder extends RecyclerView.ViewHolder{
public TextView sword;
public MyViewHolder(View view){
super(view);
sword=(TextView)view.findViewById(R.id.vocab);
}
}
public StudentAdapter(List<TopSample> wordslist){
this.wordslist=wordslist;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView=LayoutInflater.from(parent.getContext()).inflate(R.layout.dictionary_list_row,parent,false);
MyViewHolder vh=new MyViewHolder(itemView);
return vh;
//return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
TopSample sample=wordslist.get(position);
holder.sword.setText(sample.getVocab());
}
@Override
public int getItemCount() {
return wordslist.size();
}
}
TopSample code:
public class TopSample {
public String vocab;
public TopSample(){}
public TopSample(String vocab){
this.vocab=vocab;
}
public String getVocab() {
return vocab;
}
public void setVocab(String vocab) {
this.vocab = vocab;
}
}
content_main.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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main"
tools:context=".MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical" />
</RelativeLayout>
dictionary_list_row.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:clickable="true"
android:background="?android:attr/selectableItemBackground"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/vocab"
android:textSize="32dp"
android:textStyle="bold"/>
</RelativeLayout>
So the output was like:
As you can see that an individual viewholder is taking longer rectangle size as expected and recycler view is not taking full match_parent command.
I wish to have an output like this, just as an example.:
I tried to changed length and width with different combination in the two xml files but didn't get the result as expected. Kindly have a look and tell me where i am wrong.
Just one more thing to add, i am extracting data from external database in which i am successful but this problem is probably layout problem, i think. Kindly correct me.
Upvotes: 2
Views: 3538
Reputation: 2246
Problem is you have given fixed height to TextView
and given match_parent
width and have assigned font size very large.. that is 32dp.
Use below code. You can remove android:textSize="18sp"
for default size.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/vocab"
android:textSize="18sp"
android:textStyle="bold"
/>
Upvotes: 0
Reputation: 4517
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:clickable="true"
android:background="?android:attr/selectableItemBackground"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/vocab"
android:textSize="32dp"
android:textStyle="bold"
/>
</RelativeLayout>
Upvotes: 0
Reputation: 2839
in dictionary_list_row.xml
In Relative layout
android:layout_height="wrap_content"
And of TextView
android:layout_height="wrap_content"
And
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />
Upvotes: 1
Reputation: 226
Did you try to set the RelativeLayout height to wrap_content instead of match_parent ??
Upvotes: 0