使用android数据绑定将数据从Recycler视图onClick传递到Detail Activity

时间:2016-05-17 10:15:23

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

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

public class 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)

公共类List扩展了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;
}

1 个答案:

答案 0 :(得分:1)

有几种方法可以做到这一点。一种是制作List 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并在详细信息绑定中再次读取信息。将ID添加到列表中,然后您就可以在启动活动时将其解压缩:

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);
}

无论哪种方式,您都可以在详细信息的onCreate中从意图中提取数据活动:

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);
    //...
}