このコードを自分のニーズに合わせて変更しようとしています: http://udic.wordpress.com/2011/09/03/expanding-listview-items/
ユーザーの選択に基づいて、異なる TextViews を表示するために、それを変更したいと思います。私は、if句を使用すると、どうやら正常に動作するように見えることに気付きました。その後、TextViews のテキストを動的に変更したかったのですが、失敗しました。setText は最初の項目に対してのみ機能します。2 つ目は、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;
}
}
アニメーション クラス:
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;
}
}
}