亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何以編程方式將 CardView 添加到 RecyclerView 中的 LinearLayout

如何以編程方式將 CardView 添加到 RecyclerView 中的 LinearLayout

慕村9548890 2021-11-17 15:16:39
是否可以以編程方式將 a 添加CardView到 aLinearLayout內部 a RecyclerView. 目前,所有的CardViews 都被添加到 中RecyclerView,但我希望將屏幕截圖中的那些添加到 中LinearLayout。片段類public class TabFragmentRV extends android.support.v4.app.Fragment {    RecyclerView mRecyclerView;    public TabFragmentRV() {}    @Override    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        return inflater.inflate(R.layout.fragment_rv, container, false);    }    @Override    public void onActivityCreated(@Nullable Bundle savedInstanceState) {        View v = getView();        assert v != null;        mRecyclerView = v.findViewById(R.id.my_recyclerview);        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));        super.onActivityCreated(savedInstanceState);        initRVAdapter();    }    private void initRVAdapter(){        List<Object> itemsList = new ArrayList<>();        RVItemsAapter itemsListAdapter = new RVItemsAapter(getContext());        mRecyclerView.setAdapter(itemsListAdapter);        itemsList.add(new RVLineSeparator());        itemsList.add(new SectionHeader("Section E"));        itemsList.add(new SMSmessage("Item E1","Item E1 description"));        itemsList.add(new SMSmessage("Item E2","Item E2 description"));        itemsList.add(new SMSmessage("Item E3","Item E3 description"));        itemsList.add(new SMSmessage("Item E4","Item E4 description"));        itemsList.add(new RVLineSeparator());        itemsListAdapter.setCallSMSFeed(itemsList);        itemsListAdapter.notifyDataSetChanged();    }}
查看完整描述

2 回答

?
PIPIONE

TA貢獻1829條經驗 獲得超9個贊

首先使用一個線性布局的父布局,然后在其中創建包含任何布局(如線性或相對)的子布局,然后您可以創建第二個模塊的第二個子布局,在子布局中,您將使用卡片視圖和其他組件(如 textview 或編輯文本)在卡片視圖里面

否則約束布局最好開發這個布局


例如


<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/ll_sectionwithexpandability_main"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_weight="2"

android:orientation="vertical">

<LinearLayout

    android:layout_width="match_parent"

    android:layout_weight="1"

    android:layout_height="wrap_content">

<android.support.v7.widget.CardView

    android:layout_width="match_parent"

    android:layout_height="match_parent">

<LinearLayout

    xmlns:android="http://schemas.android.com/apk/res/android"

    android:id="@+id/ll_sectionheader"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:orientation="horizontal"

    android:padding="10dp"

    android:weightSum="100">


    <TextView

        android:id="@+id/tv_sectionheader_expandcollapsearrow"

        android:clickable="true"

        android:focusable="true"

        android:layout_weight="10"

        android:text="Testing"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_marginEnd="10dp"

        android:textColor="?android:attr/textColorPrimary"

        style="@android:style/TextAppearance.Large" />


    <TextView

        android:id="@+id/tv_sectionheader_title"

        android:layout_weight="90"

        android:layout_width="0dp"

        android:text="testing"

        android:layout_height="wrap_content"

        style="@android:style/TextAppearance.Large" />

   </LinearLayout>

   </android.support.v7.widget.CardView>

  </LinearLayout>

  <!-- I WANT ALL THE CARD VIEWS TO BE ADDED INSIDE THIS LINEARLAYOUT (ll_section_cards) -->

  <LinearLayout

   android:layout_width="match_parent"

    android:layout_weight="1"

   android:layout_height="wrap_content">

   <android.support.v7.widget.CardView

       android:layout_width="match_parent"

       android:layout_height="match_parent">

   <LinearLayout

    android:id="@+id/ll_section_cards"

    android:orientation="vertical"

    android:layout_width="match_parent"

    android:layout_height="wrap_content" >

  <TextView

           android:id="@+id/te"

           android:layout_weight="90"

           android:layout_width="wrap_content"

           android:text="testing"

           android:layout_height="wrap_content"

           style="@android:style/TextAppearance.Large" />

  </LinearLayout>

   </android.support.v7.widget.CardView>

  </LinearLayout>

</LinearLayout>



查看完整回答
反對 回復 2021-11-17
?
達令說

TA貢獻1821條經驗 獲得超6個贊


您的整個架構/模式似乎不合適。


您可以調整您的代碼,以便在“OnBind”方法中,您檢查下一個 n+1 對象是否是 SMSMessage 并將該批次添加到線性布局中......并以某種方式擁有他們已經擁有的記錄被“綁定”以避免雙重綁定......但是你還需要檢測該視圖何時未綁定......這一切似乎都在與框架希望你做事的方式作斗爭。


我建議讓 API(或“本地服務”)將 API 數據(最好使用像 JacksonMapper 這樣的 ObjectMapper)轉換為一個看起來更像的類:


import java.util.List;


public class SMSMessageCollection {


    private List<SMSMessage> smsMessages;

    private String headerText;

    private boolean hasStartingRVLine;

    private boolean hasEndingRVLine;


    public SMSMessageCollection(String headerText, boolean hasStartingRVLine, boolean hasEndingRVLine) {

        this.headerText = headerText;

        this.hasStartingRVLine = hasStartingRVLine;

        this.hasEndingRVLine = hasEndingRVLine;

    }


    //CUSTOM "ADDER":

    public void addSMSMessagesToCollection(List<SMSMessage> smsMessagesToAdd){

        this.smsMessages.addAll(smsMessagesToAdd);

    }


    //GETTER AND SETTER HERE -> OR USE LOMBOK...


}

這個類包含一個List<SMSMessages>所以當對象被綁定在回收器適配器中時,您可以遍歷列表并將它們添加到線性布局中。


所以這就是我的 Activity 的樣子......獲取 API 數據并制作 SMSMessageCollection(s) 列表,然后這可以通過一個 ViewHolder 傳遞給適配器......


import android.os.Bundle;

import android.support.v7.app.AppCompatActivity;


import com.fasterxml.jackson.core.type.TypeReference;

import com.fasterxml.jackson.databind.ObjectMapper;


import org.json.JSONArray;


import java.io.IOException;

import java.util.List;


public class MainActivity extends AppCompatActivity {


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);


        //This would be fetched using say Volley...and the structure should MATCH the class we have made...

        //Otherwise...make a "service class" to convert the API response to the SMSMessageCollection

        JSONArray apiDataModels = new JSONArray();


        // Jackson ObjectMapper will turn the API data in to a List or POJOs...in like two lines.

        try {

            List<SMSMessageCollection> smsMessageCollections = new ObjectMapper()

                    .readValue(apiDataModels.toString(),new TypeReference<List<SMSMessageCollection>>(){});

        } catch (IOException e) {

            e.printStackTrace();

        }


    }

}

因此,在 Adpater 的 OnBindViewHolder 方法中,您可以獲取該部分的 List 并以編程方式將它們添加到被注入的 ViewHolder 的 LienarLayout 中。


查看完整回答
反對 回復 2021-11-17
  • 2 回答
  • 0 關注
  • 182 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號