android - 使用android数据绑定(bind)将数据从Recycler View onClick传递到Detail Activity

标签 android android-fragments data-binding android-recyclerview firebase-realtime-database

我有一个使用 android 数据绑定(bind)技术通过 recyclerview 填充的项目列表,现在我想详细传递和填充相同的数据 Activity 没有得到正确的做法。所以,请帮助做到这一点。

公共(public)类 fragment 扩展 fragment {

private FirebaseRecyclerAdapter adapter;
private Firebase mFirebaseRef = new Firebase("https://xyz.firebaseio.com/category/").child("list");

public Fragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    final View rootView = inflater.inflate(R.layout.recycler_view, container, false);
    final RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);

    adapter = new FirebaseRecyclerAdapter<List, ViewHolder>List.class, R.layout.fragment,
            ViewHolder.class, mFirebaseRef) {
        @Override
        protected void populateViewHolder(ViewHolder viewHolder, List list, int i) {

            FragmentBinding binding = viewHolder.getBinding();
            binding.setList(list);
        }
    };
    recyclerView.setAdapter(adapter);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), null));
    return rootView;
}

public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

    public FragmentBinding binding;

    public ViewHolder(View itemView) {
        super(itemView);
        binding = DataBindingUtil.bind(itemView);
        itemView.setOnClickListener(this);
    }
    public FragmentBinding getBinding() {
        return binding;
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(v.getContext(), DetailedActivity.class);

        **// need help here**

        v.getContext().startActivity(intent);
    }
}


@BindingAdapter("quantity")
public static void setQuantityText(TextView view, int quantity) {
    view.setText(String.valueOf(quantity));
}
public static class Handlers {

    public  static void increment(View view, int max) {
        FragmentBinding binding = DataBindingUtil.findBinding(view);
        binding.setQuantity(Math.max(max, binding.getQuantity() + 1));
    }
    public static void decrement(View view, int min) {
        FragmentBinding binding = DataBindingUtil.findBinding(view);
        binding.setQuantity(Math.min(min, binding.getQuantity() - 1));
    }
}

<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">

<data>
    <variable
        name="List"
        type="com.xyz.www.android.model.List"/>

    <variable
        name="quantity"
        type="int"/>

    <variable
        name="Handlers"
        type="com.xyz.www.android.ui.fragments.Fragment.Handlers"/>

</data>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingStart="@dimen/activity_horizontal_margin"
    android:paddingEnd="@dimen/activity_horizontal_margin"
    android:paddingTop="8dp">

        <ImageView
            android:layout_width="76dp"
            android:layout_height="76dp"
            android:contentDescription="@string/content_description"
            app:imageUrl="@{List.productImageUrl}"
            android:padding="8dp"
            android:layout_gravity="center_horizontal|bottom"
            android:layout_marginTop="8dp" />


    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="8dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_marginStart="72dp">


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{List.productTitle}"
            android:textSize="16sp"
            android:textColor="@android:color/primary_text_light"
            app:font="@{`Roboto-Regular.ttf`}"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{List.productSku}"
            android:textSize="13sp"
            android:paddingTop="8dp"
            app:font="@{`Roboto-Regular.ttf`}"/>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/Rs"
                android:textSize="14sp"
                android:paddingTop="8dp"
                android:paddingEnd="2dp"
                android:paddingStart="2dp"
                app:font="@{`Roboto-Regular.ttf`}"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@{List.productSellingPrice}"
                android:textSize="14sp"
                android:paddingTop="8dp"
                android:paddingStart="2dp"
                android:paddingEnd="2dp"
                android:textColor="@android:color/primary_text_light"
                app:font="@{`Roboto-Regular.ttf`}"/>

        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_alignParentEnd="true"
        android:paddingTop="16dp"
        android:paddingBottom="16dp">


        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/decrease" />

        <TextView
            android:layout_width="32dp"
            android:layout_height="wrap_content"
            app:font="@{`Roboto-Regular.ttf`}"
            app:quantity="@{quantity}"
            android:textAlignment="center" />

        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/increase" />

    </LinearLayout>
    </RelativeLayout>

@JsonIgnoreProperties(ignoreUnknown=true)

公共(public)类列表扩展 BaseObservable {

String productTitle;
String productSku;
String productImageUrl;
String productDescription;
String productMrp;
String productSellingPrice;


public List() {
}

public List(String productTitle, String productSku,
                 String productImageUrl, String productDescription,
                 String productMrp, String productSellingPrice) {

    this.productTitle = productTitle;
    this.productSku = productSku;
    this.productImageUrl = productImageUrl;
    this.productDescription = productDescription;
    this.productMrp = productMrp;
    this.productSellingPrice = productSellingPrice;

}

@Bindable
public String getProductTitle() {
    return productTitle;
}

@Bindable
public String getProductSku() {
    return productSku;
}

@Bindable
public String getProductImageUrl() {
    return productImageUrl;
}

@Bindable
public String getProductDescription() {
    return productDescription;
}

@Bindable
public String getProductMrp() {
    return productMrp;
}

@Bindable
public String getProductSellingPrice() {
    return productSellingPrice;
}

最佳答案

有几种方法可以做到这一点。一种是将 List Parcelable 设为 Parcelable,以便您可以将其作为 Intent extra 传递。然后您将能够提取它并填充详细信息页面。

public static final LIST = "ListContent";
@Override
public void onClick(View v) {
    Intent intent = new Intent(v.getContext(), DetailedActivity.class);

    FragmentBinding binding = DataBindingUtil.findBinding(v);
    intent.putExtra(LIST, binding.getList());

    v.getContext().startActivity(intent);
}

另一种是只传递List的ID,在detail binding中再次读取信息。将 ID 添加到列表中,然后您就可以在开始 Activity 时提取它:

public static final LIST_ID = "ListID";
@Override
public void onClick(View v) {
    Intent intent = new Intent(v.getContext(), DetailedActivity.class);

    FragmentBinding binding = DataBindingUtil.findBinding(v);
    intent.putExtra(LIST_ID, binding.getList().getId());

    v.getContext().startActivity(intent);
}

无论哪种方式,您都可以从详细信息 Activity 的 onCreate 中的 Intent 中提取数据:

public void onCreate(...) {
    Intent intent = getIntent();
    // Using ID:
    int id = intent.getIntExtra(LIST_ID);
    List list = loadListFromId(id);

    // Using Parcelable:
    List list = intent.getParcelableExtra(LIST);
    //...
}

关于android - 使用android数据绑定(bind)将数据从Recycler View onClick传递到Detail Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37273266/

相关文章:

android - 如何获取 ArrayList<HashMap> 的值作为字符串?

java - 读取fragment中的文件并将其放入listview中

Android getParentFragment() 直接从布局中膨胀 fragment 时为空

asp.net-mvc - 如何在 ASP.NET MVC Razor 中将数据绑定(bind)到布局模板

c# - 我可以在 GridView ItemTemplate 中使用 IF 语句吗?

java - 你能停止从JAVA中读取Json吗?

android - 以编程方式检查设备是否具有 NFC 读取器

java - 如何在Android中创建这种 View ?

.net - 将 Winforms 控件数据绑定(bind)到可为空类型的最佳方法?

java - 在android中保存数据对象持久内部列表