Android数据绑定recyclelerView上有多行

时间:2017-03-16 09:00:24

标签: android

我试图找到如何实现和使用带有多行的数据绑定recyclelerview,我创建了两行,其中一行支持从左到右,另一行支持从右到左,就像android聊天应用程序一样作为电报或什么应用程序。使用数据绑定功能,我无法实现此功能以具有多行

我的适配器:

public class RobotMessagesAdapter extends RecyclerView.Adapter<RobotViewHolder> {
    private Context              context;
    private List<RobotViewModel> list;
    private LayoutInflater             inflater;

    public RobotMessagesAdapter(Context context, List<RobotViewModel> robotViewModels) {
        this.context = context;
        this.list = robotViewModels;
    }

    @Override
    public RobotViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(inflater==null){
            inflater = LayoutInflater.from(parent.getContext());
        }
        RobotDataBinding dataBinding = RobotDataBinding.inflate(inflater, parent,false);
        return new RobotViewHolder(dataBinding);
    }

    @Override
    public void onBindViewHolder(RobotViewHolder holder, int position) {
        final RobotViewModel model = list.get(position);
        holder.bind(model);

        RobotDataBinding dataBinding = holder.getRobotDataBinding();
    }

    @Override
    public int getItemViewType(int position) {
        return list.get(position).getMessageType();
    }

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

我的RecyclerView.ViewHolder

public class RobotViewHolder extends RecyclerView.ViewHolder {
    private RobotDataBinding robotDataBinding;

    public RobotViewHolder(RobotDataBinding mRobotDataBinding) {
        super(mRobotDataBinding.getRoot());
        robotDataBinding = mRobotDataBinding;
    }

    public void bind(RobotViewModel model) {
        robotDataBinding.setViewModel(model);
    }

    public RobotDataBinding getRobotDataBinding() {
        return robotDataBinding;
    }
}

以下xmls必须在recyclerview

上实施

我的user.xml:

    <layout xmlns:android="http://schemas.android.com/apk/res/android">
        <data class="UserMessagesDataBinding">
            <variable
                name="viewModel"
type="ir.pishguy.cafeAlachiq.Ui.Register.Robot.Model.RobotViewModel">
            </variable>
        </data>

        <LinearLayout
            ...
            <LinearLayout
                ...
            </LinearLayout>

            <LinearLayout
                ...
            </LinearLayout>

        </LinearLayout>
    </layout>

和我的robot.xml:

<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data class="RobotDataBinding">
        <variable
            name="viewModel"
           type="ir.pishguy.cafeAlachiq.Ui.Register.Robot.Model.RobotViewModel">
        </variable>
    </data>
    <LinearLayout
        ...
        <LinearLayout
            ...
        </LinearLayout>

        <LinearLayout
            ...
        </LinearLayout>

    </LinearLayout>
</layout>

0 个答案:

没有答案