2 回答

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>

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 中。
添加回答
舉報