Hi Guys, Maybe you all are expert in terms of using RecyclerView in android. This blog is simple example for using filter option with RecyclerView adapter. As for now you will instantiate RecyclerView and set the adapter to RecyclerView as following way. RecyclerView list = (RecyclerView) findViewById(R.id.list); list.setLayoutManager(new LinearLayoutManager(this)); list.setHasFixedSize(true); ArrayList<Number> numbers = new ArrayList<>(); String ONEs[] = {"ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE", "TEN"}; String TENs[] = {"ZERO", "TEN", "TWENTY", "THIRTY", "FOURTY", "FIFTY", "SIXTY", "SEVENTY", "EIGHTY", "NINETY", "HUNDRED"}; String HUNDREDS[] = {"ZERO", "HUNDRED", "TWO HUND
JobScheduler is the Android framework API for scheduling tasks or work. It first became available in Android 5.0 (API level 21), and remains under active development. Notably, Android 7.0 (API level 24) added the ability to trigger jobs based on ContentProvider changes.
If your app targets Android 5.0 (API level 21), we recommend that you use the JobScheduler to execute background tasks.
Here is the simple example of Scheduling and Cancelling the scheduled job. MainActivity.java
content_main.xml
MyJobService.java
Here is the full video tutorial
If you are really interested in this code, then please share this post with your friends.
Text WhatsApp message to +91-99654 70689 To join Android Developers WhatsApp group.
Join WhatsApp group by this link
Thank You
If your app targets Android 5.0 (API level 21), we recommend that you use the JobScheduler to execute background tasks.
Here is the simple example of Scheduling and Cancelling the scheduled job. MainActivity.java
public class MainActivity extends AppCompatActivity { int count; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); } public void addJob(View view) { count++; JobScheduler scheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE); ComponentName componentName = new ComponentName(this, MyJobService.class); JobInfo.Builder builder = new JobInfo.Builder(count, componentName); builder.setMinimumLatency(15000);//Start after 15 seconds PersistableBundle bundle = new PersistableBundle(); bundle.putString("Title", "Job " + count); builder.setExtras(bundle); if (scheduler != null) { int result = scheduler.schedule(builder.build()); if (result == JobScheduler.RESULT_SUCCESS) { showToast("Job scheduled successfully"); } else { showToast("Job scheduling failed"); } } } private void showToast(String s) { Toast.makeText(this, s, Toast.LENGTH_LONG).show(); } public void allJobs(View view) { startActivity(new Intent(this, AllJobsActivity.class)); } }
content_main.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" xmlns:tools="http://schemas.android.com/tools" tools:showIn="@layout/activity_main"> <Button android:id="@+id/buttonAddJob" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="addJob" android:text="Add Job" app:layout_constraintBottom_toTopOf="@id/buttonShowAllJob" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/buttonShowAllJob" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="allJobs" android:text="All Jobs" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/buttonAddJob" /> </android.support.constraint.ConstraintLayout>
MyJobService.java
public class MyJobService extends JobService { private static final String TAG = MyJobService.class.getSimpleName(); @Override public boolean onStartJob(JobParameters jobParameters) { PersistableBundle bundle = jobParameters.getExtras(); Log.v(TAG, bundle.getString("Title") + " started"); jobFinished(jobParameters, false); return true; } @Override public boolean onStopJob(JobParameters jobParameters) { Log.v(TAG, "Job completed"); return true; } }MyJobService should be registered in manifest as below.
<service android:name=".MyJobService" android:permission="android.permission.BIND_JOB_SERVICE" />MyJobService.java
public class AllJobsActivity extends AppCompatActivity { private List<JobInfo> pendingJobs = null; private JobsAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_all_jobs); Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); RecyclerView recyclerView = findViewById(R.id.myJobList); recyclerView.setHasFixedSize(true); recyclerView.setLayoutManager(new LinearLayoutManager(this)); final JobScheduler scheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE); if (scheduler != null) { pendingJobs = scheduler.getAllPendingJobs(); adapter = new JobsAdapter(pendingJobs); recyclerView.setAdapter(adapter); } ItemTouchHelper.SimpleCallback callback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) { @Override public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) { return false; } @Override public void onSwiped(final RecyclerView.ViewHolder viewHolder, int direction) { AlertDialog.Builder builder = new AlertDialog.Builder(AllJobsActivity.this); builder.setTitle("Cancel Job"); builder.setMessage("Are you sure to cancel this Job"); builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { JobInfo info = pendingJobs.get(viewHolder.getAdapterPosition()); scheduler.cancel(info.getId()); pendingJobs.remove(viewHolder.getAdapterPosition()); dialogInterface.dismiss(); adapter.notifyDataSetChanged(); } }); builder.setNegativeButton("No", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { dialogInterface.dismiss(); adapter.notifyDataSetChanged(); } }); builder.create().show(); } @Override public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) { View view = viewHolder.itemView; Paint p = new Paint(); p.setColor(Color.RED); RectF rectF; if (dX > 0) { rectF = new RectF(view.getLeft(), view.getTop(), dX, view.getBottom()); } else { rectF = new RectF(view.getRight() + dX, view.getTop(), view.getRight(), view.getBottom()); } c.drawRect(rectF, p); super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive); } }; ItemTouchHelper helper = new ItemTouchHelper(callback); helper.attachToRecyclerView(recyclerView); getSupportActionBar().setDisplayHomeAsUpEnabled(true); } }JobsAdapter.java
public class JobsAdapter extends RecyclerView.Adapter{ private List pendingJobs; public JobsAdapter(List pendingJobs) { this.pendingJobs = pendingJobs; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_list_item, parent, false); return new ViewHolder(view); } @Override public void onBindViewHolder(ViewHolder holder, int position) { holder.bind(position); } @Override public int getItemCount() { return pendingJobs.size(); } class ViewHolder extends RecyclerView.ViewHolder { private TextView textView; ViewHolder(View itemView) { super(itemView); textView = itemView.findViewById(R.id.text); } void bind(int position) { JobInfo info = pendingJobs.get(position); PersistableBundle bundle = info.getExtras(); textView.setText(bundle.get("Title").toString()); } } }
Here is the full video tutorial
Interesting right?
If you are really interested in this code, then please share this post with your friends.
Text WhatsApp message to +91-99654 70689 To join Android Developers WhatsApp group.
Join WhatsApp group by this link
Thank You
Comments
Post a Comment