எவ்வாறு Android Studio யில் View Pager ஐ உள்ளீடு செய்வது ?



1. app > res > layout> view_pager_layout க்கு சென்று கீழே தரப்பட்டுள்ள Code ஐ உள்ளீடு செய்யுங்கள்.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black">


<VideoView
android:id="@+id/videoView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.5" />

<TextView
android:id="@+id/OpenVideoTitle1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginBottom="24dp"
android:text="Video Title"
android:textColor="@color/white"
android:textSize="15dp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent" />

<ImageView
android:id="@+id/OpenVideoPlayButton1"
android:layout_width="70dp"
android:layout_height="70dp"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="@+id/videoView1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/videoView1"
app:layout_constraintTop_toTopOf="@+id/videoView1"
app:srcCompat="@drawable/play_button_icon" />

<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar_open"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/black"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:titleTextColor="@color/black" />

<ProgressBar
android:id="@+id/BuffringProgressBar1"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="@+id/videoView1"
app:layout_constraintEnd_toEndOf="@+id/videoView1"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="@+id/videoView1"
app:layout_constraintTop_toTopOf="@+id/videoView1" />

</androidx.constraintlayout.widget.ConstraintLayout>
2.app > java > main> ViewPager க்கு சென்று கீழே தரப்பட்டுள்ள Code ஐ உள்ளீடு செய்யுங்கள்.
package com.mac.it.solution.vidworld2.ViewPager;

import android.os.Bundle;
import android.view.WindowManager;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager2.widget.ViewPager2;

import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.database.FirebaseDatabase;
import com.mac.it.solution.vidworld2.R;


public class ViewPager extends AppCompatActivity {
ViewPager2 viewPager;
ViewPagerAdapter viewPagerAdapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_pager);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

viewPager =(ViewPager2) findViewById(R.id.ViewPager);

FirebaseRecyclerOptions<ViewPagerModel> options =
new FirebaseRecyclerOptions.Builder<ViewPagerModel>()
.setQuery(FirebaseDatabase.getInstance().getReference("Posts"), ViewPagerModel.class)

.build();
viewPagerAdapter = new ViewPagerAdapter(options);
viewPager.setAdapter(viewPagerAdapter);




}

@Override
protected void onStart()
{
super.onStart();
viewPagerAdapter.startListening();
}
@Override
protected void onStop()
{
super.onStop();
viewPagerAdapter.stopListening();
}
@Override
public void onPause() {

super.onPause();
}

@Override
public void onResume() {
super.onResume();

}


@Override
public void onDestroy() {

super.onDestroy();
}

}

3. app > java > main> ViewPagerAdapter க்கு சென்று கீழே தரப்பட்டுள்ள Code ஐ உள்ளீடு செய்யுங்கள்.

package com.mac.it.solution.vidworld2.ViewPager;

import android.media.MediaPlayer;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.VideoView;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;

import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.mac.it.solution.vidworld2.R;


import static androidx.recyclerview.widget.RecyclerView.ViewHolder;

public class ViewPagerAdapter extends FirebaseRecyclerAdapter<ViewPagerModel, ViewPagerAdapter.myViewHolder >{


public ViewPagerAdapter(@NonNull FirebaseRecyclerOptions<ViewPagerModel> options) {
super(options);
}

@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
@Override
protected void onBindViewHolder(@NonNull myViewHolder holder, int position, @NonNull ViewPagerModel model) {
holder.setdata(model);

}

@NonNull
@Override
public myViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View row = LayoutInflater.from(parent.getContext()).inflate(R.layout.view_pager,parent,false);
return new myViewHolder(row);

}
@Override
public ViewPagerModel getItem(int pos) {
return super.getItem(getItemCount()- 1 - pos);
}



public class myViewHolder extends ViewHolder {
VideoView VideoView;
TextView VideoTitle,VideoUrl;
ProgressBar bufferProgressBar;
ImageView VideoPlayButton;


public myViewHolder(@NonNull View itemView) {
super(itemView);
VideoTitle = itemView.findViewById(R.id.OpenVideoTitle1);
bufferProgressBar = itemView.findViewById(R.id.BuffringProgressBar1);
VideoPlayButton = itemView.findViewById(R.id.OpenVideoPlayButton1);
VideoView = itemView.findViewById(R.id.videoView1);
}

@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
void setdata(ViewPagerModel obj) {
VideoView.setVideoPath(obj.getVideo());
VideoTitle.setText(obj.getTitle());
VideoView.setOnInfoListener(new MediaPlayer.OnInfoListener() {
@Override
public boolean onInfo(MediaPlayer mp, int what, int extra) {
if (MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START == what) {
bufferProgressBar.setVisibility(View.GONE);
}
if (MediaPlayer.MEDIA_INFO_BUFFERING_START == what) {
bufferProgressBar.setVisibility(View.VISIBLE);
}
if (MediaPlayer.MEDIA_INFO_BUFFERING_END == what) {
bufferProgressBar.setVisibility(View.VISIBLE);
}
return false;
}
});



VideoView.start();
VideoView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
VideoView.pause();
VideoPlayButton.setVisibility(View.VISIBLE);
}
});


VideoPlayButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
VideoView.start();
VideoPlayButton.setVisibility(View.INVISIBLE);
}
});


}
}


}


3. app > java > main> ViewPagerModel க்கு சென்று கீழே தரப்பட்டுள்ள Code ஐ உள்ளீடு செய்யுங்கள்.
package com.mac.it.solution.vidworld2.ViewPager;

import com.google.firebase.database.ServerValue;

public class ViewPagerModel {
private String title;
private String pictureurl;
private String video;
private Object timeStamp;
private String postKey;

public ViewPagerModel(String title, String SelectedCategory, String pictureurl, String video){
this.title = title;
this.pictureurl = pictureurl;
this.video = video;
this.timeStamp = ServerValue.TIMESTAMP;
}
ViewPagerModel(){

}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getPictureurl() {
return pictureurl;
}

public void setPictureurl(String pictureurl) {
this.pictureurl = pictureurl;
}

public String getVideo() {
return video;
}

public void setVideo(String video) {
this.video = video;
}

public Object getTimeStamp() {
return timeStamp;
}

public void setTimeStamp(Object timeStamp) {
this.timeStamp = timeStamp;
}

public String getPostKey() {
return postKey;
}

public void setPostKey(String postKey) {
this.postKey = postKey;
}
}


கருத்துரையிடுக

Post a Comment (0)

புதியது பழையவை