2

Hi guys I have a problem with the cool lib from nhaarman

I integrated successfully his lib with an activity extends ListActivity

public class MainActivity extends ListActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getListView().setDivider(null);

        MyAdapter adapter = new MyAdapter();


        //setListAdapter(adapter);

        ScaleInAnimationAdapter swingRightInAnimationAdapter = new ScaleInAnimationAdapter(adapter);
        swingRightInAnimationAdapter.setAbsListView(getListView());

        setListAdapter(swingRightInAnimationAdapter);

        for(int i = 0 ; i<10; i++)
        {
            adapter.add("task" + String.valueOf(i));
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private class MyAdapter extends ArrayAdapter<String>
    {

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView tv = (TextView) convertView;
            if (tv == null) {
                tv = new TextView(MainActivity.this);
            }
            tv.setText(getItem(position));
            return tv;
        }
    }
}

This code above is working fine. The list_rows are fading in the screen.

But now I want to integrate this lib into a fragment with a listview within:

public class FragmentTask extends Fragment{

    ListView listView;
    MyAdapter adapter;
    View view;
    public FragmentTask() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Log.d(new Exception().getStackTrace()[0].getClassName(), new Exception().getStackTrace()[0].getMethodName());
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_tasks, container, false);

        Button button = (Button) view.findViewById(R.id.btn_newTask);

        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                adapter.add("task");
                //adapter.notifyDataSetChanged();
            }
        });

        listView = (ListView) view.findViewById(R.id.taskListView);
        listView.setDivider(null);

        adapter = new MyAdapter(view.getContext());

        ScaleInAnimationAdapter swingRightInAnimationAdapter = new ScaleInAnimationAdapter(adapter);
        swingRightInAnimationAdapter.setAbsListView(listView);

        listView.setAdapter(swingRightInAnimationAdapter);

        return view;
    }

    private class MyAdapter extends ArrayAdapter<String>
    {
        Context context;

        private MyAdapter(Context context) {
            this.context = context;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View row = inflater.inflate(R.layout.list_row,parent, false);
            TextView title = (TextView) row.findViewById(R.id.textView);

            title.setText(getItem(position));

            return row;
        }
    }

Here is the xml fragment_tasks:

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

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent" android:layout_height="match_parent"
 android:background="#ffc7c7c7">


  <Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="New Task"
    android:id="@+id/btn_newTask"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

 <ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/taskListView"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_above="@+id/btn_newTask"
   />

   </RelativeLayout>

Now I have the effect that the rows only fade in if i "spam" new rows so that I have to scroll down. So the list items which were hided appear now with the fade-effect. But the other list items which have room in the listview without scrolling are not fading.

Do you understand me? Please help me :-)

Michael L Perry
  • 7,327
  • 3
  • 35
  • 34
lephtism
  • 23
  • 3

1 Answers1

0

I had the same problem, but the problem is not the fragment. The height of list_view must be match_parent.

<ListView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:id="@+id/taskListView"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_above="@+id/btn_newTask"
   />

description of the problem

Hope that helps

Javier Cruz
  • 104
  • 1
  • 2