Newbie Alert
I'm populating my ListView with rows that are using ConstraintLayout. I thought that the row heights would be adjusted automatically based on the content, however that doesn't seem to be happening. Should it?
My Fragment code:
public class ConversationListFragment extends ListFragment {
public interface OnItemSelectedListener {
public void onItemSelected(Conversation conversation);
}
OnItemSelectedListener selectionListener;
@Override
public void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
Messaging controller = MessagingFactory.getInstance().getController();
Conversation[] conversations = controller.amsConversations.getActiveConversations();
setListAdapter(new ConversationListAdapter(getActivity(), conversations));
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
selectionListener = (OnItemSelectedListener) activity;
} catch (ClassCastException e){
throw new ClassCastException(activity.toString() + " must implement OnItemSelectedListener");
}
}
public void onListItemClick(ListView l, View v, int position, long id) {
Conversation conversation = (Conversation) getListAdapter().getItem(position);
selectionListener.onItemSelected(conversation);
}
}
My adapter getView method:
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.lpmessaging_ui_conversation_list_entry, parent, false);
convertView.setMinimumHeight(105); // not helping
viewHolder = new ViewHolder();
viewHolder.remoteName = (TextView) convertView.findViewById(R.id.conversation_list_remote_name);
viewHolder.lastMessage = (TextView) convertView.findViewById(R.id.conversation_list_last_message);
viewHolder.unreadCount = (TextView) convertView.findViewById(R.id.conversation_list_unread_counter);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
// set textView text here
return convertView;
}
My layout XML:
<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:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="8dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="8dp">
<TextView
android:id="@+id/conversation_list_remote_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:text="Brand Name"
android:textAlignment="viewStart"
android:textSize="17sp"
app:layout_constraintBottom_toTopOf="@+id/horizontalGuideline"
app:layout_constraintEnd_toStartOf="@+id/verticalGuideline"
app:layout_constraintStart_toStartOf="parent"
tools:layout_conversion_absoluteHeight="24dp"
tools:layout_conversion_absoluteWidth="272dp" />
<TextView
android:id="@+id/conversation_list_last_message"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:text="Last Message"
android:textAlignment="viewStart"
app:layout_constraintEnd_toStartOf="@+id/verticalGuideline"
app:layout_constraintStart_toStartOf="@+id/conversation_list_remote_name"
app:layout_constraintTop_toTopOf="@+id/horizontalGuideline" />
<TextView
android:id="@+id/conversation_list_unread_counter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:background="@drawable/lpinfra_ui_scroll_down_unread_counter_shape"
android:text="1"
android:textColor="@color/scroll_down_indicator_unread_counter_text_color"
android:textSize="@dimen/regular_text_size"
app:layout_constraintTop_toTopOf="@+id/horizontalGuideline"
app:layout_constraintBottom_toTopOf="@+id/horizontalGuideline"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/verticalGuideline"
/>
<android.support.constraint.Guideline
android:id="@+id/horizontalGuideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.5" />
<android.support.constraint.Guideline
android:id="@+id/verticalGuideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_end="45dp" />
</android.support.constraint.ConstraintLayout>
Output:
I tried to setMinimumHeight on the inflated view, but is that in SP or pixels or points or what? I tried values of 35 and 105, but it didn't change anything.
How do I get my ListView rows to size properly?
Like the iOS constraint system, you want constraints from the top of the container view down to the bottom. None of the objects in the ConstraintLayout have constraints to the top or bottom of the parent.
Add a constraint from the top TextView to the parent and a constraint from the bottom TextView to the parent and this will let android size the row properly.