应用程序根据情况选择布局

时间:2015-05-17 21:06:04

标签: android

我正在制作一个消息传递Android应用程序,但我的问题是,我如何让应用程序选择选择右手布局还是左手布局?就像我们在WhatsApp等消息应用中看到的那样,如果我是发送者,我的消息被包裹在右手布局中,而正在聊天的人消息被包裹在左手布局中,我如何实现这种类型的界面或布局我应该说?

以下是我的代码,app简单地从远程数据库接收数据并使用自定义适配器将其放入listview中:

// the Data being set to the adapter
private ArrayList<ListItem> getListData() {
    ArrayList<ListItem> listMockData = new ArrayList<ListItem>();
    String[] username = mUsername.toArray(new String[mUsername.size()]);
    String[] comments = Comment.toArray(new String[Comment.size()]);

    for (int i = 0; i < username.length; i++) {

        ListItem newsData = new ListItem();

        newsData.setUsername(username[i]);
        newsData.setmComment(comments[i]);

        listMockData.add(newsData);
        System.gc();
    }
    return listMockData;
}

我的自定义适配器使用单一视图:

public class CommentAdapter extends BaseAdapter{
    private ArrayList<ListItem> listData;
    private LayoutInflater layoutInflater;
    ViewHolder holder;
    public CommentAdapter(Context context, ArrayList<ListItem> listData) {
        this.listData = listData;
        notifyDataSetChanged();
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return listData.size();
    }

    @Override
    public Object getItem(int position) {
        return listData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.singlecomment, null);
            holder = new ViewHolder();
            holder.username = (TextView) convertView.findViewById(R.id.usernme);
            holder.message = (TextView) convertView.findViewById(R.id.postCommentBox);

            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        ListItem newsItem = listData.get(position);
        holder.username.setText(newsItem.getUsername());
        holder.message.setText(newsItem.getmComment());

        return convertView;
    }

    static class ViewHolder {
        TextView username;
        TextView message;    
    }    
}

1 个答案:

答案 0 :(得分:0)

假设您的ListItem对象有一个名为isSender的字段,以检查他是否是发件人。

public View getView(int position, View convertView, ViewGroup parent) {
  if (convertView == null) {
    convertView = layoutInflater.inflate(R.layout.singlecomment, null);
    holder = new ViewHolder();
    holder.username = (TextView) convertView.findViewById(R.id.usernme);
    holder.message = (TextView) convertView.findViewById(R.id.postCommentBox);

    convertView.setTag(holder);

  } else {
    holder = (ViewHolder) convertView.getTag();
  }

  ListItem newsItem = listData.get(position);
  if(newsItem.getIsSender()) {
    holder.usernameRight.setText(newsItem.getUsername());
    holder.messageRight.setText(newsItem.getmComment());
    holder.usernameLeft.setVisibility(View.GONE); // or something like that to hide
    holder.messageLeft.setText(View.GONE);
  } else {
    holder.usernameLeft.setText(newsItem.getUsername());
    holder.messageLeft.setText(newsItem.getmComment());
    holder.usernameRight.setVisibility(View.GONE);
    holder.messageRight.setText(View.GONE);
  }

  return convertView;
}