2012-04-23 109 views
0

我做了項目點擊動畫的列表視圖,我使用數組適配器的數據輸入和數組適配器我打電話動畫類的項目點擊保持點擊變量clickedposition -1那裏我打電話動畫....所有細節都在代碼.....
`列表視圖OnitemClick動畫

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> 

` 在第一次點擊它工作正常,但在第二次我點擊它來了同一項目的不規則..........

回答

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

這應該工作你的情況

+0

感謝你的silwar爲你的回答我得到了另一個邏輯.....非常感謝 – 2012-04-24 06:51:04

+0

你能分享另一個邏輯嗎? – silwar 2012-04-25 12:13:18

+0

@silwar +1,謝謝你對我的幫助。 – 2012-05-08 07:04:50