在RecycleView Android中的SetOnClickListner中,监听器始终为空

时间:2016-06-24 20:41:28

标签: android android-recyclerview android-cardview recycler-adapter

在调试程序时,我总是在viewHolder.likeView.setOnClickListener()和viewHolder.mView.setOnClickListener()中的OnClick方法中将侦听器视为Null。代码有什么问题?

public class RVAdapter extends RecyclerView.Adapter<RVAdapter.ViewHolder> {

List<Bitmap> imageUrl;
private final RecyclerViewClickListener listener;

public RVAdapter(List<Bitmap> imageUrl, RecyclerViewClickListener listener) {
    this.imageUrl = imageUrl;
    this.listener= listener;
}

public static class ViewHolder extends RecyclerView.ViewHolder  {
    public final CardView cv;
    public final ImageView imageDp;
    public final ImageView likeView;
    public final View mView;

    public ViewHolder(View v) {
        super(v);
        mView=v;
        cv = (CardView)itemView.findViewById(R.id.card_view);
        imageDp = (ImageView)itemView.findViewById(R.id.picView);
        likeView = (ImageView)itemView.findViewById(R.id.likeView);

    }
}

@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.photo_display, viewGroup, false);
    ViewHolder pvh = new ViewHolder(v);
    return pvh;
}

@Override
public void onBindViewHolder(final ViewHolder viewHolder,final int i) {
    viewHolder.imageDp.setImageBitmap(imageUrl.get(i));

    viewHolder.mView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           if (null !=listener)
                listener.onRowClicked(i);
        }
    });

    viewHolder.likeView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (null != listener)
                listener.onViewClicked(v,i);
        }
    });

}

@Override
public int getItemCount() {
    return imageUrl.size();
}
 }

修改

public class DisplayPicActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener,RecyclerViewClickListener{

ImageView i;
Bitmap mIcon_val;
private RecyclerView rv;
List<Bitmap> imageBit = new ArrayList<>();
ArrayList<String> picList;
RecyclerViewClickListener mListener;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_pic);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);


    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    picList = getIntent().getStringArrayListExtra("test");

    rv=(RecyclerView)findViewById(R.id.rv);
    LinearLayoutManager llm = new LinearLayoutManager(this);
    rv.setLayoutManager(llm);
    rv.setHasFixedSize(true);
    initializeAdapter();

    retriveImage(picList);

}

private void retriveImage(ArrayList<String> picList) {
    for (int i=0;i<picList.size();i++){
        urlImage(picList.get(i));
    }
    final Handler myHandler = new Handler();
    myHandler.postDelayed(new Runnable() {
        @Override
        public void run() {
            initializeAdapter();
        }
    }, 3000);
}

private void initializeAdapter(){
    RVAdapter adapter = new RVAdapter(imageBit,mListener);
    rv.setAdapter(adapter);
}

public void urlImage(final String url) {

    Thread thread = new Thread() {
        @Override
        public void run() {
            try {
                URL newurl = new URL(url);
                mIcon_val = BitmapFactory.decodeStream(newurl.openConnection().getInputStream());
                imageBit.add(mIcon_val);

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    };

    thread.start();
}


@Override
public void onViewClicked(View v, int position) {
    if(v.getId() == R.id.likeView){

    }
}

@Override
public void onRowClicked(int position) {
}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.display_pic, 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);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_camera) {
        // Handle the camera action
    } else if (id == R.id.nav_gallery) {

    } else if (id == R.id.nav_slideshow) {

    } else if (id == R.id.nav_manage) {

    } else if (id == R.id.nav_share) {

    } else if (id == R.id.nav_send) {

    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}
}

1 个答案:

答案 0 :(得分:0)

可能您的if函数订单错误。试试这个:

if (listener != null)

而不是:

if (null !=listener)

修改

好的,现在我明白了,问题出在哪里。你只是不要RecyclerViewClickListener。只需在initialiseAdapter函数中初始化它。