RecyclerView.Adapter never calls onCreateViewHolder - android

After searching for hours I can't find a solution to my problem. I have the following classes:
Main activity:
RecyclerView rvIdeas = (RecyclerView) findViewById(R.id.recView1);
// Initialize ideas
DaoSession daosession = ((App) getApplication()).getDaoSession();
IdeaDao ideadao = daosession.getIdeaDao();
ideas = (ArrayList<Idea>) ideadao.loadAll();
// Set layout manager to position the items
rvIdeas.setLayoutManager(new LinearLayoutManager(this));
// Create adapter passing in the list of ideas
IdeaAdapter adapter = new IdeaAdapter(this, ideas);
// Attach the adapter to the recyclerview to populate items
rvIdeas.setAdapter(adapter);
Then the adapter:
public class IdeaAdapter extends
RecyclerView.Adapter<IdeaAdapter.ViewHolder> {
// Provide a direct reference to each of the views within a data item
// Used to cache the views within the item layout for fast access
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView ideaname;
public TextView docCount;
public TextView last_mod;
// We also create a constructor that accepts the entire item row
// and does the view lookups to find each subview
public ViewHolder(View itemView) {
// Stores the itemView in a public final member variable that can be used
// to access the context from any ViewHolder instance.
super(itemView);
ideaname = (TextView) itemView.findViewById(R.id.main_idea_name);
docCount = (TextView) itemView.findViewById(R.id.idea_documents);
last_mod = (TextView) itemView.findViewById(R.id.idea_last_edited);
}
}
// Store a member variable for the ideas
private List<Idea> ideas;
// Store the context for easy access
private Context mContext;
// Pass in the idea array into the constructor
public IdeaAdapter(Context context, List<Idea> ideaList) {
ideas = ideaList;
mContext = context;
}
// Easy access to the context object in the recyclerview
private Context getContext() {
return mContext;
}
// Usually involves inflating a layout from XML and returning the holder
#Override
public IdeaAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
// Inflate the custom layout
View ideaView = inflater.inflate(R.layout.item_idea, parent, false);
// Return a new holder instance
return new ViewHolder(ideaView);
}
// Involves populating data into the item through holder
#Override
public void onBindViewHolder(IdeaAdapter.ViewHolder viewHolder, int position) {
// Get the data model based on position
Idea idea = ideas.get(position);
// Set item views based on your views and data model
viewHolder.ideaname.setText(idea.getName());
String docs = R.string.media + idea.getName();
viewHolder.docCount.setText(docs);
viewHolder.last_mod.setText(idea.getLast_modified().toString());
}
// Returns the total count of items in the list
#Override
public int getItemCount() {
return ideas.size();
}
}
And the .xml of fragment_main:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:app1="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"
tools:context=".view.fragments.MainFragment">
<android.support.v7.widget.RecyclerView
android:id="#+id/rvIdea"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none"
android:visibility="visible" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab_insert"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="325dp"
android:layout_marginStart="325dp"
android:layout_marginRight="16dp"
android:layout_marginEnd="16dp"
android:layout_marginTop="465dp"
android:adjustViewBounds="false"
android:baselineAlignBottom="false"
android:clickable="true"
android:nestedScrollingEnabled="true"
android:onClick="addFABclick"
app1:pressedTranslationZ="24dp"
app:layout_behavior=".view.animation.ScrollAwareFABBehavior"
app:srcCompat="#drawable/material_add_white" />
</android.support.design.widget.CoordinatorLayout>
Last but not least the layout for the recyclerview:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/main_idea_name"
android:layout_width="210dp"
android:layout_height="20dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="8dp"
android:layout_marginStart="16dp"
android:layout_marginTop="10dp"
android:textAppearance="#android:style/TextAppearance.Medium"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/idea_documents"
android:layout_width="210dp"
android:layout_height="20dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="8dp"
android:layout_marginStart="16dp"
android:layout_marginTop="0dp"
android:textColor="#color/black"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/main_idea_name" />
<ImageView
android:id="#+id/idea_share"
android:layout_width="28dp"
android:layout_height="31dp"
app:srcCompat="?attr/actionModeShareDrawable"
app:layout_constraintRight_toLeftOf="#+id/idea_go_inside"
android:layout_marginRight="22dp"
android:layout_marginEnd="22dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="16dp"
android:contentDescription="#string/share"/>
<ImageView
android:id="#+id/idea_go_inside"
android:layout_width="29dp"
android:layout_height="30dp"
app:srcCompat="#drawable/material_arrow_right"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="16dp"
android:layout_marginRight="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintRight_toRightOf="parent"
android:contentDescription="#string/go_next"/>
<ImageView
android:id="#+id/idea_trash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#android:drawable/ic_menu_delete"
app:layout_constraintRight_toLeftOf="#+id/idea_share"
android:layout_marginRight="14dp"
android:layout_marginEnd="14dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="16dp"
app:layout_constraintLeft_toRightOf="#+id/idea_documents"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
app:layout_constraintHorizontal_bias="1.0"
android:contentDescription="#string/trash"/>
<TextView
android:id="#+id/idea_last_edited"
android:layout_width="210dp"
android:layout_height="20dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="8dp"
android:layout_marginStart="16dp"
android:layout_marginTop="0dp"
android:textColor="#color/colorSecondaryText"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/idea_documents" />
</android.support.constraint.ConstraintLayout>
The problem is that onBindViewHolder and onCreateViewHolder are never called. getItemCount returns something > 0 and there are no null pointers or nested scrolls. The recyclerview displays nothing. What am I doing wrong?
Thanks for any help.

Related

RecyclerView Item Weird Spacing on phone and Emulator but Normal on Android Studio Preview

I have an app that displays posts in a Recycler View. The recycler View item has an image, a title and a chip group with hashtags
My problem is that I am getting a weird spacing between the chip group and text when I play the app on my phone or emulator but it looks fine on the preview
What this could possible be?
This is the xml for the recycler View item:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:gravity="center_horizontal|top"
android:orientation="vertical"
android:padding="10dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:background="#color/white"
android:clipToPadding="false"
android:elevation="4dp"
android:padding="2dp">
<ImageView
android:id="#+id/imageViewThumbnailRecyclerPost"
android:layout_width="157dp"
android:layout_height="100dp"
android:backgroundTint="#color/dark_gray_eg"
android:foregroundGravity="center"
android:scaleType="centerCrop"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0"
app:srcCompat="#drawable/thumbnail_preview" />
<ImageView
android:id="#+id/imageViewTypeRecyclerPost"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_margin="3dp"
android:layout_marginTop="28dp"
android:layout_marginEnd="4dp"
android:elevation="3dp"
android:foregroundGravity="center"
android:scaleType="centerInside"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/google_slides_icon" />
<ImageView
android:id="#+id/imageViewStatusRecyclerPost"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_margin="3dp"
android:foregroundGravity="center"
android:scaleType="centerInside"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageViewTypeRecyclerPost"
app:srcCompat="#drawable/icon_edited" />
</androidx.constraintlayout.widget.ConstraintLayout>
<TextView
android:id="#+id/textViewTitleRecyclerPost"
android:layout_width="157dp"
android:layout_height="wrap_content"
android:layout_marginStart="1dp"
android:layout_marginTop="5dp"
android:inputType="textMultiLine"
android:lines="2"
android:text="A Vinda da fampilia real e a independencia do Brasil"
android:textColor="#color/dark_gray_eg"
android:textSize="13sp"
android:textStyle="bold" />
<com.google.android.material.chip.ChipGroup
android:id="#+id/chipGroupRecyclerPost"
android:layout_width="157dp"
android:layout_height="wrap_content"
android:layout_marginStart="-1dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="-1dp"
app:chipSpacingHorizontal="0dp"
app:chipSpacingVertical="5dp"
app:singleLine="false">
</com.google.android.material.chip.ChipGroup>
</LinearLayout>
This is how it looks like on the preview and should look like on my phone:
This is how it is actually looking like on my phone:
This is how I initialize the recycler:
GridLayoutManager layoutManager = new GridLayoutManager(getContext(), 2);
adapterRecyclerSearch = new AdapterRecyclerSearch(getContext(), postsList);
recyclerViewSearch.setLayoutManager(layoutManager);
recyclerViewSearch.setHasFixedSize(true);
recyclerViewSearch.setAdapter(adapterRecyclerSearch);
This is my adapter:
public class AdapterRecyclerSearch extends RecyclerView.Adapter<AdapterRecyclerSearch.MyViewHolder> {
// list of Posts
public List<DatabasePostHolder> postsList = new ArrayList<>();
public Context context;
public StorageReference storage = FirebaseInstances.getStorageReference();
public StorageReference storageThumbnails = storage.child(Constants.STORAGE_FOLDER_THUMBNAILS);
public AdapterRecyclerSearch(Context context, List<DatabasePostHolder> postsList){
// recieve context for glide and posts list to display
this.context = context;
this.postsList = postsList;
}
public void setPostsList(List<DatabasePostHolder> postsList) {
this.postsList = postsList;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View postView = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_post_layout, parent, false);
return new MyViewHolder(postView);
}
#SuppressLint("ResourceAsColor")
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, int position) {
// set hashtag chips
List<String> tagsList = postHolderInPosition.getTags();
holder.chipGroupHashtags.removeAllViews();
for (int i=0; i<tagsList.size(); i++){
Chip chip = (Chip) LayoutInflater.from(context).inflate(R.layout.chip_hashtag_template, holder.chipGroupHashtags, false);
chip.setText("#" + tagsList.get(i));
holder.chipGroupHashtags.addView(chip);
}
}
#Override
public int getItemCount() {
return postsList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder{
// variables of what will be inside the layout for the adapter
TextView textViewTitle;
ImageView imageThumbnail, imageType, imageStatus;
ChipGroup chipGroupHashtags;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
// define view components from post layouts
textViewTitle = itemView.findViewById(R.id.textViewTitleRecyclerPost);
imageThumbnail = itemView.findViewById(R.id.imageViewThumbnailRecyclerPost);
imageType = itemView.findViewById(R.id.imageViewTypeRecyclerPost);
imageStatus = itemView.findViewById(R.id.imageViewStatusRecyclerPost);
chipGroupHashtags = itemView.findViewById(R.id.chipGroupRecyclerPost);
}
}
}
Adding this to the chip group appearence resource solved it for me
<item name="chipMinTouchTargetSize">0dp</item>
<item name="chipMinHeight">15dp</item>

Linear View in ScrollView with Array Adapter growing up instead of down on new items

I have an Java Android project I am working on wherein I have a LinearLayout that is managed by an array adapter.
My issue is when I add a new item to the array adapter and update the view the List of items is growing upwards instead of down as is visible in the images below.
Why is this now growing down? and how can i make it do so?
Code follows pics
Main Activity
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
>
<AnalogClock
android:id="#+id/clockMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.1" />
<TextView
android:id="#+id/dateMsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/clockMain"
/>
<TextView
android:id="#+id/titleMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/annie_use_your_telescope"
android:text="#string/app_name"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/dateMsg"
app:layout_constraintVertical_bias="0.05"
android:textSize="30sp"
android:textColor="#color/colorPrimaryDark"
/>
<ListView
android:id="#+id/taskList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintVertical_bias="0.5"
android:nestedScrollingEnabled="true"
/>
<Button
android:id="#+id/addNewTask"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/add_new_task"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintVertical_bias="0.95"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
Task List
<?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="wrap_content"
>
<TextView
android:id="#+id/taskDesc"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:textSize="15sp"
android:text="testing task"
android:layout_weight="1"
/>
<TextView
android:id="#+id/taskDate"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="this is bull shit"
android:layout_weight="1"
/>
<TextView
android:id="#+id/taskTime"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="some more bullish"
android:layout_weight="1"
/>
</LinearLayout>
inflator
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = inflater.inflate(R.layout.task_list, null);
TextView taskDesc = (TextView) view.findViewById(R.id.taskDesc);
TextView taskDate = (TextView) view.findViewById(R.id.taskDate);
TextView taskTime = (TextView)view.findViewById(R.id.taskTime);
taskDesc.setText(tasks.get(i).description);
taskDate.setText(tasks.get(i).taskDate.toString());
taskTime.setText(tasks.get(i).taskTime.toString());
return view;
}
import java.util.ArrayList;
public class TaskAdapter extends BaseAdapter {
Context context;
LayoutInflater inflater;
ArrayList<Task> tasks;
public TaskAdapter(Context applicationContext, ArrayList<Task> tasks) {
this.context = applicationContext;
this.tasks = new ArrayList<>(tasks);
inflater = (LayoutInflater.from(applicationContext));
}
#Override
public int getCount() {
return tasks.size();
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = inflater.inflate(R.layout.task_list, null);
TextView taskDesc = (TextView) view.findViewById(R.id.taskDesc);
TextView taskDate = (TextView) view.findViewById(R.id.taskDate);
TextView taskTime = (TextView)view.findViewById(R.id.taskTime);
taskDesc.setText(tasks.get(i).description);
taskDate.setText(tasks.get(i).taskDate.toString());
taskTime.setText(tasks.get(i).taskTime.toString());
return view;
}
}
Can you add the array adapter you are using(how you are inserting data in arrayadapter) and where you are passing the array to listview.

Trouble with ListView

I have an issue with my Main Activity and a ListView and I absolutly don't understand how it works...
OK ! This is what I want :
Expectation
It means I need a list of item : TextViews
And OUTSIDE of this list a simple button. I don't want the button to be part of the list.
But here is what I have :
Reality
As you can see, in every child of my list i have my text AND my button : Why ??
My code Main :
public class MainMenu extends AppCompatActivity implements IRemDeck, IRemCard {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
ListView listView = findViewById(R.id.MainDecksList);
AdapterDeck adpDeck;
ArrayList<Deck> myDecks = new ArrayList<>();
for (int i = 0; i<5;i++){
myDecks.add(new Deck("Deck " + i));
}
adpDeck = new AdapterDeck(this, 0, myDecks);
listView.setAdapter(adpDeck);
}
My Adapter (for the ListView) :
public AdapterDeck(#NonNull Context context, int resource, #NonNull ArrayList<Deck> decks) {
super(context, resource, decks);
this.decks = decks;
this.context = context;
}
#Override
public int getCount() {
return decks.size();
}
#Override
public Deck getItem(int pos) {
return decks.get(pos);
}
#Override
public long getItemId(int pos) {
return 0;
//just return 0 if your list items do not have an Id variable.
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.activity_main_menu, null);
}
//Handle TextView and display string from your list
TextView listItemText = view.findViewById(R.id.MainDeckNom);
listItemText.setText(decks.get(position).getNom());
return view;
}
The XML :
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.MainMenu">
<TextView
android:id="#+id/MainDeckNom"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ListView
android:id="#+id/MainDecksList"
android:layout_width="363dp"
android:layout_height="426dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/MainDecksList" />
</android.support.constraint.ConstraintLayout>
If someone could explain to me how to solve my problem (and how it works eventually) I would be very grateful !
Thanks a lot :)
listview has to have its own layout, so you need to create a new layout that just contains listview and in getView() method use it as a row of your listView
1- create a new row_listview.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.MainMenu">
<TextView
android:id="#+id/MainDeckNom"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.constraint.ConstraintLayout>
2-Change this line
view = inflater.inflate(R.layout.activity_main_menu, null);
to
view = inflater.inflate(R.layout.row_listview, null);
In your adapter's getView() method, you have to design a child_view layout. The layout which you return will be show in every item of the list. Because you're returning main_activity_layout so every your item would show the mainAcitivy, which contains a button. Solution is create new layout with your own design and replace it with R.layout.main_acitivity_layout. Good luck
You should not use R.layout.activity_main_menu in getView, because getView returns a line of the list which you don't want to have a button.
So, you have a separate layout activity_main_men specified as Content view for the activity which does not have TextView:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.MainMenu">
<ListView
android:id="#+id/MainDecksList"
android:layout_width="363dp"
android:layout_height="426dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/MainDecksList" />
</android.support.constraint.ConstraintLayout>
The in getView instead of
view = inflater.inflate(R.layout.activity_main_menu, null);
you just place:
view = new TextView()
view.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

empty row in listview

I created a listView which I associated with a adapter. Problem is that rows appear empty even if I set the textview from the view which was created. I mention that the number of rows from listview is right. Thank you.
shopping_page_item
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/productTitle"
android:layout_width="0dp"
android:layout_height="47dp"
android:layout_marginEnd="32dp"
android:layout_marginStart="32dp"
android:layout_marginTop="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Adapter
public class ShoppingListAdapter extends ArrayAdapter<String> {
public ShoppingListAdapter(Context context, String content[]) {
super(context, R.layout.shopping_page_item, content);
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.shopping_page_item, parent, false);
}
TextView editText = convertView.findViewById(R.id.productTitle);
editText.setText(getItem(position).toString());
return convertView;
}
}
Try this
<TextView
android:id="#+id/productTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="32dp"
android:layout_marginStart="32dp"
android:layout_marginTop="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Your parent layout's height is match_content, make it wrap_content. It may be possible that your one row covering the complete screen.

Is there a way to make a recycler view show a fixed number of items?

I have an alert dialog built in a ConstraintLayout with a RecyclerView in it.
My problem is that recycler view has not a fixed size and it only displays a few items (it's scrollable though, but I need it to be bigger).
This is my layout:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:fontFamily="sans-serif-light"
android:text="#string/traces_dialog_title"
android:textColor="#color/black_color"
android:textSize="26sp"
app:layout_constraintBottom_toBottomOf="#+id/view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="#+id/view"
android:layout_width="0dp"
android:layout_height="0.5dp"
android:layout_marginBottom="45dp"
android:layout_marginTop="53dp"
android:background="#color/graySeparatorBarColor"
app:layout_constraintBottom_toBottomOf="#+id/linearLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/linearLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="4dp"
android:fontFamily="sans-serif-light"
android:text="#string/order_number_label"
android:textColor="#color/black_color"
android:textSize="20sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView2" />
<TextView
android:id="#+id/order_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginStart="8dp"
android:fontFamily="sans-serif-light"
android:text="#12345"
android:textColor="#color/black_color"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="#+id/medication_name"
app:layout_constraintStart_toEndOf="#+id/linearLayout"
app:layout_constraintTop_toTopOf="#+id/linearLayout" />
<TextView
android:id="#+id/medication_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="4dp"
android:fontFamily="sans-serif"
android:text="Ciclofosfamida 500mg"
android:textColor="#color/black_color"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linearLayout" />
<android.support.v7.widget.RecyclerView
android:id="#+id/traces"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:nestedScrollingEnabled="false"
android:overScrollMode="never"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/medication_name"
app:layout_constraintVertical_chainStyle="spread_inside"
app:layout_constraintBottom_toTopOf="#+id/view3"
android:layout_marginBottom="16dp">
</android.support.v7.widget.RecyclerView>
<View
android:id="#+id/view3"
android:layout_width="0dp"
android:layout_height="0.5dp"
android:layout_marginBottom="8dp"
android:background="#color/graySeparatorBarColor"
app:layout_constraintBottom_toTopOf="#+id/close"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="#+id/close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginEnd="16dp"
android:fontFamily="sans-serif-medium"
android:paddingBottom="#dimen/spacingTiny"
android:paddingTop="#dimen/spacingTiny"
android:text="#string/accept_delivery"
android:textColor="#color/buttonColor"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</android.support.constraint.ConstraintLayout>
And this is how I open and handle the AlertDialog:
private void openTracesDialog(final Medication medication) {
AlertDialog.Builder builder;
final AlertDialog alertDialog;
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.show_traces_dialog, null);
RecyclerView traceList = (RecyclerView) layout.findViewById(R.id.traces);
TextView orderNumber = (TextView) layout.findViewById(R.id.order_number);
TextView medicationName = (TextView) layout.findViewById(R.id.medication_name);
medicationName.setText(medication.getName());
orderNumber.setText("#" + order.getOrderNumber());
traceList.setLayoutManager(new LinearLayoutManager(mContext));
traceList.setAdapter(new RecyclerView.Adapter<TraceHolder>() {
#Override
public TraceHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.item_trace, null);
v.setLayoutParams(new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.WRAP_CONTENT));
return new MedicationExtendedAdapter.TraceHolder(v);
}
#Override
public void onBindViewHolder(TraceHolder holder, int position) {
int tracePosition = position+1;
holder.mTraceLabel.setText("TRAZA "+tracePosition);
holder.mTraceNumber.setText(medication.getTraces().get(position));
}
#Override
public int getItemCount() {
return medication.getTraces().size();
}
});
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
alertDialog = builder.create();
TextView closeButton = (TextView) layout.findViewById(R.id.close);
closeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alertDialog.dismiss();
}
});
alertDialog.show();
}
Is there a way to make a recycler view show a fixed number of items?
Using the default AlertDialog, it is not possible. Why you really need to change the default behaviour?
Anyway you can create your own custom fragment layout instead of the default DialogFragment or AlertDialog with transparent background so that you can assign it to any height you want. Then show/hide that fragment whenever you want instead of showing an alert dialog.
Change your:
#Override
public int getItemCount() {
return medication.getTraces().size();
}
to:
#Override
public int getItemCount() {
return 100;
}
or whatever value you want.
Already fixed this. What I did was to set a fixed height depending on the screen to make the Dialog bigger. As all elements are constrainted, making Dialog higher makes the RecyclerView higher also.
ConstraintLayout popUpLayout = (ConstraintLayout) layout.findViewById(R.id.constraint);
popUpLayout.setMinHeight((int) (mContext.getResources().getDisplayMetrics().heightPixels * 0.75));
you can change the return of this method
#Override
public int getItemCount() {
return medication.getTraces().size();
}
to a fixed number
#Override
public int getItemCount() {
return YOUR_FIXED_NUMBER;
}
but you should take care if the size of your list is inferior than YOUR_FIXED_NUMBER you will have crashes so you should add a test for this, like that:
#Override
public int getItemCount() {
if(medication.getTraces().size() > YOUR_FIXED_NUMBER)
return YOUR_FIXED_NUMBER;
else
return medication.getTraces().size(); //or any lower number depends on your need
}
If RecyclerView has children (items) that has fixed width and height. For allowing the RecyclerView to optimize better by figuring out the exact height and width of the entire list based on the your adapter, use this line:
recyclerView.setHasFixedSize(true)
You should override the layoutmanager of your recycleview for this. This way it will only disable scrolling.
public class CustomGridLayoutManager extends LinearLayoutManager {
private boolean isScrollEnabled = true; // create boolean variable here
public CustomGridLayoutManager(Context context) {
super(context);
}
// create method to enable or disable scrolling
public void setScrollEnabled(boolean flag) {
this.isScrollEnabled = flag;
}
#Override
public boolean canScrollVertically() {
//Similarly you can customize "canScrollHorizontally()" for managing
//horizontal scroll
return isScrollEnabled && super.canScrollVertically();
}
}
now add your custom layout manager to your recyclerview
now you can disable and enable scrolling functionality of your recyclerview
linearLayoutManager = new LinearLayoutManager(context) {
#Override
public boolean canScrollVertically() {
return false;
}
};

Categories

Resources