来自nhaarman的ListViewAnimation片段

时间:2014-06-24 06:28:20

标签: android listview animation fragment

嗨,大家好我从nhaarman的酷lib中遇到了问题

我成功地将他的lib与活动扩展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;
        }
    }
}

上面的代码工作正常。 list_rows在屏幕上淡出。

但是现在我想将这个lib集成到一个带有listview的片段中:

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;
        }
    }

这是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>

现在我的效果是,只有当我发送垃圾邮件时,行才会淡入淡出。新行,所以我必须向下滚动。因此,隐藏的列表项目现在显示为淡入淡出效果。但是列表视图中没有滚动空间的其他列表项目并没有消失。

你了解我吗? 请帮帮我: - )

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,但问题不在于片段。 list_view的高度必须是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

希望有所帮助