我正在尝试修改此代码以满足我的需求:http://udinic.wordpress.com/2011/09/03/expanding-listview-items/
我想更改它,以便根据用户的选择显示不同的TextView。我意识到,使用if-clauses并且它似乎工作得很好,显然。之后我想动态地更改TextViews的文本,但是失败了。 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;
}
}
}
答案 0 :(得分: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");
您当前拥有的行只会获得带有标题title1和title2的第一个视图。使用给定的view
按ID查找视图,您将获得点击视图中