问题描述
我有以下布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textGoesHere"
android:drawableRight="@drawable/row_receive"
android:text="hello alert" />
</RelativeLayout>
和
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textGoesHere"
android:drawableLeft="@drawable/row_send"
android:text="hello alert" />
</LinearLayout>
我正在尝试创建一个显示这两种布局的 ListView。这是我的 java 代码,它具有主类、一个 Message 类,用于存储来自 EditText 的数据,用户填写并应该填充 ListView,最后是自定义 Adapter 类:
package com.example.androidlabs;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class ChatRoomActivity extends AppCompatActivity {
List<String> listElements = new ArrayList<String>();
ListView theListView;
Button sendButton;
Button receiveButton;
EditText chatText;
Adapter adapter;
Message message = new Message();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat_room);
theListView = (ListView) findViewById(R.id.theListView);
sendButton = (Button) findViewById(R.id.sendButton);
chatText = (EditText) findViewById(R.id.chatText);
receiveButton = (Button) findViewById(R.id.receiveButton);
theListView.setAdapter(adapter = new Adapter());
// sendButton.setOnClickListener(click -> listElements.add("new element"));
sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
message.setMessage(chatText.getText().toString());
message.setType(0);
listElements.add(chatText.getText().toString());
adapter.notifyDataSetChanged();
}
});
receiveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
message.setMessage(chatText.getText().toString());
message.setType(1);
listElements.add(chatText.getText().toString());
adapter.notifyDataSetChanged();
}
});
}
public class Message {
private String text;
private int type;
public void setMessage(String text) { this.text = text; }
public String getMessage() { return text; }
public void setType(int type) { this.type = type; }
public int getType() {return type; }
}
public class Adapter extends BaseAdapter {
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
if (message.getType() == 0) { return 0; }
else { return 1; }
}
@Override
public int getCount() {
return listElements.size();
}
@Override
public Object getItem(int position) {
return listElements.get(position);
}
@Override
public long getItemId(int position) {
return (long) position;
}
@Override
public View getView(int position,View old,ViewGroup parent) {
View newView = old;
LayoutInflater inflater = getLayoutInflater();
int listViewItemType = this.getItemViewType(position);
//make a new row
if (newView == null) {
if (listViewItemType == 0) {
newView = inflater.inflate(R.layout.row_send_layout,parent,false);
}
else {
newView = inflater.inflate(R.layout.row_receive_layout,false);
}
}
//set what the text should be for this row:
TextView tView = newView.findViewById(R.id.textGoesHere);
tView.setText( getItem(position).toString() );
//return it to be put in the table
return newView;
}
}
}
我希望列表视图如下所示:
但最终发生的是它根据用户点击的按钮改变了 ListView 的所有布局:
The ListView I get if I click on "Send"
The ListView I get if I click on "Recieve"
我可以在我的代码中进行哪些调整以获得所需的结果?我假设它在我的 getView() 方法中,但我不知道那是什么。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)