2016-05-16 76 views
2

我在CardView内部有一个代表RecycleView项目的按钮。我已经设法处理ViewHolder类中的click事件,但我需要在MainActivity上调用函数,我如何使用下面的代码实现它?处理RecycleView项目中的图像按钮点击

我的代码如下

ShopItemRecyclerViewAdapter.java 
public class ShopItemRecyclerViewAdapter extends RecyclerView.Adapter<ShopItemRecyclerViewAdapter.ListItemViewHolder> { 

      static ArrayList<ShopListItemModel> list; 
      LayoutInflater inflater; 

      public ShopItemRecyclerViewAdapter(ArrayList<ShopListItemModel> list, Context context){ 
       inflater = LayoutInflater.from(context); 
       this.list = list; 
      } 



      public ListItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
       View view = inflater.inflate(R.layout.list_item, parent , false); 
       ListItemViewHolder vh = new ListItemViewHolder(view); 
       return vh; 

      } 

      public void onBindViewHolder(ListItemViewHolder holder, int position) { 
       ShopListItemModel current = list.get(position); 
       holder.name.setText(current.getName()); 
       holder.price.setText(String.valueOf(current.getPrice())); 

      } 

      public int getItemCount() { 
       return list.size(); 
      } 

      public static class ListItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { 
       CardView cv; 
       TextView name; 
       TextView price; 
       ImageButton btnDelete; 

       ListItemViewHolder(final View itemView) { 
        super(itemView); 

        cv = (CardView)itemView.findViewById(R.id.cvShopListItem); 
        name = (TextView)itemView.findViewById(R.id.name); 
        price = (TextView)itemView.findViewById(R.id.price); 
        btnDelete = (ImageButton)itemView.findViewById(R.id.btnDeleteItem); 

        itemView.setOnClickListener(this); 
        btnDelete.setOnClickListener(this); 
       } 

       @Override 
       public void onClick(View v) { 
        //here i can handle the click but i think i need to use it in the main activity 
       } 
      } 
     } 

MainActivity.java(跳过无关紧要代码)

public class ShopCartScreen extends AppCompatActivity implements AdapterView.OnItemSelectedListener { 

    RecyclerView cartItems; //recycler to hold the cart list 
    ArrayList<ShopListItemModel> list = new ArrayList<ShopListItemModel>(); 
    ShopItemRecyclerViewAdapter adapter; 
    GetShopingCartList getShopingCartList; ////instance of network operation class to retrieve shop cart items list from server data base 

    protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.shop_carts_list); 
      cartItems = (RecyclerView) findViewById(R.id.newListItem); 
      cartItems.setHasFixedSize(true); 
      LinearLayoutManager llm = new LinearLayoutManager(this); 
      llm.setOrientation(LinearLayoutManager.VERTICAL); 
      cartItems.setLayoutManager(llm) 
    } 

      public void bindData(int listNumber) { 
      getShopingCartList = new GetShopingCartList(this, list, adapter, cartItems, listNumber, totalPrice); 
      getShopingCartList.execute("link to query which returns json object"); 
      } 
    } 

戈tShopingCartList.java网络运行

public class GetShopingCartList extends AsyncTask<String, String, ArrayList<ShopListItemModel>> { 

      private ArrayList<ShopListItemModel> shopCartItemList; 
      Context context; 
      RecyclerView items; 
      ShopItemRecyclerViewAdapter adapter; 
      int listNumber; 

      public GetShopingCartList(Context context, ArrayList<ShopListItemModel> shopCartItemList, ShopItemRecyclerViewAdapter adapter, 
             RecyclerView items ,int listNumber) { 
       this.context = context; 
       this.shopCartItemList = shopCartItemList; 
       this.adapter = adapter; 
       this.items = items; 
       this.listNumber = listNumber;  
      } 
     protected ArrayList<ShopListItemModel> doInBackground(String... params) { 
       HttpURLConnection connection = null; 
       BufferedReader reader = null; 
       shopCartItemList = new ArrayList<ShopListItemModel>(); 
     try { 
        URL url = new URL(params[0]); 
        connection = (HttpURLConnection) url.openConnection(); 
        connection.connect(); 
        InputStream stream = connection.getInputStream(); 
        reader = new BufferedReader(new InputStreamReader(stream)); 
        StringBuffer buffer = new StringBuffer(); 

        String line = ""; 
        while ((line = reader.readLine()) != null) { 
         buffer.append(line); 

        } 
        String finalJson = buffer.toString(); 
        JSONObject parentObject = new JSONObject(finalJson); 
        JSONArray parentArray = parentObject.getJSONArray("result"); 
     for (int i = 0; i < parentArray.length(); i++) { 
         JSONObject finalObject = parentArray.getJSONObject(i);//get the cuttent json object which is representaion of shop cart model object 

         String name = finalObject.getString("name"); 
         String price = finalObject.getString("price"); 
         Double d = Double.parseDouble(price); 
         ShopListItemModel item = new ShopListItemModel(name, d); 
         shopCartItemList.add(item);//adds the shopcart to the list of shop carts model 

        } 
       } catch (Exception e) { 
        e.printStackTrace(); 

       } finally { 
        if (connection != null) { 
         connection.disconnect(); 
        } 
        try { 
         if (reader != null) { 
          reader.close(); 
         } 
        } catch (IOException e) { 
         e.printStackTrace(); 

        } 
       } 
       return shopCartItemList; 
      } 

      protected void onPostExecute(ArrayList<ShopListItemModel> s) { 
       super.onPostExecute(s); 
       adapter = new ShopItemRecyclerViewAdapter(shopCartItemList, context); 
       items.setAdapter(adapter); 
     } 
       public ArrayList<ShopListItemModel> getList() { 
       return shopCartItemList; 
      } 

     } 
+0

您可以使用本地广播接收器,这个..从适配器请在'的onClick()广播'和你**活动收到**。我可以举一个例子,如果你想实现..! –

+0

即时通讯对于Android开发来说很新颖,如果您对此有所了解,请尽量以解释为例。我也从来没有实施广播接收器 –

回答

1

内ShopCartScreen.java实现方法,那么你可以使用适配器内部context对象。

((ShopCartScreen)context).methodImplemented(ShopListItemModel model) 
    //add this code inside onClick event of the button 
+0

我已经这样实施。主要活动:公共无效deleteItem(int postion){ list = getShopingCartList.getList(); list.remove(postion); adapter = new ShopItemRecyclerViewAdapter(list,this); cartItems.setAdapter(adapter); }它的工作,但通过这种方式删除动画不起作用,你有一个解决方案? –

+0

onClick在viewHolder中的按钮:public void onClick(View v){ //这里我可以处理点击,但我认为我需要在主要活动 ((ShopCartScreen)上下文).deleteItem(getPosition() ); } getPosition也被弃用 –

-1

确定这是我的解决方案,如果有人需要它(ⅰ合并在主活性2种方法/ 1和1中的再循环器适配器):

在我的再循环器适配器添加此删除方法:

//delete the item from the recycler Immediately for user interaction 
public void delete(int position){ 
     list.remove(position); 
     notifyItemRemoved(position); 
    } 
我ViewHolder类

ListItemViewHolder(final View itemView) { 
      super(itemView); 

      cv = (CardView)itemView.findViewById(R.id.cvShopListItem); 
      name = (TextView)itemView.findViewById(R.id.name); 
      price = (TextView)itemView.findViewById(R.id.price); 
      btnDelete = (ImageButton)itemView.findViewById(R.id.btnDeleteItem); 

      itemView.setOnClickListener(this); 
      btnDelete.setOnClickListener(this); 
     } 

     @Override 
     public void onClick(View v) { 
      ((ShopCartScreen)context).deleteItem(getPosition());//calls method in main activity 
      delete(getPosition()); 
     } 

,并在主要活动:

public void deleteItem(int postion){ 
      list = getShopingCartList.getList(); 
      ShopListItemModel tmp = list.get(postion); 
      tmp.getName(); //gets the item name to remove 
      shopCartModel.getNumber(); //gets the cart number for deleting the item for the correct cart 
      new DeleteCartItem(this , shopCartModel , tmp).execute(); //remove the item from the data base 
     }