gpt4 book ai didi

java - TextView setText 在 LinearLayout 中不起作用

转载 作者:行者123 更新时间:2023-11-30 11:35:37 25 4
gpt4 key购买 nike

我正在尝试修改此代码以满足我的需要:http://udinic.wordpress.com/2011/09/03/expanding-listview-items/

我想更改它,以便根据用户的选择显示不同的 TextView。我意识到,使用 if 子句,它似乎工作正常,显然。之后我想动态更改 TextViews 的文本,但失败了。 setText 仅适用于第一项。第二个始终显示 XML 中设置的静态文本 ...

谢谢!

ListView 上的监听器:

       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>

自定义列表适配器:

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

最佳答案

尝试改变

((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");

您当前拥有的行仅获取 ID 为 title1 和 title2 的第一个 View 。使用给定的 view 按 id 查找 View ,您将获得第一个 id title1 的 View inside 被点击的 View 。

关于java - TextView setText 在 LinearLayout 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15002579/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com