android - 在 Android 回收器 View 中以编程方式左右对齐 View

标签 android android-recyclerview

我正在使用 android 回收器 View 创建聊天布局

Activity 主

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="ch.uffapp.recyclerview.MainActivity">

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="100dp" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="50dp">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="opponent text message"
        android:id="@+id/opponent_send_text_message"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="opponent image message"
        android:id="@+id/opponent_send_image_message"/>

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_alignParentBottom="true">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="own text message"
        android:id="@+id/own_send_text_message"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="own image message"
        android:id="@+id/own_send_image_message" />

</LinearLayout>

</RelativeLayout>

recycler_view_item

<RelativeLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">

<LinearLayout android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:id="@+id/private_dialog_item_view">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@android:color/white"
            android:id="@+id/private_dialog_message_view"/>

        <ImageView
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:scaleType="centerCrop"
            android:id="@+id/private_dialog_image_view"/>

    </RelativeLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_gravity="right">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@android:color/white"
            android:id="@+id/private_dialog_time_view"
            android:text="09:00 pm"
            android:layout_marginRight="10dp"
            android:textSize="10dp"
            android:layout_marginBottom="5dp"/>

        <ImageView
            android:layout_width="10dp"
            android:layout_height="10dp"
            android:src="@drawable/ic_message_read_icon"
            android:layout_marginRight="5dp"/>

    </LinearLayout>

</LinearLayout>

</RelativeLayout>

我的适配器

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> 
{

private Context context;
private List<UffMessage> uffMessages = new ArrayList<>();

public MyAdapter(Context context, List<UffMessage> uffMessages) {
    this.context = context;
    this.uffMessages = uffMessages;
}

public class MyViewHolder extends RecyclerView.ViewHolder {

    private TextView messageView;
    private TextView timeView;
    private ImageView imageView;
    private LinearLayout itemBOdy;

    public MyViewHolder(View itemView) {
        super(itemView);
        messageView = (TextView) 
itemView.findViewById(R.id.private_dialog_message_view);
        timeView = (TextView) 
itemView.findViewById(R.id.private_dialog_time_view);
        imageView = (ImageView) 
itemView.findViewById(R.id.private_dialog_image_view);
        itemBOdy = (LinearLayout) 
itemView.findViewById(R.id.private_dialog_item_view);
    }

}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.recycler_view_item, parent, false);
    return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    resetUI(holder);
    UffMessage uffMessage = uffMessages.get(position);

    if (uffMessage.isOwnMessage) {
        makeOwn(holder);
    } else {
        makeOpponent(holder);
    }



    switch (uffMessage.messageType) {
        case "text":
            holder.messageView.setVisibility(View.VISIBLE);
            holder.messageView.setText(uffMessage.textMessage);
            holder.timeView.setText(uffMessage.messageTime);
            break;
        case "image":
            holder.imageView.setVisibility(View.VISIBLE);
            holder.imageView.setImageResource(R.drawable.edge6);
            holder.timeView.setText(uffMessage.messageTime);
            break;
    }
}

@Override
public int getItemCount() {
    return uffMessages.size();
}

private void resetUI(MyViewHolder holder) {
    holder.messageView.setVisibility(View.GONE);
    holder.imageView.setVisibility(View.GONE);
}

private void makeOpponent(MyViewHolder holder) {
    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) 
holder.itemBOdy.getLayoutParams();
    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
    holder.itemBOdy.setLayoutParams(params);

holder.itemBOdy.setBackgroundColor(context.getResources()
.getColor(R.color.colorPrimary));
}

private void makeOwn(MyViewHolder holder) {
    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) 
holder.itemBOdy.getLayoutParams();
    params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    holder.itemView.setLayoutParams(params);

holder.itemBOdy.setBackgroundColor(context.getResources()
.getColor(R.color.colorAccent));
}

}

主要 Activity

public class MainActivity extends AppCompatActivity implements 
View.OnClickListener {

private Button ownTextMessageBtn;
private Button ownImageMessageBtn;
private Button opponentTextMessageBtn;
private Button opponentImageMessageBtn;
private RecyclerView recyclerView;
private LinearLayoutManager layoutManager;
private List<UffMessage> uffMessages = new ArrayList<>();
private MyAdapter myAdapter;

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

    ownTextMessageBtn = (Button) findViewById(R.id.own_send_text_message);
    ownImageMessageBtn = (Button) findViewById(R.id.own_send_image_message);
    opponentTextMessageBtn = (Button) 
findViewById(R.id.opponent_send_text_message);
    opponentImageMessageBtn = (Button) 
findViewById(R.id.opponent_send_image_message);
    layoutManager = new LinearLayoutManager(this);
    layoutManager.setStackFromEnd(true);
    recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
    recyclerView.setLayoutManager(layoutManager);
    ownTextMessageBtn.setOnClickListener(this);
    ownImageMessageBtn.setOnClickListener(this);
    opponentTextMessageBtn.setOnClickListener(this);
    opponentImageMessageBtn.setOnClickListener(this);

    myAdapter = new MyAdapter(getApplicationContext(), uffMessages);
    recyclerView.setAdapter(myAdapter);

}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.own_send_text_message:
            uffMessages.add(new UffMessage("hi how are you", "12:00 pm", 
R.drawable.edge6, "text", true));
            myAdapter.notifyDataSetChanged();
            break;

        case R.id.own_send_image_message:
            uffMessages.add(new UffMessage("hi how are you", "12:00 pm", 
R.drawable.edge6, "image", true));
            myAdapter.notifyDataSetChanged();
            break;

        case R.id.opponent_send_text_message:
            uffMessages.add(new UffMessage("hi how are you", "12:00 pm", 
R.drawable.edge6, "text", false));
            myAdapter.notifyDataSetChanged();
            break;

        case R.id.opponent_send_image_message:
            uffMessages.add(new UffMessage("hi how are you", "12:00 pm", 
R.drawable.edge6, "image", false));
            myAdapter.notifyDataSetChanged();
            break;
    }
}
}

但问题是有些 View 会自动扩展自身,我想将它们左右对齐而不扩展它们。

enter image description here

最佳答案

View 会被回收,因此在滚动一段时间后规则

params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

堆叠在一个 View 上,导致 View 拉伸(stretch)。

为什么不使用简单易懂的 FrameLayout 替换低效的 RelativeLayout 并使用重力呢?

final FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) holder.itemBOdy.getLayoutParams();
params.gravity = GravityCompat.START; // or GravityCompat.END
holder.itemBOdy.setLayoutParams(params);

关于android - 在 Android 回收器 View 中以编程方式左右对齐 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44140138/

相关文章:

android - Google 游戏登录状态未在之前的 Activity 中共享

android - 当两个用户/ friend 彼此靠近时发出警报 - Android Proximity

android - 将项目添加到我的 ArrayList 不会正确更新 ListView

java - 使按钮功能像键盘一样

android - RecyclerView滚动监听的方法onscolled自动调用不滚动

android - 将 RecyclerView 中的 CardView 扩展为新 Activity ,例如 Google Android 的收件箱

android - 单击项目时没有显示 toast RecyclerView

java - 如何在 ListView 中显示firebase数据?

java - 在 Soap Web 服务响应中获取 Json 数据

java - 不知道如何为 RecyclerView 注入(inject)适配器