Android:在与其他行交互时访问任何特定的recyclerview行

时间:2017-01-16 08:15:22

标签: android android-recyclerview recycler-adapter

我创建了一个包含海关行的Recyclerview。我想在用户更改任何其他行的内容时更新位置0的行内容。怎么能实现这一目标?

    public void onBindViewHolder(final FeedListRowHolder feedListRowHolder, int i) {
            final CartItemsPojo feedItem = feedItemList.get(i);
            posi = i;
            if ((feedItem.category_id.equalsIgnoreCase("-11"))) {
                feedListRowHolder.num_items.setText("ITEMS (" + feedItem.item_qty + ")");
                feedListRowHolder.total_p.setText("TOTAL: Rs."+feedItem.total_price+".00");
            }

            if (feedItem.category_id.toString().equalsIgnoreCase("1")) {
                feedListRowHolder.item_name.setText(feedItem.item_name);
            }

            if (feedItem.category_id.toString().equalsIgnoreCase("2")) {
                feedListRowHolder.item_name.setText(feedItem.item_name);
                 feedListRowHolder.dec_qty = setImage(xyz);
feedListRowHolder.dec_qty.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                       // How do I get the first row here and the update the content
                    }
                });
            }
        }

         class FeedListRowHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
            protected TextView num_items;
            protected TextView total_p;
            protected TextView cart_total;
            protected TextView del_charges;
            protected TextView final_p;        

            public FeedListRowHolder(View view, int rowType) {
                super(view);
                if (rowType == -11) {
                    num_items = (TextView) view.findViewById(R.id.num_items);
                    total_p = (TextView) view.findViewById(R.id.total_p);

                }
                if (rowType == 1) {
                    item_price_per_pound = (RobotoBoldTextView) view.findViewById(R.id.item_price_per_pound);
                    item_total_price = (RobotoBoldTextView) view.findViewById(R.id.item_total_price);
                    item_name = (RobotoLightTextView) view.findViewById(R.id.item_name);
                    shapeDrpDwn = (Spinner) view.findViewById(R.id.shapeDrpDwn);
                }
                if (rowType == 2) {
                    item_name = (RobotoLightTextView) view.findViewById(R.id.item_name);
                    dec_qty = (ImageView) view.findViewById(R.id.dec_qty);

                }
                view.setOnClickListener(this);
            }

单击侦听器正在按预期工作,即每当我点击第三行中使用的图像时触发onClick方法,同时我需要增加第一行中使用的价格计数。

怎么做?

3 个答案:

答案 0 :(得分:1)

要访问第一行,只需修改数据集中的第一项(在您的情况下为feedItemList)。

在你的onClick监听器中试试这个

CartItemsPojo item = feedItemList.get(0);
// Make changes to item
notifyDataSetChange ();

答案 1 :(得分:1)

最好的办法是将数值保留在数据集对象中。这样,您可以轻松地将更改投影到数据集,然后调用adapter.notifyItemChanged(0)来更新第一行。

// this is a sample pseudocode, so go easy on it
public voin onClick(){
  carItemPojo.setCount(carItemPojo.getCount()+1);
  getAdapter().notifyItemChanged(0);
}

答案 2 :(得分:0)

public class HelloActivity extends Activity {

    private RecyclerView mRecyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        mRecyclerView.setAdapter(new HelloAdapter());
        DefaultItemAnimator animator = new DefaultItemAnimator() {
            @Override
            public boolean canReuseUpdatedViewHolder(RecyclerView.ViewHolder viewHolder) {
                return true;
            }
        };
        mRecyclerView.setItemAnimator(animator);
    }

    private static class HelloAdapter extends RecyclerView.Adapter<HelloAdapter.HelloViewHolder> {

        public class HelloViewHolder extends RecyclerView.ViewHolder {

            public TextView textView;

            public HelloViewHolder(CardView cardView) {
                super(cardView);
                textView = (TextView) cardView.findViewById(R.id.text_view);
            }

        }

        @Override
        public HelloViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            CardView cardView = (CardView) LayoutInflater.from(parent.getContext()).inflate(
                    R.layout.card_item, parent, false);
            return new HelloViewHolder(cardView);
        }

        @Override
        public void onBindViewHolder(HelloViewHolder holder, int position) {
            bind(holder);
        }

        @Override
        public void onBindViewHolder(HelloViewHolder holder, int position, List<Object> payload) {
            Log.d("butt", "payload " + payload.toString());
            bind(holder);
        }

        @Override
        public int getItemCount() {
            return 20;
        }

        private void bind(final HelloViewHolder holder) {
            holder.textView.setText("item " + holder.getAdapterPosition());
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    final int position = holder.getAdapterPosition();
                    // (if postion==0)
                    Log.d("butt", "click " + position);
                    HelloAdapter.this.notifyItemChanged(position, "payload " + position);
                }
            });
        }

    }

}

activity_main.xml中

    <RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".HelloActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</RelativeLayout>

card_item.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="100dip"
    android:layout_margin="5dip"
    card_view:cardElevation="5dip">

    <TextView
        android:id="@+id/text_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</android.support.v7.widget.CardView>

以下是更多details