2016-03-03 190 views
0

我已将图像按钮放置在CardView内部的图像视图顶部。我想在用户点击图像按钮时触发一些操作。然而,第一个问题是当我在MainActivity中使用findViewById获取图像按钮时,它返回null(可能是因为图像视图在item_row xml中,它显示了卡片视图中的行数据)。在卡片视图中放置在图像视图中的图像按钮不会触发Onclick事件

另一个问题是如何解决这种情况,其中activity_main.xml不同,其中包含具有item_row.xml图像视图顶部的图像按钮的回收站视图。

MainActivity.java

package com.innovation.myapp.jwelleryonrent.View; 

import android.graphics.drawable.Drawable; 
import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.support.v4.widget.SwipeRefreshLayout; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.LinearLayoutManager; 
import android.support.v7.widget.RecyclerView; 
import android.support.v7.widget.Toolbar; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.ViewGroup; 
import android.widget.Button; 
import android.widget.ImageButton; 
import android.widget.Toast; 

import com.innovation.myapp.jwelleryonrent.R; 
import com.innovation.myapp.jwelleryonrent.controller.jwelleryController; 
import com.innovation.myapp.jwelleryonrent.model.adapter.CustomItemClickListner; 
import com.innovation.myapp.jwelleryonrent.model.adapter.JwelleryAdapter; 
import com.innovation.myapp.jwelleryonrent.model.pojo.JwelleryCollection; 

import java.util.ArrayList; 
import java.util.List; 

public class MainActivity extends AppCompatActivity implements jwelleryController.JwelleryCallbackListener { 
    private Toolbar mToolbar; 
    private RecyclerView mRecyclerView; 
    private SwipeRefreshLayout mSwipeRefreshLayout; 
    private List<JwelleryCollection> mJwelleryList = new ArrayList<>(); 
    private JwelleryAdapter mJwelleryAdapter; 
    private jwelleryController mController; 
    private boolean isInFavourites = false; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     configToolbar(); 
     mController = new jwelleryController(MainActivity.this); 
     configViews(); 
     ImageButton imgButton = (ImageButton) findViewById(R.id.favIcon); 
     imgButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Snackbar.make(v, "Item Added", Snackbar.LENGTH_LONG) 
         .setAction("Action", null).show(); 
      } 
     }); 


     FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
         .setAction("Action", null).show(); 
      } 
     }); 




    } 


    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) 
    { 
     View rootView = inflater.inflate(R.layout.item_row, container, false); 
     ImageButton imgBtn = (ImageButton) findViewById(R.id.favIcon); 
     imgBtn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       addItemToBag(v); 
      } 
     }); 
     return rootView; 
    } 


    private void addItemToBag(View v) 
    { 
     isInFavourites = true; 
     ImageButton btnFaviourite = (ImageButton) findViewById(R.id.favIcon); 
     if(isInFavourites==true) { 

      btnFaviourite.setImageResource(R.drawable.ic_favorite_white_24dp); 
     } 
     else 
      btnFaviourite.setImageResource(R.drawable.ic_favorite_border); 
     Snackbar.make(v, "Item added to Favourites", Snackbar.LENGTH_LONG) 
       .setAction("Action", null).show(); 
    } 


    private void configToolbar() { 
     mToolbar = (Toolbar) this.findViewById(R.id.toolbar); 
     setSupportActionBar(mToolbar); 
    } 

    private void initializeAdapter() 
    { 

     mJwelleryAdapter = new JwelleryAdapter(mJwelleryList); 

     mJwelleryAdapter.setOnItemClickListner(new CustomItemClickListner() { 
      @Override 
      public void onItemClick(View v, int position) { 
       Toast.makeText(MainActivity.this, "Clicked Item: "+position,Toast.LENGTH_LONG).show(); 
      } 
     }); 

//  mJwelleryAdapter = new JwelleryAdapter(mJwelleryList, new CustomItemClickListner() { 
//   @Override 
//   public void onItemClick(View v, int position) { 
//    Toast.makeText(MainActivity.this, "Clicked Item: "+position,Toast.LENGTH_LONG).show(); 
//   } 
//  }); 
     mRecyclerView.setAdapter(mJwelleryAdapter); 

     mSwipeRefreshLayout.setColorSchemeColors(getResources().getColor(R.color.colorAccent), 
       getResources().getColor(R.color.colorPrimary), 
       getResources().getColor(R.color.colorPrimaryDark)); 

     mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { 
      @Override 
      public void onRefresh() { 
       mController.startFetching(); 
      } 
     }); 


    } 


    private void configViews() { 
     mRecyclerView = (RecyclerView) this.findViewById(R.id.list); 
     mSwipeRefreshLayout = (SwipeRefreshLayout) this.findViewById(R.id.swipe); 

     mRecyclerView.setHasFixedSize(true); 
     mRecyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this)); 
     mRecyclerView.setRecycledViewPool(new RecyclerView.RecycledViewPool()); 
     mController.startFetching(); 
     initializeAdapter(); 
//  mJwelleryAdapter= new JwelleryAdapter(mJwelleryList); 

    } 

     @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

     @Override 
     public void onFetchStart() { 

     } 

     @Override 
     public void onFetchProgress(JwelleryCollection jwellery) { 
      mJwelleryAdapter.addJwellery(jwellery); 
     } 

     @Override 
     public void onFetchProgress(List<JwelleryCollection> jwelleryList) { 

     } 

     @Override 
     public void onFetchComplete() { 
      mSwipeRefreshLayout.setRefreshing(false); 
     } 

     @Override 
     public void onFetchFailure() { 

     } 
} 

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:fitsSystemWindows="true" 
tools:context=".MainActivity"> 

<android.support.design.widget.AppBarLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:theme="@style/AppTheme.AppBarOverlay"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="?attr/colorPrimary" 
     app:popupTheme="@style/AppTheme.PopupOverlay" 
     android:elevation="8dp"/> 

</android.support.design.widget.AppBarLayout> 

<include layout="@layout/content_main" /> 

<android.support.design.widget.FloatingActionButton 
    android:id="@+id/fab" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="bottom|end" 
    android:layout_margin="@dimen/fab_margin" 
    android:src="@android:drawable/ic_dialog_email" /> 

</android.support.design.widget.CoordinatorLayout> 

item_row.xml其中含有关于图像查看顶部图像按钮(ID =图标)。我想触发行动的favicon

<?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="wrap_content" 
android:layout_margin="8dp" 
card_view:cardCornerRadius="1dp" 
> 

<RelativeLayout 

    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:padding="8dp" 
    android:layout_margin="5dp" 
    > 

    <ImageView 
     android:id="@+id/flowerImage" 
     android:layout_width="match_parent" 
     android:layout_height="200dp" 

     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_alignParentTop="true" 
     android:visibility="visible"/> 
    <ImageButton 
     android:id="@+id/favIcon" 
     android:layout_width="32dp" 
     android:layout_height="32dp" 
     android:scaleType="fitXY" 
     android:layout_margin="5dp" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:background="@drawable/ic_favorite_border" 
     /> 



    <LinearLayout 

     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@+id/flowerImage" 

     android:orientation="vertical" 
     > 

     <TextView 
      android:id="@+id/flowerName" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Medium Text" 
      android:textColor="@color/colorPrimary" 
      android:textAppearance="?android:attr/textAppearanceMedium" 

/> 

     <TextView 
      android:id="@+id/flowerCategory" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Small Text" 
      android:layout_alignBottom="@id/flowerName" 
      android:textAppearance="?android:attr/textAppearanceSmall" 
      android:visibility="visible"/> 

     <TextView 
      android:id="@+id/flowerPrice" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignBottom="@id/flowerCategory" 
      android:text="New Text" 
      android:visibility="visible"/> 

     <TextView 
      android:id="@+id/flowerInstruction" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:visibility="gone" 
      android:text="New Text" 
/> 

    </LinearLayout> 


</RelativeLayout> 

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

代码ViewHolder类,

public class Holder extends RecyclerView.ViewHolder { 

    public TextView mName, mCategory, mPrice, mInstructions; 
    public ImageView mImage; 


    public Holder(View itemView) { 
     super(itemView); 
     mImage = (ImageView) itemView.findViewById(R.id.flowerImage); 
     mName = (TextView) itemView.findViewById(R.id.flowerName); 
     mCategory = (TextView) itemView.findViewById(R.id.flowerCategory); 
     mPrice = (TextView) itemView.findViewById(R.id.flowerPrice); 
     mInstructions = (TextView) itemView.findViewById(R.id.flowerInstruction); 

     ImageButton imgButton = (ImageButton) itemView.findViewById(R.id.favIcon); 
     imgButton.setOnClickListener(new View.OnClickListener(){ 
      @Override 
      public void onClick(View v) { 
       addItemToBag(v); 
      } 
     }); 


    } 


    public Holder(View itemView,int ViewType,Context c) { 
    // Creating ViewHolder Constructor with View and viewType As a parameter 
     super(itemView); 

    } 
+0

你提的问题是非常不清楚,你为什么用引号?只需写纯文本。您发布的所有内容都是xml,但您声明您的'onClick'不起作用,因为'findViewById'为空。发布您的实际代码。你的'ImageButton'为null的原因很可能是因为你在你的'Activity'中调用它,但是你在适配器中膨胀视图。 – Pztar

+0

@Vimal你可以分享你设置onClickListener为imageButton的Java代码? –

+0

这里是MainActivity.java的代码onCreate ImageButton imgButton =(ImageButton)findViewById(R.id.favIcon); //这里我得到imgButton为空 imgButton.setOnClickListener(新View.OnClickListener(){ @覆盖 公共无效的onClick(视图v){ Snackbar.make(V “产品加入”,Snackbar.LENGTH_LONG) .setAction(“Action”,null).show(); } }); – Vimal

回答

0

尝试(rootView补充):

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) 
{ 
    View rootView = inflater.inflate(R.layout.item_row, container, false); 
    ImageButton imgBtn = (ImageButton) rootView.findViewById(R.id.favIcon); 
    imgBtn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      addItemToBag(v); 
     } 
    }); 
    return rootView; 
} 
+0

当onCreateView被颠倒时?因为现在当我点击图像按钮时它不会触发。 我做的另一种方法是将onCreateView的整个代码放在ViewHolder构造函数中,图像按钮点击被触发,但是当我滚动列表时,addItemToBag函数代码被执行,虽然我没有点击任何其他卡的图像按钮。 – Vimal

+0

您是否建议解决上述问题? – Vimal

+0

我看不到您的持有者代码,请寄给我。 这是您在持有人构造函数中执行此操作的权利。 – MahDi

相关问题