Search code examples
androidandroid-recyclerviewswitch-statementandroid-cardview

How to handle switch widget (or button) outside RecyclerView in activity


I have recyclerview with cardview to load data and there are two columns, the code work fine but I need to hide the first column with switch widget. I have trying some code but none work for me. Can someone help me. I'm new in android. Thanks

Here is the screenshot

Screenshot

Below is my code

MyFragment.java

public class MyFragment extends Fragment {

    public MyFragment() {

    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup viewGroup,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_recyclerview, viewGroup, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mobile = getResources().getStringArray(R.array.mobile);
        desktop = getResources().getStringArray(R.array.desktop);

        RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

        MyAdapter adapter = new MyAdapter(getContext());
        recyclerView.setAdapter(adapter);

        // this code below didn't work. i thought it's because different 
        // layout between fragment and adapter but i don't know to solve it
        final TextView tvMobile = (TextView) view.findViewById(R.id.tv_mobile);
        Switch mSwitch = (Switch) view.findViewById(R.id.switch_item);
        mSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    tvMobile.setVisibility(View.GONE);
                } else {
                    tvMobile.setVisibility(View.VISIBLE);
                }
            }
        });
    }
}

MyAdapter.java

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

    private LayoutInflater inflater;

    public static String[] mobile;
    public static String[] desktop;

    public MyAdapter(Context context) {
        inflater = LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public MyAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_cardview, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyAdapter.MyViewHolder holder, int position) {
        holder.tvMobile.setText(mobile[position]);
        holder.tvDesktop.setText(desktop[position]);
    }

    @Override
    public int getItemCount() {
        return mobile.length;
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView tvMobile;
        private TextView tvDesktop;

        MyViewHolder(View itemView) {
            super(itemView);
            tvMobile = (TextView) itemView.findViewById(R.id.tv_mobile);
            tvDesktop = (TextView) itemView.findViewById(R.id.tv_desktop);
        }
    }
}

fragment_recyclerview.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/background_light"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="end"
        android:background="#dfe7f2"
        android:gravity="end"
        android:orientation="horizontal"
        android:paddingBottom="5dp"
        android:paddingRight="16dp"
        android:paddingTop="5dp">

        <Switch
            android:id="@+id/switch_item"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="Hide Mobile Row"
            android:textColor="@color/text_color"
            android:textSize="18sp" />
    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/color_divider" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false"
        android:paddingBottom="16dp"
        android:paddingTop="16dp" />
</LinearLayout>

item_cardview.xml

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_gravity="center"
    android:layout_marginBottom="8dp"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    card_view:cardBackgroundColor="@color/background_dark">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:orientation="horizontal"
        android:weightSum="2">

        <TextView
            android:layout_width="5dp"
            android:layout_height="match_parent"
            android:background="@drawable/bg_card_view" />

        <TextView
            android:id="@+id/tv_mobile"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:paddingLeft="10dp"
            android:text="romaji"
            android:textColor="@color/black_1"
            android:textSize="16sp" />

        <TextView
            android:id="@+id/tv_desktop"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:paddingLeft="10dp"
            android:text="arti"
            android:textColor="@color/black_1"
            android:textSize="16sp" />
    </LinearLayout>
</android.support.v7.widget.CardView>

Solution

  • In your adapter change like this

    public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
    
    public static boolean isMobileHided = false; //add this flag
    
    @Override
    public void onBindViewHolder(@NonNull MyAdapter.MyViewHolder holder, int position) {
        holder.tvMobile.setText(mobile[position]);
        holder.tvDesktop.setText(desktop[position]);
        if (isMobileHided) {
            holder.tvMobile.setVisblity(View.GONE);
        }
        else{
            holder.tvMobile.setVisblity(View.VISIBLE);
        }
    }
    }
    

    In your switch listener(From activity/fragments)

     mSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
               //update the switch button status to adapter flag variable
               adapter.isMobileHided = isChecked;
               //refresh the adapter
               adapter.notifyDataSetChanged();
        }
    });