Pastikan terdapat data image di res>drawable dan video di res>raw
Class set/get objek
Class adapter
Class activity
Class set/get objek
package adehodijah.example.com.pertemuan4e; /** * Created by AdeHodijah on 3/15/2018. */ public class KamusImageEntity { private String dtIstilah; private String dtArti; private String dtKeterangan; private Integer photoView; private Integer videoView; private Integer laguMP3; private Integer videoMP4; public KamusImageEntity(String dtIstilah, String dtArti, String dtKeterangan, Integer photoView, Integer videoView, Integer laguMP3, Integer videoMP4) { this.dtIstilah = dtIstilah; this.dtArti = dtArti; this.dtKeterangan = dtKeterangan; this.photoView = photoView; this.videoView = videoView; this.laguMP3 = laguMP3; this.videoMP4 = videoMP4; } public String getDtIstilah() { return dtIstilah; } public void setDtIstilah(String dtIstilah) { this.dtIstilah = dtIstilah; } public String getDtArti() { return dtArti; } public void setDtArti(String dtArti) { this.dtArti = dtArti; } public String getDtKeterangan() { return dtKeterangan; } public void setDtKeterangan(String dtKeterangan) { this.dtKeterangan = dtKeterangan; } public Integer getPhotoView() { return photoView; } public void setPhotoView(Integer photoView) { this.photoView = photoView; } public Integer getVideoView() { return videoView; } public void setVideoView(Integer videoView) { this.videoView = videoView; } public Integer getLaguMP3() { return laguMP3; } public void setLaguMP3(Integer laguMP3) { this.laguMP3 = laguMP3; } public Integer getVideoMP4() { return videoMP4; } public void setVideoMP4(Integer videoMP4) { this.videoMP4 = videoMP4; } }
Class adapter
package adehodijah.example.com.pertemuan4e; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.support.v4.graphics.drawable.RoundedBitmapDrawable; import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import java.util.List; /** * Created by AdeHodijah on 3/15/2018. */ public class MyRecyclerViewClickNImageRefreshAdapter extends RecyclerView.Adapter<MyRecyclerViewClickNImageRefreshAdapter.ViewHolder> { private List<KamusImageEntity> mData; private ItemClickListener mClickListener; private Context ctx; public enum TypeClick { IMAGE_LAGU, IMAGE_VIDEO; } public void refreshDataAdapter(List<KamusImageEntity> newData){ mData.clear(); mData = newData; } // data is passed into the constructor MyRecyclerViewClickNImageRefreshAdapter(Context context, List<KamusImageEntity> data) { this.ctx = context; this.mData = data; } // inflates the row layout from xml when needed @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rowistilah, parent, false); return new ViewHolder(view); } // binds the data to the TextView in each row @Override public void onBindViewHolder(ViewHolder holder, int position) { KamusImageEntity kamus = mData.get(position); holder.myTextIstilah.setText(kamus.getDtIstilah()); holder.myTextArti.setText(kamus.getDtArti()); holder.myTextKeterangan.setText(kamus.getDtKeterangan()); Bitmap srcBitmapPhoto = BitmapFactory.decodeResource(ctx.getResources(), kamus.getPhotoView()); holder.photoView.setImageBitmap(srcBitmapPhoto); circularImage(holder.photoView, srcBitmapPhoto); Bitmap srcBitmapVideo = BitmapFactory.decodeResource(ctx.getResources(), kamus.getVideoView()); holder.videoView.setImageBitmap(srcBitmapVideo); circularImage(holder.videoView, srcBitmapVideo); } // total number of rows @Override public int getItemCount() { return mData.size(); } // stores and recycles views as they are scrolled off screen public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { TextView myTextIstilah; TextView myTextArti; TextView myTextKeterangan; public ImageView photoView, videoView; ViewHolder(View itemView) { super(itemView); myTextIstilah = (TextView) itemView.findViewById(R.id.istilah); myTextArti = (TextView) itemView.findViewById(R.id.arti); myTextKeterangan = (TextView) itemView.findViewById(R.id.keterangan); // itemView.setOnClickListener(this);//event click untuk view photoView = (ImageView) itemView.findViewById(R.id.imgAudio); videoView = (ImageView) itemView.findViewById(R.id.imgVideo); // myTextIstilah.setOnClickListener(this);// myTextKeterangan.setOnClickListener(this); photoView.setOnClickListener(this); videoView.setOnClickListener(this); } @Override public void onClick(View view) { // if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition()); //untuk click view if (mClickListener != null){ if(view == photoView){ mClickListener.onItemClick(view, getAdapterPosition(), TypeClick.IMAGE_LAGU); }else if(view.equals(videoView)){ mClickListener.onItemClick(view, getAdapterPosition(), TypeClick.IMAGE_VIDEO); } } } } // convenience method for getting data at click position KamusImageEntity getItem(int id) { return mData.get(id); } // allows clicks events to be caught void setClickListener(ItemClickListener itemClickListener) { this.mClickListener = itemClickListener; } // parent activity will implement this method to respond to click events untuk view// public interface ItemClickListener {// void onItemClick(View view, int position);// } public interface ItemClickListener { void onItemClick(View view, int position, TypeClick tipe); } private void circularImage(ImageView imageView, Bitmap srcBitmap) { Paint paint = new Paint(); int srcBitmapWidth = srcBitmap.getWidth(); int srcBitmapHeight = srcBitmap.getHeight(); int borderWidth = 30; // destination bitmap width int dstBitmapWidth = Math.min(srcBitmapWidth, srcBitmapHeight) + borderWidth * 2; //float radius = Math.min(srcBitmapWidth,srcBitmapHeight)/2; // Initializing a new bitmap to draw source bitmap, border and shadow Bitmap dstBitmap = Bitmap.createBitmap(dstBitmapWidth, dstBitmapWidth, Bitmap.Config.ARGB_8888); // Initialize a new canvas Canvas canvas = new Canvas(dstBitmap); // Draw a solid color to canvas canvas.drawColor(Color.WHITE); // Draw the source bitmap to destination bitmap by keeping border and shadow spaces canvas.drawBitmap(srcBitmap, (dstBitmapWidth - srcBitmapWidth) / 2, (dstBitmapWidth - srcBitmapHeight) / 2, null); // Use Paint to draw border paint.setStyle(Paint.Style.STROKE); // paint.setStrokeWidth(borderWidth * 2); paint.setColor(Color.WHITE); // Draw the border in destination bitmap canvas.drawCircle(canvas.getWidth() / 2, canvas.getHeight() / 2, canvas.getWidth() / 2, paint); // Use Paint to draw shadow paint.setColor(Color.LTGRAY); // Draw the shadow on circular bitmap canvas.drawCircle(canvas.getWidth() / 2, canvas.getHeight() / 2, canvas.getWidth() / 2, paint); RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(ctx.getResources(), dstBitmap); // Make the ImageView image to a circular image roundedBitmapDrawable.setCircular(true); roundedBitmapDrawable.setAntiAlias(true); // Set the ImageView image as drawable object imageView.setImageDrawable(roundedBitmapDrawable); } }
Class activity
package adehodijah.example.com.pertemuan4e; import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.media.AudioManager; import android.media.MediaPlayer; import android.net.Uri; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.View; import android.view.WindowManager; import android.widget.Button; import android.widget.MediaController; import android.widget.Toast; import android.widget.VideoView; import java.io.IOException; import java.util.ArrayList; public class ListClickNImageRefreshActivity extends AppCompatActivity implements MyRecyclerViewClickNImageRefreshAdapter.ItemClickListener { private DatabaseHelper dbhelper; private SQLiteDatabase db = null; private Cursor kamusCursor = null; private MyRecyclerViewClickNImageRefreshAdapter adapter; RecyclerView recyclerView; MediaPlayer mediaPlayer = new MediaPlayer(); private MediaController mediacontroller; View videoLayout; VideoView videoV; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_list); recyclerView = (RecyclerView) findViewById(R.id.listData); videoLayout = findViewById(R.id.videoLayout); videoLayout.setVisibility(View.INVISIBLE); videoV = (VideoView) findViewById(R.id.videoView); showData(); Button btn = (Button) findViewById(R.id.buttonAdd); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent inte = new Intent(ListClickNImageRefreshActivity.this, AddActivity.class); startActivity(inte); } }); } public void showData() { ArrayList<KamusImageEntity> istilahArtiKeterangan = new ArrayList<>(); KamusImageEntity dataKamusImageEntity1 = new KamusImageEntity("soa", "service", "yuuuuu", R.drawable.icon_audio, R.drawable.icon_video, R.raw.little_kitten, R.raw.greatestloveofall); KamusImageEntity dataKamusImageEntity2 = new KamusImageEntity("bpmn", "process", "hiii", R.drawable.icon_audio, R.drawable.icon_video, R.raw.sheep_sound, R.raw.whitneyhouston); istilahArtiKeterangan.add(dataKamusImageEntity1); istilahArtiKeterangan.add(dataKamusImageEntity2); adapter = new MyRecyclerViewClickNImageRefreshAdapter(this, istilahArtiKeterangan); RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext()); recyclerView.setLayoutManager(mLayoutManager); recyclerView.setAdapter(adapter); adapter.setClickListener(this); } @Override public void onItemClick(View view, int position, MyRecyclerViewClickNImageRefreshAdapter.TypeClick tipe) { KamusImageEntity lg = adapter.getItem(position); if (tipe == MyRecyclerViewClickNImageRefreshAdapter.TypeClick.IMAGE_LAGU) { Uri myUri = Uri.parse("android.resource://" + getPackageName() + "/" + lg.getLaguMP3()); mediaPlayer = new MediaPlayer(); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); try{ mediaPlayer.setDataSource(getApplicationContext(), myUri); } catch (IllegalArgumentException e) { Toast.makeText(getApplicationContext(), "You might not set the URL correctly", Toast.LENGTH_LONG).show(); } catch (SecurityException e) { Toast.makeText(getApplicationContext(), "You might not set the URL correctly", Toast.LENGTH_LONG).show(); } catch (IOException e) { e.printStackTrace(); } try{ mediaPlayer.prepare(); } catch (IllegalArgumentException e) { Toast.makeText(getApplicationContext(), "You might not set the URL correctly", Toast.LENGTH_LONG).show(); } catch (IOException e) { Toast.makeText(getApplicationContext(), "You might not set the URL correctly", Toast.LENGTH_LONG).show(); } mediaPlayer.start(); } else if (tipe == MyRecyclerViewClickNImageRefreshAdapter.TypeClick.IMAGE_VIDEO) { videoLayout = findViewById(R.id.videoLayout); videoV = (VideoView) findViewById(R.id.videoView); videoLayout.setVisibility(View.VISIBLE); this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); videoV = (VideoView) findViewById(R.id.videoView); videoV.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + lg.getVideoMP4())); videoV.setMediaController(new MediaController(this)); videoV.requestFocus(); videoV.start(); } } @Override protected void onResume() { super.onResume(); mediaPlayer.stop(); mediaPlayer.release(); } }
layout activity_list
<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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:background="#ecf0f1" > <Button android:id="@+id/buttonAdd" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" style="?android:attr/borderlessButtonStyle" android:drawableLeft="@drawable/icon_add" android:text="Tambah Data Baru" /> <android.support.v7.widget.RecyclerView android:id="@+id/listData" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/buttonAdd" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> <LinearLayout android:id="@+id/videoLayout" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <VideoView android:id="@+id/videoView" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout> </RelativeLayout>
Komentar
Posting Komentar