简体   繁体   English

如何从Android Java中的呼叫响应组合两个适配器?

[英]How to combine two Adapter from call response in Android Java?

Is there a way to combine two adapters that are inside a Callback from Model?有没有办法组合模型回调中的两个适配器? Like, I get data from two sources and I want to display both of them in the same recyclerView就像,我从两个来源获取数据,我想在同一个 recyclerView 中显示它们

I tried to load the method first then put both adapters on concatAdapter but I got a null from this concatAdapter and then my app crashed我尝试先加载该方法,然后将两个适配器都放在 concatAdapter 上,但是我从这个 concatAdapter 中得到了一个空值,然后我的应用程序崩溃了

here is my method in Java这是我在 Java 中的方法

private void fetchDataKabupaten(){
        progressBar.setVisibility(View.VISIBLE);
        token = getIntent().getStringExtra("token");
        Call<KabupatenModel> kabupatenModelCall = RetrofitClient.getLoginInterface().getKabupatenData("Bearer "+token);
        kabupatenModelCall.enqueue(new Callback<KabupatenModel>() {
            @Override
            public void onResponse(Call<KabupatenModel> call, Response<KabupatenModel> response) {
                kabupatenList = response.body().getKabupaten();
                adKabupaten = new KabupatenAdapter(MainActivity.this, kabupatenList);
                progressBar.setVisibility(View.GONE);
            }
}


private void fetchDataKecamatan(){
        progressBar.setVisibility(View.VISIBLE);
        token = getIntent().getStringExtra("token");
        Call<KecamatanModel> kecamatanModelCall = RetrofitClient.getLoginInterface().getKecamatanData("Bearer " + token);
        kecamatanModelCall.enqueue(new Callback<KecamatanModel>() {
            @Override
            public void onResponse(Call<KecamatanModel> call, Response<KecamatanModel> response) {
                kecamatanList = response.body().getKecamatan();
                adKecamatan = new KecamatanAdapter(MainActivity.this, kecamatanList);
                progressBar.setVisibility(View.GONE);
            }
}

I have tried to call both in MainActivity then setAdapter with concatAdapter like this:我尝试在 MainActivity 中调用两者,然后使用 concatAdapter 调用 setAdapter,如下所示:

fetchDataKabupaten();
fetchDataKecamatan();
concatAdapter = new ConcatAdapter(adKecamatan, adKabupaten);
rV.setAdapter(concatAdapter);

why is it null?为什么它是空的? but, when I only set one Adapter inside the response method it works.但是,当我只在响应方法中设置一个适配器时,它就起作用了。

you can only use one adapter in recyclerview, but adapter can have multiple view.在 recyclerview 中只能使用一个适配器,但适配器可以有多个视图。

for example I have adapter for chat, with right and left view例如我有用于聊天的适配器,具有左右视图

Adapter Chat Detail适配器聊天详情

class AdapterChatDetail(val datas: ArrayList<ChatMessage>) :
RecyclerView.Adapter<RecyclerView.ViewHolder>() {
private lateinit var ctx: Context

companion object {
    const val LEFT = 0
    const val RIGHT = 1
}

inner class ViewHolderLeft(val binding: AdapterMyChatLeftBinding) :
    RecyclerView.ViewHolder(binding.root) {
    fun bind(data: ChatMessage) {
        
    }
}

inner class ViewHolderRight(val binding: AdapterMyChatRightBinding) :
    RecyclerView.ViewHolder(binding.root) {
    fun bind(data: ChatMessage) {
        
    }
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
    ctx = parent.context
    return when (viewType) {
        LEFT -> ViewHolderLeft(
            AdapterMyChatLeftBinding.inflate(
                LayoutInflater.from(ctx),
                parent,
                false
            )
        )
        RIGHT -> ViewHolderRight(
            AdapterMyChatRightBinding.inflate(
                LayoutInflater.from(ctx),
                parent,
                false
            )
        )
        else -> ViewHolderLeft(
            AdapterMyChatLeftBinding.inflate(
                LayoutInflater.from(ctx),
                parent,
                false
            )
        )
    }
}

override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
    when (getItemViewType(position)) {
        LEFT -> (holder as ViewHolderLeft).bind(datas[position])
        RIGHT -> (holder as ViewHolderRight).bind(datas[position])
    }
}

override fun getItemCount() = datas.size

override fun getItemViewType(position: Int): Int {
    val data = datas[position]
    return if (data.position == "right") RIGHT else LEFT
}
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM