列表视图OnitemClick动画

时间:2012-04-23 08:29:35

标签: android

我做了一个列表视图,在项目点击上有动画,我使用数组适配器进行数据输入,在数组适配器中,我在项目点击中调用动画类,保持点击变量clickedposition -1,我调用动画.. ..所有细节都在代码中......
`

public class TaskListAdapter extends ArrayAdapter<FTask> {

    protected FTask[] values= null;
    protected Context context = null;
    private int clickedPosition = -1;

    public TaskListAdapter(Context context,FTask[] values) {
        this(context, R.layout.task_row, values);
    }
    public TaskListAdapter(Context context, int taskRow, FTask[] values) {
        super(context, taskRow, values);
        this.values = values;
        this.context = context;

    }
    public void toggle(int position) {
        //If its already opened. Close it. Else open it.
        if(clickedPosition == position){
            clickedPosition = -1;
        }else{
            clickedPosition = position;
        }
        notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        if (convertView == null) {
            row = View.inflate(context, R.layout.task_row, null);
            LinearLayout single_message_holder = (LinearLayout) row
                    .findViewById(R.id.single_task_holder);
            single_message_holder.setBackgroundColor(Color.WHITE);
        }

        final FTask tsk = values[position];
        TextView service_description = (TextView) row.findViewById(R.id.task_description);
        service_description.setText(tsk.getBody());


        TextView service_detail_description = (TextView) row.findViewById(R.id.task_detail_description);
        service_detail_description.setText(tsk.getBody());
        LinearLayout ani=(LinearLayout)row.findViewById(R.id.ani);


        if (clickedPosition == position) {

            Log.d("****Position*****",String.valueOf(position));
            //service_detail_description.setAnimation(AnimationUtils.loadAnimation(context, 
              //      R.anim.push_up_in));
            ani.startAnimation(new VisibleAni(1.0f, 1.0f, 0.0f, 1.0f, 800, ani, true,true));
            ani.setVisibility(View.VISIBLE);
            service_description.setVisibility(View.GONE);
            Log.d("SENDING TASK STATUS", "AGAINNNNN");
            sendTaskStatus(tsk);

        } else {
            //ani.startAnimation(new VisibleAni(1.0f, 1.0f, 0.0f, 1.0f, 800, ani, true,false));
            ani.setVisibility(View.GONE);
            service_description.setVisibility(View.VISIBLE);

        }

        ImageView icon = (ImageView) row.findViewById(R.id.taskicon);
        if (tsk.isDone()==1) {
            icon.setImageResource(R.drawable.done);
            service_description.setPaintFlags(service_description.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
        } else {
            icon.setImageResource(R.drawable.undone);
            service_description.setPaintFlags(service_description.getPaintFlags() & ~Paint.STRIKE_THRU_TEXT_FLAG );
        }

        icon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(tsk.isDone()==0){
                    tsk.setDone(1);
                    notifyDataSetChanged();
                }
                else{
                    tsk.setDone(0);
                    notifyDataSetChanged();
                }
            }
        });
        return row;
    }
    private void sendTaskStatus(FTask tsk){
        //TODO pathch, 
        Log.e("TaskListAdapter",    "Set status");
        if (LoginManager.getSessionId() == null ||LoginManager.getSessionId().equals("DUMMY") || LoginManager.getSessionId().equals("")){
            Log.e("INVALID USER_ID or SESSION_ID",  "Not connectind to network");
            return;
        }
        NetworkDataFetcher fetcher = new NetworkDataFetcher(NetworkDataFetcher.LINK_DATA_ACCESS);

        try {
            String response = fetcher.execute(new String[] { MessageManager.MSG_JSON_ID_TYPE, MessageManager.MSG_JSON_ID_SESSIONID, MessageManager.MSG_JSON_ID_MID},
                    new String[] {"message_status", LoginManager.getSessionId(), tsk.getTskid() }).get();

            if(response == null){
                Log.e("TaskListAdapter", "response null");
                return;
            }
            Log.w("TasksListAdapter", "Response = "+response);
            return;

        } catch (InterruptedException e) {
            Log.d("InterrupptedException", "InterrupptedException");
            return;
        } catch (ExecutionException e) {
            Log.d("ExecutionException", "ExecutionException");
            return;
        }
    }
}

And the Animation Class

import android.view.View;
import android.view.animation.ScaleAnimation;
import android.view.animation.Transformation;
import android.widget.LinearLayout.LayoutParams;

public class VisibleAni extends ScaleAnimation {
    private View mView;

    private LayoutParams mLayoutParams;

    private boolean mclick;

    private int mMarginBottomFromY, mMarginBottomToY;

    private boolean mVanishAfter = false;

    public VisibleAni(float fromX, float toX, float fromY, float toY, int duration, View view,
            boolean vanishAfter,boolean click) {
        super(fromX, toX, fromY, toY);
        setDuration(duration);
        mView = view;
        mclick = click;
        mVanishAfter = vanishAfter;
        mLayoutParams = (LayoutParams) view.getLayoutParams();
        int height = mView.getHeight();
        mMarginBottomFromY = (int) (height * fromY) + mLayoutParams.bottomMargin - height;
        mMarginBottomToY = (int) (0 - ((height * toY) + mLayoutParams.bottomMargin)) - height;
        if (!mclick) {
            mView.setVisibility(View.GONE); 
        }

    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        super.applyTransformation(interpolatedTime, t);
        if (interpolatedTime < 1.0f) {
            int newMarginBottom = mMarginBottomFromY + (int) ((mMarginBottomToY - mMarginBottomFromY) * interpolatedTime);
            mLayoutParams.setMargins(mLayoutParams.leftMargin, mLayoutParams.topMargin,
                mLayoutParams.rightMargin, newMarginBottom);
            mView.getParent().requestLayout();
        } else if (mVanishAfter ) {
            mView.setVisibility(View.VISIBLE);
        }

    }

}    

And the XML file related to this is ..........

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:descendantFocusability="blocksDescendants"
    >
    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/single_task_holder"
        android:gravity="top|left"
        android:paddingRight="10dp"
        android:paddingLeft="0dp"
        android:paddingTop="0dp"
        android:background="@color/white"
        android:orientation="horizontal"

        >

         <LinearLayout
             android:id="@+id/task_icon_lay"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:orientation="horizontal" >

                 <ImageView 
                    android:id="@+id/taskicon"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="top"        

                    />


                 </LinearLayout>
                 <LinearLayout android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:orientation="vertical">
                <TextView
                    android:id="@+id/task_description"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:layout_width="fill_parent" 
                    android:layout_height="fill_parent"
                    android:paddingBottom="10dp"
                    android:paddingLeft="10dp"
                    android:singleLine="true"
                    android:ellipsize="end"
                    android:paddingRight="20dp"
                    android:textSize="16dp"
                    android:textColor="@color/body_color"
                    />
                </LinearLayout>
                <LinearLayout android:layout_width="fill_parent"
                     android:layout_height="wrap_content"
                     android:id="@+id/ani"
                     android:orientation="vertical">
                <TextView
                    android:id="@+id/task_detail_description"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:background="@android:drawable/edit_text"
                    android:layout_width="fill_parent" 
                    android:layout_height="fill_parent"
                    android:paddingBottom="15dp"
                    android:paddingLeft="15dp"
                    android:paddingTop="15dp"
                    android:paddingRight="20dp"
                    android:textSize="16dp"
                    android:textColor="@color/body_color"
                    />

                </LinearLayout>

     </LinearLayout>


</LinearLayout>

` 在第一次点击它工作正常,但在第二次我点击相同的项目它不规则..........

1 个答案:

答案 0 :(得分:5)

而不是动画视图(在您的情况下为LinearLayout),为什么不通过将OnItemClickListener设置为ListView来为视图设置动画

这将按如下方式完成

ListView lv = (ListView)findViewById(R.id.listView);
lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
                    final View v = arg1;
                    LinearLayout ani = (LinearLayout)v.findViewById(R.id.ani);
                    ani.startAnimation(new VisibleAni(1.0f, 1.0f, 0.0f, 1.0f, 800, ani, true,true));
                    ani.setVisibility(View.VISIBLE);
                    }
}

这适用于您的情况