添加新值时,自定义列表视图值是否会波动?

时间:2017-05-26 10:30:59

标签: java android listview

我正在尝试聊天应用程序。发送应该在右侧的消息时,收到的消息应该在左侧。我做得很好。当我在列表视图中添加新值时,先前的值已经波动。任何人都可以解决我的问题吗?我附上了我的源代码。

主要活动:

public class MainActivity extends AppCompatActivity {
ArrayList<ChatData> data = new ArrayList<ChatData>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final ListView listView = (ListView) findViewById(R.id.list_item);

    final ChatAdaptor chatAdaptor = new ChatAdaptor(this, getDetails());
    listView.setAdapter(chatAdaptor);
    listView.setDivider(null);

    ImageView imageView = (ImageView) findViewById(R.id.submit);
    imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            EditText objText = (EditText) findViewById(R.id.text_msg);

          chatAdaptor.add(new ChatData(objText.getText().toString(), false));
            listView.setSelection(chatAdaptor.getCount() - 1);
            objText.setText(null);
          //  chatAdaptor.notifyDataSetChanged();
        }
    });
}

public ArrayList<ChatData> getDetails() {
    data.add(new ChatData("Hi! Agent", false));
    data.add(new ChatData("Hi! Agent ,Are you there ", false));
    data.add(new ChatData("Yeah tell me how can I help you", true));
    data.add(new ChatData("I am getting wrong balance", false));
    return data;

}

}

自定义适配器:

public class ChatAdaptor extends ArrayAdapter<ChatData> {
private Activity activity;

String TAG = "bks";

private List<ChatData> originalData = null;


public ChatAdaptor(Activity activity, ArrayList<ChatData> objects) {
    super(activity, 0, objects);
    this.activity = activity;
    this.originalData = objects;

}

@Override
//called when rendering the list
public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder holder;

    if (convertView == null) {
        // convertView = mInflater.inflate(R.layout.search_data, true,null);
        LayoutInflater inflater = activity.getLayoutInflater();
        convertView = inflater.inflate(R.layout.test, null, false);


        if (originalData.get(position).isAgentMsg())
            convertView = inflater.inflate(R.layout.show_chat_agent, null, true);
        else
            convertView = inflater.inflate(R.layout.show_chat_client, null, true);


        // Creates a ViewHolder and store references to the three views
        // we want to bind data to.

        holder = new ViewHolder();
        holder.objMsg = ( TextView ) convertView.findViewById(R.id.msg) ;


        // Bind the data efficiently with the holder.

        convertView.setTag(holder);
    } else {
        // Get the ViewHolder back to get fast access to the TextView
        holder = (ViewHolder) convertView.getTag();
    }
    holder.objMsg.setText(originalData.get(position).getMsg());


    return convertView;
}


static class ViewHolder {
    TextView objMsg;
    /**  TextView objClientText, objAgentText;
     ImageView objClientImage1, objClientImage2;
     ImageView objAgentImage1, objAgentImage2;
     */
}

}

谢谢你 Kalanidhi M

1 个答案:

答案 0 :(得分:1)

只需在getView方法

中更改此行即可
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.test, parent, true);


if (originalData.get(position).isAgentMsg())
            convertView = inflater.inflate(R.layout.show_chat_agent, parent, true);
        else
            convertView = inflater.inflate(R.layout.show_chat_client, parent, true);