如何以编程方式将 CardView 添加到 RecyclerView 中的 LinearLayout

是否可以以编程方式将 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();

    }

}



慕村9548890
浏览 165回答 2
2回答

PIPIONE

首先使用一个线性布局的父布局,然后在其中创建包含任何布局(如线性或相对)的子布局,然后您可以创建第二个模块的第二个子布局,在子布局中,您将使用卡片视图和其他组件(如 textview 或编辑文本)在卡片视图里面否则约束布局最好开发这个布局例如<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns: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&nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; android:layout_weight="1"&nbsp; &nbsp; android:layout_height="wrap_content"><android.support.v7.widget.CardView&nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; android:layout_height="match_parent"><LinearLayout&nbsp; &nbsp; xmlns:android="http://schemas.android.com/apk/res/android"&nbsp; &nbsp; android:id="@+id/ll_sectionheader"&nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; android:orientation="horizontal"&nbsp; &nbsp; android:padding="10dp"&nbsp; &nbsp; android:weightSum="100">&nbsp; &nbsp; <TextView&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/tv_sectionheader_expandcollapsearrow"&nbsp; &nbsp; &nbsp; &nbsp; android:clickable="true"&nbsp; &nbsp; &nbsp; &nbsp; android:focusable="true"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_weight="10"&nbsp; &nbsp; &nbsp; &nbsp; android:text="Testing"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginEnd="10dp"&nbsp; &nbsp; &nbsp; &nbsp; android:textColor="?android:attr/textColorPrimary"&nbsp; &nbsp; &nbsp; &nbsp; style="@android:style/TextAppearance.Large" />&nbsp; &nbsp; <TextView&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/tv_sectionheader_title"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_weight="90"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="0dp"&nbsp; &nbsp; &nbsp; &nbsp; android:text="testing"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; style="@android:style/TextAppearance.Large" />&nbsp; &nbsp;</LinearLayout>&nbsp; &nbsp;</android.support.v7.widget.CardView>&nbsp; </LinearLayout>&nbsp; <!-- I WANT ALL THE CARD VIEWS TO BE ADDED INSIDE THIS LINEARLAYOUT (ll_section_cards) -->&nbsp; <LinearLayout&nbsp; &nbsp;android:layout_width="match_parent"&nbsp; &nbsp; android:layout_weight="1"&nbsp; &nbsp;android:layout_height="wrap_content">&nbsp; &nbsp;<android.support.v7.widget.CardView&nbsp; &nbsp; &nbsp; &nbsp;android:layout_width="match_parent"&nbsp; &nbsp; &nbsp; &nbsp;android:layout_height="match_parent">&nbsp; &nbsp;<LinearLayout&nbsp; &nbsp; android:id="@+id/ll_section_cards"&nbsp; &nbsp; android:orientation="vertical"&nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; android:layout_height="wrap_content" >&nbsp; <TextView&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;android:id="@+id/te"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;android:layout_weight="90"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;android:layout_width="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;android:text="testing"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;style="@android:style/TextAppearance.Large" />&nbsp; </LinearLayout>&nbsp; &nbsp;</android.support.v7.widget.CardView>&nbsp; </LinearLayout></LinearLayout>

达令说

您的整个架构/模式似乎不合适。您可以调整您的代码,以便在“OnBind”方法中,您检查下一个 n+1 对象是否是 SMSMessage 并将该批次添加到线性布局中......并以某种方式拥有他们已经拥有的记录被“绑定”以避免双重绑定......但是你还需要检测该视图何时未绑定......这一切似乎都在与框架希望你做事的方式作斗争。我建议让 API(或“本地服务”)将 API 数据(最好使用像 JacksonMapper 这样的 ObjectMapper)转换为一个看起来更像的类:import java.util.List;public class SMSMessageCollection {&nbsp; &nbsp; private List<SMSMessage> smsMessages;&nbsp; &nbsp; private String headerText;&nbsp; &nbsp; private boolean hasStartingRVLine;&nbsp; &nbsp; private boolean hasEndingRVLine;&nbsp; &nbsp; public SMSMessageCollection(String headerText, boolean hasStartingRVLine, boolean hasEndingRVLine) {&nbsp; &nbsp; &nbsp; &nbsp; this.headerText = headerText;&nbsp; &nbsp; &nbsp; &nbsp; this.hasStartingRVLine = hasStartingRVLine;&nbsp; &nbsp; &nbsp; &nbsp; this.hasEndingRVLine = hasEndingRVLine;&nbsp; &nbsp; }&nbsp; &nbsp; //CUSTOM "ADDER":&nbsp; &nbsp; public void addSMSMessagesToCollection(List<SMSMessage> smsMessagesToAdd){&nbsp; &nbsp; &nbsp; &nbsp; this.smsMessages.addAll(smsMessagesToAdd);&nbsp; &nbsp; }&nbsp; &nbsp; //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 {&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; &nbsp; &nbsp; setContentView(R.layout.activity_main);&nbsp; &nbsp; &nbsp; &nbsp; //This would be fetched using say Volley...and the structure should MATCH the class we have made...&nbsp; &nbsp; &nbsp; &nbsp; //Otherwise...make a "service class" to convert the API response to the SMSMessageCollection&nbsp; &nbsp; &nbsp; &nbsp; JSONArray apiDataModels = new JSONArray();&nbsp; &nbsp; &nbsp; &nbsp; // Jackson ObjectMapper will turn the API data in to a List or POJOs...in like two lines.&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<SMSMessageCollection> smsMessageCollections = new ObjectMapper()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .readValue(apiDataModels.toString(),new TypeReference<List<SMSMessageCollection>>(){});&nbsp; &nbsp; &nbsp; &nbsp; } catch (IOException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}因此,在 Adpater 的 OnBindViewHolder 方法中,您可以获取该部分的 List 并以编程方式将它们添加到被注入的 ViewHolder 的 LienarLayout 中。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java