Android: Improper layout change due to animation

2

I have a problem here and I already posted in the international stackoverflow, but no one answered me, so I'm posting here to see if there is any charitable soul to help me. Come on !!

I just wanted to do a listing of "tasks" so I used the following layout to list:

lauout / fragment_list.xml:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="match_parent"
   android:layout_height="match_parent">

<ProgressBar
    android:id="@+id/loading_progress"
    style="?android:attr/progressBarStyleLarge"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="gone"
    android:layout_gravity="center" />

<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/listView"
    android:layout_gravity="center_horizontal" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginTop="10dp"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textStyle="bold"
    android:id="@+id/no_item_message"
    android:visibility="invisible" />

</LinearLayout>

And here are the items that will be inserted in the list:

layout / task.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="horizontal"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:showDividers="middle"
 android:divider="?android:dividerVertical">

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/task_checkbox"
    android:clickable="true"
    android:enabled="true"
    android:singleLine="false"
    android:gravity="center_horizontal"
    android:layout_marginTop="20dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:visibility="visible"/>

<LinearLayout
 android:orientation="vertical"
 android:layout_width="245dp"
 android:layout_height="wrap_content"
 android:layout_marginLeft="10dp"
 android:id="@+id/task_container">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:layout_marginRight="10dp"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:hint="@string/title_task"
    android:id="@+id/title_task"
    android:layout_gravity="left|center_vertical" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="10dp"
    android:hint="@string/task_vinculado"
    android:id="@+id/task_vinculado"
    android:layout_gravity="left|center_vertical"
    android:layout_marginTop="5dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_marginRight="10dp"
    android:layout_marginBottom="5dp"
    android:hint="@string/limit_date"
    android:id="@+id/limit_date"
    android:layout_gravity="left|center_vertical" />
</LinearLayout>

<ImageButton
    android:id="@+id/delete_task_button"
    android:layout_marginLeft="5dp"
    android:layout_marginTop="15dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_action_discard"
    android:background="?android:selectableItemBackground"
    android:contentDescription="@string/action_remove_item" />

</LinearLayout>

Everything went as I wanted and my layout looked like this:

Layout Preview (the colors I've set up in Java code)

So I decided to make the layout animations described on the Android Developers website (just like here: developer.android.com/training/animation/layout.html)

Then I replace the layout / fragment_list.xml file with the following file:

layout / test.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

<ScrollView android:layout_width="match_parent"
  android:layout_height="match_parent">

<LinearLayout android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:showDividers="middle"
    android:divider="?android:dividerHorizontal"
    android:animateLayoutChanges="true"
    android:paddingLeft="5dp"
    android:paddingRight="5dp" />

</ScrollView>

<TextView
   android:id="@android:id/empty"
   style="?android:textAppearanceSmall"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_gravity="center"
   android:text="@string/no_task"
   android:padding="32dp"
   android:textColor="?android:textColorSecondary" />

</FrameLayout>

And I'm manipulating the List manually in a snippet like this:

TaskFragment.java: (the code is simplified for better understanding)

public class TaskFragment extends Fragment {
  private ViewGroup mContainerView;
  private View view;


  public TaskFragment() {
  }

  @Override
  public View onCreateView(final LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {

    view = inflater.inflate(R.layout.teste, container, false);
    assert view != null;

    mContainerView = (ViewGroup) view.findViewById(R.id.container);

    return view;
  }


  @Override
  public void onStart(){
     super.onStart();

    ContentResolver contentResolver = getActivity().getContentResolver();
    Uri uri = NectarContract.Task.CONTENT_URI;
    Cursor c = contentResolver.query(uri, NectarContract.Task.PROJECTION, null, null, null);
    assert c != null;
    while (c.moveToNext()) {
        Task task = new Task();
        task.setId(c.getInt(NectarContract.COLUMN_TASK_ID));
        task.setTitle(c.getString(NectarContract.COLUMN_TASK_TITLE));
        task.setVinculado(c.getString(NectarContract.COLUMN_TASK_VINCULADO));
        task.setEnd(c.getString(NectarContract.COLUMN_TASK_END));
        task.setPrioridade(c.getInt(NectarContract.COLUMN_TASK_PRIORIDADE));

        view.findViewById(android.R.id.empty).setVisibility(View.GONE);
        populate(task);
     }
  }

  private void populate(final Task task){
    final ViewGroup newView = (ViewGroup) LayoutInflater.from(getActivity().getApplicationContext()).inflate(R.layout.task, mContainerView, false);

    CheckBox checkbox = (CheckBox) newView.findViewById(R.id.task_checkbox);
    checkbox.setTag(task.getId());
    TextView title = (TextView) newView.findViewById(R.id.title_task);
    TextView vinculado = (TextView) newView.findViewById(R.id.task_vinculado);
    TextView end = (TextView) newView.findViewById(R.id.limit_date);
    LinearLayout container = (LinearLayout) newView.findViewById(R.id.task_container);

    title.setText(task.getTitle());
    if (task.getPrioridade() == 0) {
        title.setTextColor(Color.BLUE);
    }
    if (task.getPrioridade() == 1) {
        title.setTextColor(0xFFE29400);
    }
    if (task.getPrioridade() == 2) {
        title.setTextColor(Color.RED);
    }
    vinculado.setText("Vinculado a: " + task.getVinculado());
    end.setText("Data prevista: " + task.getEndString());

    container.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(getActivity().getApplicationContext(), TaskActivity.class);
            intent.putExtra(ARG_TASK, task);
            intent.putExtra(TaskActivity.USER, user);
            intent.putExtra(TaskActivity.METHOD, TaskActivity.UPDATE);
            startActivityForResult(intent, 1);
        }
    });
    newView.findViewById(R.id.delete_task_button).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mContainerView.removeView(newView);
            if (mContainerView.getChildCount() == 0) {
                view.findViewById(android.R.id.empty).setVisibility(View.VISIBLE);
            }
        }
    });
    mContainerView.addView(newView, 0);
  }
}

But using a list in this way has changed my layout settings. Look:

Layout Preview 2

I've done everything to change this, to get back the old look, and I did not get it at all. What I want is to continue with this layout that supports animation, but with the old look.

Note: all the changes I make in layout / task.xml, is not correct as shown in the preview of Android Studio, I have to go running the application and testing until I get the configuration I want, and there are still things that I can not change! Is this an animation API problem?

Thank you in advance for trying to help me in this !!

    
asked by anonymous 26.06.2014 / 21:01

1 answer

2

Change the Context passed to LayoutInflater , passing Application because there is probably loss of information in relation to Activity .

You can use:

LayoutInflater.from(getActivity());
// ou
getActivity().getLayoutInflater() // Sugerido nos comentários
// ou
(LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
26.06.2014 / 22:19