Answer the question
In order to leave comments, you need to log in
How to run a function which is in MainAcitvity inside onBindViewHolder in adapter?
Good day. There is an adapter, inside of it, of course, there is onBindViewHolder, there is a condition under which one function should be launched, which is generally located in MainActivity)
I would be grateful for the help, since I am new to Java and do not really understand how to implement this.
Here is the adapter:
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import com.squareup.picasso.Picasso;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import ru.vladimirkhudyakov.escentral.Item.ActsSectionItem;
import ru.vladimirkhudyakov.escentral.R;
public class ActsSectionAdapter extends RecyclerView.Adapter<ActsSectionAdapter.ExampleViewHolder> {
private final Context mContext;
private int selectedPosition = -1;
Boolean flagPosition = false;
private int lastPosition = -1;
private final ArrayList<ActsSectionItem> mExampleList;
SharedPreferences sharedPreferences;
public ActsSectionAdapter(Context context, ArrayList<ActsSectionItem> exampleList) {
mContext = context;
mExampleList = exampleList;
sharedPreferences = context.getSharedPreferences("vote-acts", Context.MODE_PRIVATE);
}
@Override
public @NotNull ExampleViewHolder onCreateViewHolder(@NotNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(mContext).inflate(R.layout.list_custom_3, parent, false);
return new ExampleViewHolder(v);
}
@Override
public void onBindViewHolder(@NotNull ExampleViewHolder holder, int position) {
@SuppressLint("CommitPrefEdits") SharedPreferences.Editor editor = sharedPreferences.edit();
if (position > lastPosition) {
holder.linearLayout.setVisibility(View.GONE);
new Handler().postDelayed(() -> {
holder.linearLayout.setVisibility(View.VISIBLE);
holder.linearLayout.setAnimation(AnimationUtils.loadAnimation(mContext, R.anim.fade_transition_anim));
}, 1000);
lastPosition = position;
}
ActsSectionItem currentItem = mExampleList.get(position);
String imageUrl = currentItem.getImageUrl();
String creatorName = currentItem.getCreator();
int placeCount = currentItem.getPlaceCount();
int likeCount = currentItem.getLikeCount();
holder.mTextViewCreator.setText(creatorName);
holder.mTextViewLikes.setText(String.valueOf(likeCount));
holder.mTextViewPlace.setText(String.valueOf(placeCount));
holder.webView.loadData(currentItem.getSrcVideo(), "text/html", "utf-8");
Picasso.get().load(imageUrl).fit().centerInside().into(holder.mImageView);
if(selectedPosition == position) {
if(flagPosition) {
flagPosition = false;
holder.mLinearLayoutAdditional.animate().alpha(0f);
holder.mLinearLayoutAdditional.setVisibility(View.GONE);
holder.itemView.setBackgroundColor(Color.parseColor("#0A0A2E"));
holder.mTextViewPlace.setTextColor(Color.parseColor("#ffffff"));
holder.mTextViewLikes.setTextColor(Color.parseColor("#ffffff"));
}
else {
flagPosition = true;
holder.mLinearLayoutAdditional.animate().alpha(1f);
holder.mLinearLayoutAdditional.setVisibility(View.VISIBLE);
holder.mTextViewLikes.setTextColor(Color.parseColor("#8383ea"));
holder.mTextViewPlace.setTextColor(Color.parseColor("#8383ea"));
holder.itemView.setBackgroundResource(R.drawable.view_substarte_country_list);
holder.mVoteArtist.setText(String.valueOf(10));
editor.putInt("id-part", currentItem.getId());
editor.apply();
}
}
else {
holder.mLinearLayoutAdditional.animate().alpha(0f);
holder.mLinearLayoutAdditional.setVisibility(View.GONE);
holder.itemView.setBackgroundColor(Color.parseColor("#0A0A2E"));
holder.mTextViewPlace.setTextColor(Color.parseColor("#ffffff"));
holder.mTextViewLikes.setTextColor(Color.parseColor("#ffffff"));
}
holder.itemView.setOnClickListener(v -> {
selectedPosition = position;
notifyDataSetChanged();
});
}
@Override
public int getItemCount() {
return mExampleList.size();
}
public static class ExampleViewHolder extends RecyclerView.ViewHolder {
public ImageView mImageView;
public TextView mTextViewCreator;
public TextView mTextViewLikes;
public TextView mTextViewPlace;
public TextView mVoteArtist;
public LinearLayout mLinearLayoutAdditional;
public WebView webView;
public LinearLayout linearLayout;
@SuppressLint("SetJavaScriptEnabled")
public ExampleViewHolder(View itemView) {
super(itemView);
linearLayout = itemView.findViewById(R.id.listLayoutActs);
webView = itemView.findViewById(R.id.videoView);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient());
mLinearLayoutAdditional = itemView.findViewById(R.id.popa);
mImageView = itemView.findViewById(R.id.imageViewLogoCountryList3);
mTextViewCreator = itemView.findViewById(R.id.textViewNameCountryList3);
mTextViewLikes = itemView.findViewById(R.id.textViewPointsList3);
mTextViewPlace = itemView.findViewById(R.id.textViewPlaceList3);
mVoteArtist = itemView.findViewById(R.id.voteArtist);
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question