gpt4 book ai didi

java - 以编程方式更改 ListView 行中对象的顺序

转载 作者:行者123 更新时间:2023-12-02 07:27:43 25 4
gpt4 key购买 nike

目前我有:

    <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="15:22:33 \nApr 13 1999"
android:textColor="#FFFFFFFF"

android:textAppearance="?android:attr/textAppearanceSmall" />

<TextView
android:id="@+id/comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dip"
android:background="@drawable/incoming_message_buble"
android:paddingLeft="10dip"
android:text="Hello bubbles!"
android:textColor="@android:color/primary_text_light" />

而且效果很好。

这里是适配器代码:

public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.activity_sms_row_layout, parent, false);
}

wrapper = (LinearLayout) row.findViewById(R.id.wrapper);

OneComment coment = getItem(position);

countryName = (TextView) row.findViewById(R.id.comment);

countryName.setText(coment.comment);


countryName.setBackgroundResource(coment.left ? R.drawable.incoming_message_buble : R.drawable.outgoing_message_buble);
wrapper.setGravity(coment.left ? Gravity.LEFT : Gravity.RIGHT);

return row;
}

我必须改变顺序,对于左重力,我需要首先显示textview注释,然后显示textView1对于重力右,保留 xml 设置。

请指教。

最佳答案

我建议使用多种布局,而不是不断地重新组织每一行的布局。重写这两个方法:

@Override
public int getItemViewType(int position) {
return getItem(position).left ? 0 : 1;
}

@Override
public int getViewTypeCount() {
return 2;
}

并修改您的getView():

@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(getItemViewType == 0) {
// Use the left layout
} else {
// Use the right layout
}
}

关于java - 以编程方式更改 ListView 行中对象的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13316605/

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