在我的 android 应用程序中,我有以下类。我想在 UI 中绑定这些类,以根据对象数据显示 UI 内容。
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<import type="java.util.Map"/>
<import type="java.util.List"/>
<variable name="itemContainerInformation" type="com.myApp.Models.ItemsContainerInformation"/>
</data>
<RelativeLayout
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/cardview_light_background"
tools:context="com.myApp.Fragments.DetailedViewFragment"
android:id="@+id/layout_detailed_view">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{itemInformation.t1Caption}"
android:textAlignment="textStart"
android:gravity="start"
android:id="@+id/textView_day_t1_header"
android:layout_below="@+id/textView_date_header"
android:textStyle="normal|bold"
android:textSize="20sp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:textColor="@color/cardview_light_background"
android:background="@color/colorPrimary"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{itemContainerInformation.currentDateHeaderCaption}"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:textAlignment="center"
android:textColor="@color/colorAccent"
android:background="@color/colorPrimaryDark"
android:id="@+id/textView_date_header"
android:textStyle="normal|bold"
android:layout_marginTop="10dp"
android:textSize="24sp" />
<LinearLayout android:orientation="horizontal"
android:id="@+id/linear_layout_information"
android:layout_below="@+id/textView_day_t1_header"
android:background="@color/fall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<LinearLayout android:orientation="vertical"
android:id="@+id/linear_layout_day_information_list"
android:layout_weight="1" android:layout_height="fill_parent" android:layout_width="0dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{itemContainerInformation.itemInformations[0].Caption}"
android:textAlignment="textStart"
android:gravity="start"
android:id="@+id/textView_day_information1"
android:textStyle="normal|bold"
android:textSize="16sp"
android:layout_marginTop="30dp"
android:textColor="@color/colorPrimary" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{itemContainerInformation.itemInformations[1].Caption}"
android:textAlignment="textStart"
android:gravity="start"
android:id="@+id/textView_day_information2"
android:textStyle="normal|bold"
android:textSize="16sp"
android:textColor="@color/colorPrimary" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{itemContainerInformation.itemInformations[2].Caption}"
android:textAlignment="textStart"
android:gravity="start"
android:id="@+id/textView_day_information3"
android:textStyle="normal|bold"
android:textSize="16sp"
android:textColor="@color/colorPrimary" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{itemContainerInformation.itemInformations[3].Caption}"
android:textAlignment="textStart"
android:gravity="start"
android:id="@+id/textView_day_information4"
android:textStyle="normal|bold"
android:textSize="16sp"
android:textColor="@color/colorPrimary" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{itemContainerInformation.itemInformations[4].Caption}"
android:textAlignment="textStart"
android:gravity="start"
android:id="@+id/textView_day_information5"
android:textStyle="normal|bold"
android:textSize="16sp"
android:textColor="@color/colorPrimary" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=" "
android:textAlignment="textStart"
android:gravity="start"
android:id="@+id/textView_day_information6"
android:textStyle="normal|bold"
android:layout_marginBottom="20dp"
android:textSize="16sp"
android:textColor="@color/colorPrimary" />
</LinearLayout>
<LinearLayout android:orientation="vertical"
android:id="@+id/linear_layout_day"
android:layout_weight="1" android:layout_height="match_parent" android:layout_width="0dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_toRightOf="@+id/linear_layout_day_information_list"
android:text="@{itemContainerInformation.currentDate}"
android:textAlignment="viewEnd"
android:gravity="end"
android:id="@+id/textView_day"
android:textStyle="normal|bold"
android:textSize="100dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:textColor="@color/colorPrimary" />
</LinearLayout>
</LinearLayout>
<LinearLayout android:orientation="vertical"
android:id="@+id/linear_layout_information_bottom"
android:layout_below="@+id/linear_layout_information"
android:background="@color/fall"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{itemContainerInformation.itemInformations[5].Caption}"
android:textAlignment="viewEnd"
android:gravity="end"
android:id="@+id/textView_day_information_bottom1"
android:textStyle="normal|bold"
android:textSize="16sp"
android:layout_marginTop="0dp"
android:textColor="@color/colorPrimary" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{itemContainerInformation.itemInformations[6].Caption}"
android:textAlignment="viewEnd"
android:gravity="end"
android:id="@+id/textView_day_information_bottom2"
android:layout_below="@+id/textView_day_information_bottom1"
android:textStyle="normal|bold"
android:textSize="16sp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:textColor="@color/colorPrimary" />
</LinearLayout>
项目信息.java
public class ItemInformation extends BaseObservable {
private String caption;
private int lineIndex;
private int lineFontSize;
private int lineFontColor;
private int lineIndentation;
private int imageIndex;
public ItemInformation()
{
}
@Bindable
public String getCaption() { return caption; }
public void setCaption(String caption) {
this.caption = caption;
notifyPropertyChanged(BR.caption);
}
@Bindable
public int getLineIndex() { return lineIndex; }
public void setLineIndex(int lineIndex) {
this.lineIndex = lineIndex;
notifyPropertyChanged(BR.lineIndex);
}
@Bindable
public int getLineFontSize() { return lineFontSize; }
public void setLineFontSize(int lineFontSize) {
this.lineFontSize = lineFontSize;
notifyPropertyChanged(BR.lineFontSize);
}
@Bindable
public int getLineFontColor() { return lineFontColor; }
public void setLineFontColor(int lineFontColor) {
this.lineFontColor = lineFontColor;
notifyPropertyChanged(BR.lineFontColor);
}
@Bindable
public int getLineIndentation() { return lineIndentation; }
public void setLineIndentation(int lineIndentation) {
this.lineIndentation = lineIndentation;
notifyPropertyChanged(BR.lineIndentation);
}
@Bindable
public int getImageIndex() { return imageIndex; }
public void setImageIndex(int imageIndex) {
this.imageIndex = imageIndex;
notifyPropertyChanged(BR.imageIndex);}
public static int GetColorInformation(String fontcolor)
{
return Color.parseColor(fontcolor);
//int desiredColour = getResources().getColor(getResources().getIdentifier("my_color", "color"
}
}
项目容器信息.java
public class ItemContainerInformation extends BaseObservable {
private List<ItemInformation> itemInformations = new ArrayList<ItemInformation>();
private String currentDate;
private String currentDateHeaderCaption;
private String t1Caption;
private int rowIndex;
private int columnIndex;
private int currentMonth;
private int currentYear;
public ItemContainerInformation()
{
Calendar calendar = Calendar.getInstance();
int mYear = calendar.get(Calendar.YEAR);
int mMonth = calendar.get(Calendar.MONTH);
int mDay = calendar.get(Calendar.DAY_OF_MONTH);
}
public ItemContainerInformation(String currentDate) {
this.currentDate = currentDate;
}
@Bindable
public String getCurrentDate() {
return currentDate;
}
public void setCurrentDate(String currentDate) {
this.currentDate = currentDate;
notifyPropertyChanged(BR.currentDate);
}
@Bindable
public String getCurrentDateHeaderCaption() {
return currentDateHeaderCaption;
}
public void setCurrentDateHeaderCaption(String currentDateHeaderCaption) {
this.currentDateHeaderCaption = currentDateHeaderCaption;
notifyPropertyChanged(BR.currentDateHeaderCaption);
}
@Bindable
public String getT1Caption() {
return t1Caption;
}
public void setT1Caption(String t1Caption) {
this.t1Caption = t1Caption;
notifyPropertyChanged(BR.t1Caption);
}
@Bindable
public int getRowIndex() {
return rowIndex;
}
public void setRowIndex(int rowIndex) {
this.rowIndex = rowIndex;
notifyPropertyChanged(BR.rowIndex);
}
@Bindable
public int getColumnIndex() {
return columnIndex;
}
public void setColumnIndex(int columnIndex) {
this.columnIndex = columnIndex;
notifyPropertyChanged(BR.columnIndex);
}
@Bindable
public int getCurrentMonth() {
return currentMonth;
}
public void setCurrentMonth(int currentMonth) {
this.currentMonth = currentMonth;
notifyPropertyChanged(BR.currentMonth);
}
@Bindable
public int getCurrentYear() {
return currentYear;
}
public void setCurrentYear(int currentYear) {
this.currentYear = currentYear;
notifyPropertyChanged(BR.currentYear);
}
@Bindable
public List<ItemInformation> getItemInformations() {
return itemInformations;
}
public void setItemInformations(List<ItemInformation> itemInformations) {
this.itemInformations = itemInformations;
notifyPropertyChanged(BR.itemInformations);
}
}
详细视图片段
public class DetailedViewFragment extends Fragment {
ItemContainerInformation currentInormationItem;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_detailed_view, container, false);
if(currentInormationItem == null)
{
currentInormationItem = new ItemContainerInformation("100");
List<ItemInformation> dailyItems = new ArrayList<ItemInformation>();
for(int i = 0; i < 10; i++)
{
ItemInformation item = new ItemInformation();
item.setCaption("Caption_");
dailyItems.add(item);
}
currentDayInormationItem.setItemInformations(dailyItems);
}
FragmentDetailedDayViewBinding binding = DataBindingUtil.inflate(
inflater, R.layout.fragment_detailed_view, container, false);
View view = binding.getRoot();
//here data must be an instance of the class MarsDataProvider
binding.setDateInformation(currentDayInormationItem);
return v;
}
// This method is used to update the object from 'ParentItem'
// So when a UI click happens in the ParentFragment this method is called from ParentFragment
// which updates the objects. Now this updated object () will have new values which i want to update in the UI inside detailed view fragment.
public void UpdateDetailedDayView(DateInformation dayInormationItem)
{
// Should update all textviews in the child view but not working???
currentDayInormationItem = dayInormationItem;
}
}
父视图片段
public class ParentViewFragment extends Fragment {
// This is inside the parent fragment class which has multiple items populated using Adapter and ViewHolder
// On item click of the UI i want to update the 'DetailedViewFragment'.
MyRecyclerViewAdapter.OnItemClickListener onItemClickListener = new MyRecyclerViewAdapter.OnItemClickListener() {
@Override
public void onItemClick(ItemInformation itemInformation) {
Toast.makeText(getContext(), "Item Clicked --- " + itemInformation.getCurrentDate(), Toast.LENGTH_LONG).show();
// On click i am creating new fragment and updating it
DetailedViewFragment detailedView = new DetailedViewFragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.replace(R.id.month_detailed_view_container, detailedView).commit();
detailedView.UpdateDetailedDayView(itemInformation);
}
};
}
基本上我在 UI 中有不同的文本视图,并且相应的 UI 属性存储在对象中,如缩进、前景色、字体大小等。我如何将这些信息绑定到文本视图?
我无法使用此行查看文本,有人可以帮助我纠正下一行中的错误并使用特定标识和前景色显示文本。
android:text="@{itemContainerInformation.itemInformations[0].Caption}"
如何绑定对象并根据对象数据更新 UI?
谢谢。