I am using RecyclerView
and mounting a list with multiple buttons
. I can change the color of button
according to a if
, if the condition is true it changes, but when I go up and down the list, it ends up changing other buttons
that can not change the color.
Adapter
public class RVAdapterTp1 extends RecyclerView.Adapter<RVAdapterTp1.MyViewHolder> {
private ArrayList<horario> horarioI;
private ArrayList<horario> horarioO;
private LayoutInflater mLayoutInflater;
private MaterialDialog mMaterialDialog;
private Context mContext;
public RVAdapterTp1(Context c, ArrayList<horario> i, ArrayList<horario> o) {
this.horarioI = i;
this.horarioO = o;
mContext = c;
//mLayoutInflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.tp1_item, parent, false);
MyViewHolder mvh = new MyViewHolder((v));
return mvh;
}
@Override
public void onBindViewHolder( final MyViewHolder holder, final int position) {
if (position < horarioO.size()) {
holder.cvO.setText(horarioO.get(position).getIo());
holder.cvO.setVisibility(View.VISIBLE);
if (horarioO.get(position).getOb() == 0) {
holder.cvO.setBackgroundColor(Color.parseColor("#FFC107"));
holder.cvO.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mMaterialDialog = new MaterialDialog(mContext);
mMaterialDialog.setTitle("ATENÇÃO");
mMaterialDialog.setMessage(horarioO.get(position).getObs());
mMaterialDialog.setPositiveButton("OK", new View.OnClickListener() {
@Override
public void onClick(View v) {
mMaterialDialog.dismiss();
}
});
mMaterialDialog.show();
}
});
}
} else {
holder.cvO.setText("");
holder.cvO.setVisibility(View.INVISIBLE);
}
if (position < horarioI.size()) {
holder.cvI.setText(horarioI.get(position).getIo());
holder.cvI.setVisibility(View.VISIBLE);
Log.v("AQUIIIaaaaaa", holder.cvI.getText().toString());
if (horarioI.get(position).getOb() == 0) {
horarioI.get(position).setOb(1);
Log.v("AQUIII", holder.cvI.getText().toString());
holder.cvI.setBackgroundColor(Color.parseColor("#FFC107"));
Log.v("AQUIIIeeeee", horarioI.get(position).getIo());
holder.cvI.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mMaterialDialog = new MaterialDialog(mContext);
mMaterialDialog.setTitle("ATENÇÃO");
mMaterialDialog.setMessage(horarioI.get(position).getObs());
mMaterialDialog.setPositiveButton("OK", new View.OnClickListener() {
@Override
public void onClick(View v) {
mMaterialDialog.dismiss();
}
});
Log.v("AQUIIIuuuu", holder.cvI.getText().toString());
mMaterialDialog.show();
}
});
}
} else {
holder.cvI.setText("");
holder.cvI.setVisibility(View.INVISIBLE);
}
}
@Override
public int getItemCount() {
if (horarioO.size() >= horarioI.size()) {
return horarioO.size();
} else {
return horarioI.size();
}
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public Button cvI;
public Button cvO;
public MyViewHolder(View itemView) {
super(itemView);
cvI = (Button) itemView.findViewById(R.id.cvI);
cvO = (Button) itemView.findViewById(R.id.cvO);
}
}
}
Fragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_tp1, container, false);
mRecyclerView = (RecyclerView) view.findViewById(R.id.rv_list);
mRecyclerView.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
llm.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(llm);
adapterTp1 = new RVAdapterTp1(getActivity(), horarioI, horarioO);
mRecyclerView.setAdapter(adapterTp1);
return view;
}
Item layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/llTp1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="@+id/cvI"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginBottom="2dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="4dp"
android:layout_marginTop="0dp"
android:layout_weight="1"
android:textColor="@color/primary_text"
android:textSize="30sp"
android:visibility="invisible"></Button>
<Button
android:id="@+id/cvO"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginBottom="2dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="4dp"
android:layout_marginTop="0dp"
android:layout_weight="1"
android:textColor="@color/primary_text"
android:textSize="30sp"
android:visibility="invisible"></Button>
</LinearLayout>
Layout RecyclerView
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Frags.Tp1Frag">
<android.support.v7.widget.RecyclerView
android:layout_gravity="center"
android:id="@+id/rv_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</FrameLayout>
ViewPage Layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:titleTextColor="@color/primary_text"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay" />
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabTextColor="#B6B6B6"
app:tabSelectedTextColor="#FFFFFF"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>