私はプログラムをデバッグしている間、viewHolder.likeView.setOnClickListener()およびviewHolder.mView.setOnClickListener()内のOnClickメソッドで常にリスナーをNullとして取得しています。コードの何が問題ですか?RecycleView AndroidのSetOnClickListner内に常にリスナーを取得する
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();
}
}
EDIT
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;
}
}
ここではわかりませんが、ここでコードの6行目はリスナーではなくリスナーですか? – Geoff
それはタイプミスでした。とにかくそれはメソッド内のパラメータ名でした。 – Keyul
'RVAdapter'を作成するコードを投稿する –