bmelnychuk / AndroidTreeView

AndroidTreeView. TreeView implementation for android
Apache License 2.0
3k stars 621 forks source link

Layout Height and Width is not not respected #125

Open glend1 opened 7 years ago

glend1 commented 7 years ago

When you apply a custom ViewHolder the Height and Width is not respected. for example;

XML layout;

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="64dp"
android:background="?android:attr/selectableItemBackground"
android:gravity="center_vertical">

<TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_marginStart="40dp"
    android:textStyle="bold"/>

</RelativeLayout>

Java class;

public class CustomNavigationView extends TreeNode.BaseNodeViewHolder<MenuItem> {
public CustomNavigationView(Context context) {
    super(context);
}

@Override
public View createNodeView(TreeNode node, MenuItem value) {
    final LayoutInflater inflater = LayoutInflater.from(context);
    final View view = inflater.inflate(R.layout.list_item, null, false);
    TextView tvValue = (TextView) view.findViewById(R.id.text);
    tvValue.setText(value.getName());

    return view;
}

}

Using this example the width from the RelativeLayout isn't applied and instead uses WRAP_CONTENT, however if i set the layout_width of TextView to MATCH_PARENT it fills the display as expected using Android Studio as a reference. Why is this? Can this be fixed?

The same happens when you attempt to set the layout_height for RelativeLayout nothing happens, instead TreeView uses the minHeight exclusively. layout_width is a required XML tag. It should work. Why is minHeight used instead? Can it be altered to use layout_height?

jurgensr commented 6 years ago

Fix TreeNode.java

    public View getView() {
        if (mView != null) {
            return mView;
        }
        final View nodeView = getNodeView();
        nodeView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));//JR - fix for MATCH_PARENT
        final TreeNodeWrapperView nodeWrapperView = new TreeNodeWrapperView(nodeView.getContext(), getContainerStyle());
        nodeWrapperView.insertNodeView(nodeView);
        mView = nodeWrapperView;

        return mView;
    }
fukemy commented 6 years ago

t.y. use gradle can not edit treenode class. So i import project manual and now it work as expect!

jurfeyo commented 6 years ago

BTW, you can override this method in your ViewHolder for items but not in library's source code.