问题
I am using RecyclerView custum adapter where data is fetching from getter and setter method and there is a one issue with superHero.getStatus() i.e if there is "deleted" on that position then all the view visibility will be View.GONE otherwise all views will be show on that item. Sometime it's not working as below code condition. And another issue is when I click on button then it's effects on another views. Please check below code. And tell me what should I do for improve it. Should I used getTag() and setTag() method to solve this issue?
public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder> {
private static final String url1 = "************************.php";
private static final String KEY_MATRI_ID_TO="matriID_to";
private static final String KEY_MATRI_ID_BY="matriID_by";
SessionManager session;
public String matri_id_to, matri_id_by, str_gender;
String str_status,str_EI;
//Imageloader to load image
private ImageLoader imageLoader;
private Context context;
//List to store all superheroes
List<SuperHero> superHeroes;
//Constructor of this class
public CardAdapter(List<SuperHero> superHeroes, Context context){
super();
//Getting all superheroes
this.superHeroes = superHeroes;
this.context = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.superheroes_list_shortlist_me, parent, false);
// Session class instance
session = new SessionManager(context);
session.checkLogin();
// get user data from session
HashMap<String, String> user = session.getUserDetails();
matri_id_by = user.get(SessionManager.KEY_EMAIL);
str_gender = user.get(SessionManager.KEY_GENDER);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
//Getting the particular item from the list
final SuperHero superHero = superHeroes.get(position);
holder.textViewId.setText(superHero.getMglId());
str_status = superHero.getStatus();
if(str_status.equalsIgnoreCase("deleted"))
{
holder.tvCommunity.setText("This profile has been deleted");
holder.tvCommunity.setTextColor(Color.RED);
holder.Community.setVisibility(View.GONE);
holder.AgeHeight.setVisibility(View.GONE);
holder.Occupation.setVisibility(View.GONE);
holder.Income.setVisibility(View.GONE);
holder.shortlist_on.setVisibility(View.GONE);
holder.imageView.setVisibility(View.GONE);
holder.tvAgeHeight.setVisibility(View.GONE);
holder.tvIncome.setVisibility(View.GONE);
holder.tvShortlistOn.setVisibility(View.GONE);
holder.tvOccupation.setVisibility(View.GONE);
holder.btnEI.setVisibility(View.GONE);
holder.btnViewProfile.setVisibility(View.GONE);
}
else
{
holder.AgeHeight.setText(superHero.getAge()+" / "+superHero.getAgeHeight());
holder.Community.setText(superHero.getCommunity()+" / "+superHero.getCaste());
holder.Occupation.setText(superHero.getOccupation());
holder.Income.setText(superHero.getIncome());
holder.shortlist_on.setText(superHero.getShortlisted_on());
holder.btnEI.setText(superHero.getExpress_Intrest());
//Loading image from url
imageLoader = CustomVolleyRequest.getInstance(context).getImageLoader();
imageLoader.get(superHero.getImageUrl(), ImageLoader.getImageListener(holder.imageView, R.drawable.image, android.R.drawable.ic_dialog_alert));
holder.imageView.setImageUrl(superHero.getImageUrl(), imageLoader);
}
str_EI = superHero.getExpress_Intrest();
if(str_EI.toString().equalsIgnoreCase("Accepted")) {
holder.btnEI.setText(str_EI);
holder.btnEI.setBackgroundColor(Color.parseColor("#FF045B49"));
holder.btnEI.setEnabled(false);
}
else if(str_EI.toString().equalsIgnoreCase("Reject")){
holder.btnEI.setText(str_EI);
holder.btnEI.setBackgroundColor(Color.parseColor("#FF045B49"));
holder.btnEI.setEnabled(false);
}
else if(str_EI.toString().equalsIgnoreCase("Declined")){
holder.btnEI.setText(str_EI);
holder.btnEI.setBackgroundColor(Color.parseColor("#FF045B49"));
holder.btnEI.setEnabled(false);
}
else if(str_EI.toString().equalsIgnoreCase("Pending..")){
holder.btnEI.setText(str_EI);
holder.btnEI.setBackgroundColor(Color.parseColor("#FF045B49"));
holder.btnEI.setEnabled(false);
}
else
{
holder.btnEI.setText(str_EI);
}
holder.btnViewProfile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent n = new Intent(holder.itemView.getContext(),BlankActivity.class);
String str_id = holder.textViewId.getText().toString();
n.putExtra("ID",str_id);
holder.itemView.getContext().startActivity(n);
}
});
holder.btnEI.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
superHero.setExpress_Intrest("Wait...");
holder.btnEI.setText(superHero.getExpress_Intrest());
matri_id_to = superHero.getMglId();
holder.expressInterest(position);
}
});
}
@Override
public int getItemCount() {
return superHeroes.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
//Views
public NetworkImageView imageView;
public TextView textViewId;
public TextView AgeHeight,tvAgeHeight;
public TextView Community, tvCommunity;
public TextView Occupation, tvOccupation;
public TextView Income, tvIncome;
public TextView shortlist_on, tvShortlistOn;
public Button btnViewProfile;
public Button btnEI;
//Initializing Views
public ViewHolder(final View itemView) {
super(itemView);
imageView = (NetworkImageView) itemView.findViewById(R.id.imageViewHero);
textViewId = (TextView) itemView.findViewById(R.id.textViewId);
tvAgeHeight = (TextView) itemView.findViewById(R.id.tvAgeHeight);
AgeHeight = (TextView) itemView.findViewById(R.id.AgeHeight);
tvCommunity = (TextView) itemView.findViewById(R.id.tvCommunity);
Community = (TextView) itemView.findViewById(R.id.Community);
tvOccupation = (TextView) itemView.findViewById(R.id.tvOccupation);
Occupation = (TextView) itemView.findViewById(R.id.Occupation);
tvIncome = (TextView) itemView.findViewById(R.id.tvIncome);
Income = (TextView) itemView.findViewById(R.id.Income);
tvShortlistOn = (TextView) itemView.findViewById(R.id.tvShortlistOn);
shortlist_on = (TextView) itemView.findViewById(R.id.shortlistOn);
btnViewProfile = (Button) itemView.findViewById(R.id.viewProfile);
btnEI = (Button) itemView.findViewById(R.id.sendEI);
}
public void expressInterest(final int position) {
StringRequest stringRequest1 = new StringRequest(Request.Method.POST, url1, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if(response.trim().equalsIgnoreCase("success")) {
superHeroes.get(position).setExpress_Intrest("Pending..");
notifyDataSetChanged();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(context, error.toString(), Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put(KEY_MATRI_ID_BY,matri_id_by);
params.put(KEY_MATRI_ID_TO,matri_id_to);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(context);
requestQueue.add(stringRequest1);
}
}
}
回答1:
1) First issues visible view
you need to put visibility code for other view in else part because if once status change from deleted to other then your view will not visible so you need to put visible code.
ex. I have added four line in your else part. please check. and add as per you requirements
if(str_status.equalsIgnoreCase("deleted"))
{
holder.tvCommunity.setText("This profile has been deleted");
holder.tvCommunity.setTextColor(Color.RED);
holder.Community.setVisibility(View.GONE);
holder.AgeHeight.setVisibility(View.GONE);
holder.Occupation.setVisibility(View.GONE);
holder.Income.setVisibility(View.GONE);
holder.shortlist_on.setVisibility(View.GONE);
holder.imageView.setVisibility(View.GONE);
holder.tvAgeHeight.setVisibility(View.GONE);
holder.tvIncome.setVisibility(View.GONE);
holder.tvShortlistOn.setVisibility(View.GONE);
holder.tvOccupation.setVisibility(View.GONE);
holder.btnEI.setVisibility(View.GONE);
holder.btnViewProfile.setVisibility(View.GONE);
}
else
{
//Add this
holder.Community.setVisibility(View.VISIBLE);
holder.AgeHeight.setVisibility(View.VISIBLE);
holder.Occupation.setVisibility(View.VISIBLE);
holder.Income.setVisibility(View.VISIBLE);
holder.AgeHeight.setText(superHero.getAge()+" / "+superHero.getAgeHeight());
holder.Community.setText(superHero.getCommunity()+" / "+superHero.getCaste());
holder.Occupation.setText(superHero.getOccupation());
holder.Income.setText(superHero.getIncome());
holder.shortlist_on.setText(superHero.getShortlisted_on());
holder.btnEI.setText(superHero.getExpress_Intrest());
//Loading image from url
imageLoader = CustomVolleyRequest.getInstance(context).getImageLoader();
imageLoader.get(superHero.getImageUrl(), ImageLoader.getImageListener(holder.imageView, R.drawable.image, android.R.drawable.ic_dialog_alert));
holder.imageView.setImageUrl(superHero.getImageUrl(), imageLoader);
}
来源:https://stackoverflow.com/questions/47807252/how-to-use-gettag-and-settag-in-recycler-view