简体   繁体   English

Android 工作室,试图用基础 xml 和 ArrayAdapter 膨胀 xml 文件

[英]Android studio, trying to inflate an xml file with a base xml and ArrayAdapter

I'm trying to fill an xml (of a chat log) with an xml file of how it looks, and a class where i can get the text of the message from.我正在尝试用 xml 文件填充 xml(聊天日志),以及 class 文件,我可以从中获取消息的文本。

Im using ArrayAdapter and inflation.我使用 ArrayAdapter 和通货膨胀。 I have 2 XML files, one of them is "messageother", which is for someone elses message, and "messageme" for messages the user himself wrote.我有 2 个 XML 文件,其中一个是“messageother”,用于其他人的消息,“messageme”用于用户自己编写的消息。

public class MsgAdapter extends ArrayAdapter<Msg> {

    private final Context context;
    private final ArrayList<Msg> msgList;


    private TextView tvText;

    public MsgAdapter(Context context, ArrayList<Msg> messages) {
        super(context, R.layout.activ, messages);
        this.context=context;
        this.msgList=messages;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.messageother, parent, false);

        return rowView;
    }
} 

Now, this would just duplicate the same xml file of "messageother" until its done.现在,这只会复制“messageother”的相同 xml 文件,直到完成。 Instead, id like to go one by one in the ListView, and check if the Msg.getMine() = true;取而代之的是,id喜欢在ListView中一一对应go,并检查Msg.getMine() = true; If so, id like to inflate that one with the xml "messageme" like so:如果是这样,我想用 xml "messageme" 像这样膨胀那个:

View rowView = inflater.inflate(R.layout.messageme, parent, false);

How can I do such a thing?我怎么能做这样的事情?

public static class MsgAdapter extends ArrayAdapter<Msg> {
    private static final class ViewHolders {
        View mMessageOthersView;
        View mMessageMeView;
        private ViewHolders(@NonNull final LayoutInflater layoutInflater, @NonNull View parent) {
            this.mMessageOthersView = layoutInflater.inflate(R.layout.messageother, parent, false);
            this.mMessageMeView = layoutInflater.inflate(R.layout.messageme, parent, false);
        }
    }

    private final ArrayList<Msg> msgList;
    private final LayoutInflater mLayoutInflater;

    public MsgAdapter(Context context, ArrayList<Msg> messages) {
        super(context, R.layout.activ, messages);
        this.msgList = messages;
        this.mLayoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        final ViewHolders cViewHolders;
        if (convertView == null) {
            cViewHolders = new ViewHolders(this.mLayoutInflater, parent);
            convertView.setTag(cViewHolders);
        } else {
            cViewHolders = (ViewHolders)convertView.getTag();
        }

        final View finalView;
        final Msg cMsg = this.getItem(position);
        if (cMsg.getMine()) finalView = cViewHolders.mMessageMeView;
        else finalView = cViewHolders.mMessageOthersView;

            //"finalView" is the final inflated Layout
        finalView.findViewById(....)

        return finalView;
    }
}

A further step could be Caching all needed View for each of Layouts avoiding to do a complete "findById()" for each Item......进一步的步骤可能是为每个布局缓存所有需要的视图,避免为每个项目执行完整的“findById()”......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android:将XML OnClick膨胀吗? - Android: Inflate XML OnClick? 如何使用自定义ArrayAdapter类来膨胀XML布局? - How to inflate the XML layout using Custom ArrayAdapter Class? NullPointerException ArrayAdapter适配器Android XML - NullPointerException ArrayAdapter adapter Android XML 在Android Studio上反序列化xml文件 - Deserialize xml file on android studio 如何在ConstraintLayout类中为android膨胀XML Layout? - How to android inflate XML Layout in ConstraintLayout class? Google Map将二进制XML文件中的异常膨胀 - Google Map inflate exception in binary XML file 无法从 xml 文件中膨胀 RelativeLayout - Unable to inflate RelativeLayout from xml file 无法膨胀android.view.InflateException:二进制XML文件第24行:膨胀类片段时出错 - Failed to inflate android.view.InflateException: Binary XML file line #24: Error inflating class fragment 可以在Android的ArrayAdapter getView()中膨胀两个布局吗? - Is it possible to inflate two layouts in ArrayAdapter getView() in Android? Android Studio - 当 TextView 位于自定义 xml 文件时尝试使用代码更改它的属性时,应用程序崩溃 - Android Studio - App crashing when trying to change a TextView properties with code when it's located at a custom xml file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM