Monday, 14 August 2017

Recyclerview Radiobutton Android Tutorial









build.gradle
-----------------
 compile 'com.android.support:recyclerview-v7:25.3.1'
    compile 'com.android.support:cardview-v7:25.3.1'
    compile 'com.jakewharton:butterknife:8.5.1'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'
activity_main.xml
------------------------
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="com.anilsoft.anil.MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview_demo"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>
layout_data.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:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    card_view:cardElevation="5dp"
    card_view:cardUseCompatPadding="true">

    <RadioButton
        android:id="@+id/rview_radiobutton"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:scaleType="fitXY" />


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="15dp">

        <TextView
            android:id="@+id/textView_rvname"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="74dp"
            android:layout_marginStart="74dp"
            android:text="TextView"
            android:textSize="17sp" />

        <TextView
            android:id="@+id/textView_rvloc"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="74dp"
            android:layout_marginStart="74dp"
            android:text="A"
            android:textSize="17sp" />
    </LinearLayout>

</android.support.v7.widget.CardView>

Utils.Java
----------------
package com.anilsoft.anil.utils;

import com.anilsoft.anil.model.Student;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Anil on 8/14/2017.
 */

public class Utils {

    public static List<Student> loadStudentData() {
        List<Student> llStudentList = new ArrayList<>();
        llStudentList.add(new Student("Anil", "Tadipatri,RayalaSeema"));
        llStudentList.add(new Student("Aswartha", "Jammalamadugu,RayalaSeema"));
        llStudentList.add(new Student("Jayachandra", "Chittor,RayalaSeema"));
        llStudentList.add(new Student("Mahendra", "Kadapa,RayalaSeema"));
        llStudentList.add(new Student("Kishore", "Rajampeta,RayalaSeema"));
        llStudentList.add(new Student("Rakesh", "Nandyala,RayalaSeema"));
        llStudentList.add(new Student("Suresh", "Kurnool,RayalaSeema"));
        llStudentList.add(new Student("Sudhakar", "Ananthapur,RayalaSeema"));
        llStudentList.add(new Student("Prasad", "Ananthapur,RayalaSeema"));
        return llStudentList;
    }
}
Student.Java
------------------
package com.anilsoft.anil.model;

/**
 * Created by Anil on 8/14/2017.
 */

public class Student {
    String name;
    String location;

    public Student(String name, String location) {
        this.name = name;
        this.location = location;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }
}

StudentAdapter.Java
------------------------
package com.anilsoft.anil.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.anilsoft.anil.R;
import com.anilsoft.anil.model.Student;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Anil on 8/14/2017.
 */

public class StudentAdapter extends RecyclerView.Adapter<StudentAdapter.StudentVH> {
    Context context;
    List<Student> list;
    public int mSelectedItem = -1;

    public StudentAdapter(Context context, List<Student> list) {
        this.context = context;
        this.list = list;
    }


    @Override
    public StudentVH onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_data, parent, false);

        return new StudentVH(view);
    }

    @Override
    public void onBindViewHolder(StudentVH holder, int position) {
        holder.rviewRadiobutton.setChecked(position == mSelectedItem);
        holder.loadData(position);


    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class StudentVH extends RecyclerView.ViewHolder {
        @BindView(R.id.textView_rvname)
        TextView textViewRvname;
        @BindView(R.id.textView_rvloc)
        TextView textViewRvloc;
        @BindView(R.id.rview_radiobutton)
        RadioButton rviewRadiobutton;


        public StudentVH(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);


        }

        void loadData(final int position) {
            textViewRvname.setText(list.get(position).getName());
            textViewRvloc.setText(list.get(position).getLocation());
            View.OnClickListener listener = new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mSelectedItem = getAdapterPosition();
                    notifyDataSetChanged();
                    Toast.makeText(context, "" + list.get(position).getName() + "\n" + list.get(position).getLocation(), Toast.LENGTH_SHORT).show();


                }
            };
            rviewRadiobutton.setOnClickListener(listener);
            itemView.setOnClickListener(listener);
        }
    }

}

MainActivity.Java
---------------------------
package com.anilsoft.anil;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.anilsoft.anil.adapter.StudentAdapter;
import com.anilsoft.anil.utils.Utils;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.recyclerview_demo)
    RecyclerView recyclerviewDemo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        loadStudentInformation();


    }

    private void loadStudentInformation() {
        StudentAdapter studentAdapter = new StudentAdapter(MainActivity.this, Utils.loadStudentData());
        recyclerviewDemo.setLayoutManager(new LinearLayoutManager(this));
        recyclerviewDemo.setAdapter(studentAdapter);
    }
}






2 comments:

  1. Thank you for your post. This is excellent information. It is amazing and wonderful to visit your site.
    SMM
    SEM
    SMO
    Android

    ReplyDelete
  2. Thank you for your post. This is excellent information. It is amazing and wonderful to visit your site.sss
    Ppc
    Cpc
    Youtube ads
    Webdesign

    ReplyDelete