2013-02-21 90 views
2

我試圖修改此代碼適合我的需求:​​TextView中的setText不工作中的LinearLayout

我想改變它,以顯示不同的TextViews,根據用戶的選擇。我意識到,if-clause和它似乎很好,顯然。之後我想動態更改TextView的文本,失敗了。 setText僅適用於第一項。第二個總是顯示在XML中設置的靜態文本...

謝謝!

監聽器列表視圖:

 list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, final View view, int position, long id) { 


      ((TextView) findViewById(R.id.title1)).setText("Custom1"); 
      ((TextView) findViewById(R.id.title2)).setText("Custom2"); 
      String selectedFromList =(String) (list.getItemAtPosition(position)); 

      if (selectedFromList.equals("udini1")) { 
       View toolbar1 = view.findViewById(R.id.toolbar1); 

       // Creating the expand animation for the item 
       ExpandAnimation expandAni1 = new ExpandAnimation(toolbar1, 500); 

       // Start the animation on the toolbar 
       toolbar1.startAnimation(expandAni1); 

      } 

      if (selectedFromList.equals("udini2")) { 
       View toolbar2 = view.findViewById(R.id.toolbar2); 

       // Creating the expand animation for the item 
       ExpandAnimation expandAni2 = new ExpandAnimation(toolbar2, 500); 

       // Start the animation on the toolbar 
       toolbar2.startAnimation(expandAni2); 


      } 



     } 
    }); 
} 

list_item.xml

<LinearLayout android:id="@+id/toolbar1" 
     android:layout_marginBottom="-50dip" 
     android:visibility="gone" 
     android:layout_height="50dip" 
     android:layout_width="fill_parent"> 
    <TextView android:id="@+id/title1" 
      android:padding="20dip" 
      android:focusable="false" 
      android:focusableInTouchMode="false" 
      android:layout_width="fill_parent" 
      android:text="Dummy1" 
      android:layout_height="wrap_content"/> 
</LinearLayout> 

    <LinearLayout android:id="@+id/toolbar2" 
     android:layout_marginBottom="-50dip" 
     android:visibility="gone" 
     android:layout_height="50dip" 
     android:layout_width="fill_parent"> 
    <TextView android:id="@+id/title2" 
      android:padding="20dip" 
      android:focusable="false" 
      android:focusableInTouchMode="false" 
      android:layout_width="fill_parent" 
      android:text="Dummy2" 
      android:layout_height="wrap_content"/> 
</LinearLayout> 

CustomListAdapter:

class CustomListAdapter extends ArrayAdapter<String> { 
     public CustomListAdapter(Context context, int textViewResourceId) { 
      super(context, textViewResourceId); 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 

      if (convertView == null) { 
       convertView = getLayoutInflater().inflate(R.layout.list_item, null); 
      } 

      ((TextView)convertView.findViewById(R.id.title)).setText(getItem(position)); 

      // Resets the toolbar to be closed 
      View toolbar = convertView.findViewById(R.id.toolbar1); 
      ((LinearLayout.LayoutParams) toolbar.getLayoutParams()).bottomMargin = -50; 
      toolbar.setVisibility(View.GONE); 

      return convertView; 
     } 
    } 

AnimationClass:

public class ExpandAnimation extends Animation { 
    private View mAnimatedView; 
    private LayoutParams mViewLayoutParams; 
    private int mMarginStart, mMarginEnd; 
    private boolean mIsVisibleAfter = false; 
    private boolean mWasEndedAlready = false; 
    /** 
    * Initialize the animation 
    * @param view The layout we want to animate 
    * @param duration The duration of the animation, in ms 
    */ 
    public ExpandAnimation(View view, int duration) { 
     setDuration(duration); 
     mAnimatedView = view; 
     mViewLayoutParams = (LayoutParams) view.getLayoutParams(); 

     // decide to show or hide the view 
     mIsVisibleAfter = (view.getVisibility() == View.VISIBLE); 

     mMarginStart = mViewLayoutParams.bottomMargin; 
     mMarginEnd = (mMarginStart == 0 ? (0- view.getHeight()) : 0); 

     view.setVisibility(View.VISIBLE); 
    } 

    @Override 
    protected void applyTransformation(float interpolatedTime, Transformation t) { 
     super.applyTransformation(interpolatedTime, t); 

     if (interpolatedTime < 1.0f) { 

      // Calculating the new bottom margin, and setting it 
      mViewLayoutParams.bottomMargin = mMarginStart 
        + (int) ((mMarginEnd - mMarginStart) * interpolatedTime); 

      // Invalidating the layout, making us seeing the changes we made 
      mAnimatedView.requestLayout(); 

     // Making sure we didn't run the ending before (it happens!) 
     } else if (!mWasEndedAlready) { 
      mViewLayoutParams.bottomMargin = mMarginEnd; 
      mAnimatedView.requestLayout(); 

      if (mIsVisibleAfter) { 
       mAnimatedView.setVisibility(View.GONE); 
      } 
      mWasEndedAlready = true; 
     } 
    } 
} 

回答

1

嘗試改變

((TextView) findViewById(R.id.title1)).setText("Custom1"); 
((TextView) findViewById(R.id.title2)).setText("Custom2"); 

這個

((TextView) view.findViewById(R.id.title1)).setText("Custom1"); 
((TextView) view.findViewById(R.id.title2)).setText("Custom2"); 

您目前只得到與IDS TITLE1和標題2的第一個視圖的線條。使用給定的view按ID查找視圖,然後您將獲得點擊視圖中以內的id爲title1 的第一個視圖。

+0

謝謝!工作:) – mailman 2013-02-21 12:58:01