1
I’m making an application and in one of the fragments I have a Recyclerview being used to list some view holders with some textviews and an image view.
I implemented a simple Adapter to display a Toast by clicking the Viewholder successfully, but now I wanted to change the code to display the image temporarily in full screen while the user keeps the viewholder pressed.
The images used are all inside the drawble folder, below code snippets:
Adapter:
public class MyFirstAdapter extends RecyclerView.Adapter {
private ArrayList<Republica> reps;
private MyOnItemClickListener myOnItemClickListener;
public void setMyOnItemClickListener(MyOnItemClickListener myOnItemClickListener){
this.myOnItemClickListener = myOnItemClickListener;
}
public interface MyOnItemClickListener {
void myOnItemClick(String nome);
}
public MyFirstAdapter(ArrayList<Republica> reps) {
this.reps = reps;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
//return null;
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.adapter_republicas, viewGroup, false);
v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (myOnItemClickListener != null) {
TextView txt = view.findViewById(R.id.txtRepNome);
myOnItemClickListener.myOnItemClick(txt.getText().toString());
}
}
});
return new MyFirstViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
((MyFirstViewHolder)viewHolder).bind(reps.get(i));
}
@Override
public int getItemCount() {
return this.reps.size();
}
public class MyFirstViewHolder extends RecyclerView.ViewHolder{
private ImageView imageView;
private TextView txtNome;
private TextView txtBio;
private RatingBar level;
private Button btnVagas;
public MyFirstViewHolder(View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.imgView);
txtNome = itemView.findViewById(R.id.txtRepNome);
txtBio = itemView.findViewById(R.id.txtRepBio);
level = itemView.findViewById(R.id.ratebarLevel);
btnVagas = itemView.findViewById(R.id.buttonVagas);
}
public void bind(final Republica rep){
imageView.setImageResource(rep.getFoto());
txtNome.setText(rep.getNome());
txtBio.setText(rep.getBio());
level.setRating(rep.getLevel_agitação());
int vagas = rep.getVagas_permanetes();
if (vagas == 0){
btnVagas.setText("Sem vagas");
btnVagas.setEnabled(false);
}
else {
btnVagas.setText(vagas + " vagas");
}
}
}
}
Fragment using the Adapter:
public class RepublicasFragment extends Fragment {
private RecyclerView mRecyclerView;
private MyFirstAdapter mAdapter;
private ImageView imgView;
public RepublicasFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_republicas, container, false);
mRecyclerView = (RecyclerView) v.findViewById(R.id.rViewRepublicas);
LinearLayoutManager manager = new LinearLayoutManager(getContext());
mRecyclerView.setLayoutManager(manager);
mRecyclerView.setHasFixedSize(true);
mAdapter = new MyFirstAdapter(new ArrayList<>(Arrays.asList(Republica.republicas)));
mRecyclerView.setAdapter(mAdapter);
/*
Adicionando o Listener
*/
mAdapter.setMyOnItemClickListener(new MyFirstAdapter.MyOnItemClickListener() {
@Override
public void myOnItemClick(String nome) {
Toast.makeText(getContext(),nome,Toast.LENGTH_SHORT).show();
}
});
return v;
}
}
Good ! Take a look at onTouchEvent. And use the ACTION_DOWN and UP properties to address your needs.
– Sergio Clemente