Why these random marks are appearing in every recyclerview and viewpager items? - android

I have 2 recycler views and a view pager in a fragment.
Everything else works fine but both recycler view items are showing a random mark on the upper left corner of every items. And same thing is happening for the view pager items.
This fragment is in a fragment activity and view pager is using pager adapter. And I am using Picasso library to load images on the items and also using network policy to cache the images in disk.
recycler view items appearing like this
view pager items appearing like this
Recycler View adapter
public class CourseListAdapter extends RecyclerView.Adapter<CourseListAdapter.CourseListViewHolder> {
public static final int HOME_PAGE = 1;
public static final int DISPLAY_COURSE = 2;
private Picasso mPicasso;
private List<DisplayCourse> courseList;
private static final String TAG = "CourseListAdapter";
private OnItemClickListener mListener;
public interface OnItemClickListener {
void onItemClick(int position, View view);
}
private String mSender;
public void setOnItemClickListener(OnItemClickListener listener) {
mListener = listener;
}
public CourseListAdapter(List<DisplayCourse> courseList, String sender) {
this.courseList = courseList;
this.mSender = sender;
mPicasso = Picasso.get();
}
#NonNull
#Override
public CourseListViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
switch (viewType) {
case 1:
View view1 = inflater.inflate(R.layout.recom_course_home_layout, parent, false);
return new CourseListViewHolder(view1, mListener);
case 2:
View view2 = inflater.inflate(R.layout.display_course_layout, parent, false);
return new CourseListViewHolder(view2, mListener);
default:
View view3 = inflater.inflate(R.layout.section_video_item_layout, parent, false);
return new CourseListViewHolder(view3, mListener);
}
}
#Override
public void onBindViewHolder(#NonNull CourseListViewHolder holder, int position) {
holder.title.setText(courseList.get(position).getCourseTitle());
mPicasso.load(courseList.get(position).getThumbnailURL())
.networkPolicy(NetworkPolicy.OFFLINE)
.into(holder.thumbnail, new Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError(Exception e) {
mPicasso.load(courseList.get(position).getThumbnailURL())
.error(R.drawable.ofklogo)
.into(holder.thumbnail, new Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError(Exception e) {
}
});
}
});
}
#Override
public int getItemCount() {
return courseList.size();
}
public static class CourseListViewHolder extends RecyclerView.ViewHolder {
ImageView thumbnail;
TextView title;
public CourseListViewHolder(#NonNull View itemView, final OnItemClickListener listener) {
super(itemView);
title = itemView.findViewById(R.id.courseTitle);
thumbnail = itemView.findViewById(R.id.courseThumbNailImageView);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int position = getAdapterPosition();
if (listener != null) {
if (position != RecyclerView.NO_POSITION) {
listener.onItemClick(position, view);
}
}
}
});
}
}
#Override
public int getItemViewType(int position) {
if (mSender.equals("home_page")) {
return HOME_PAGE;
} else if (mSender.equals("displayCourse")) {
return DISPLAY_COURSE;
}
return -1;
}
}
View pager adapter
public class VideoSliderAdapter extends PagerAdapter {
private boolean doNotifyDataSetChangedOnce = false;
private static final String TAG = "VideoSliderAdapter";
private YouTubePlayerView youTubePlayerView;
private View gradientView;
private ImageView thumbNail;
private LinearLayout layout;
private Picasso picasso;
private List<Video> videoList;
private Context mContext;
private Lifecycle mLifeCycle;
public VideoSliderAdapter(List<Video> videoList, Context context, Lifecycle lifecycle) {
this.videoList = videoList;
this.mContext = context;
this.mLifeCycle = lifecycle;
doNotifyDataSetChangedOnce = true;
picasso = Picasso.get();
}
#Override
public int getCount() {
if (doNotifyDataSetChangedOnce) {
doNotifyDataSetChangedOnce = false;
notifyDataSetChanged();
}
return videoList.size();
}
#Override
public int getItemPosition(#NonNull Object object) {
return POSITION_NONE;
}
#Override
public boolean isViewFromObject(#NonNull View view, #NonNull Object object) {
return (view == (CardView) object);
}
#NonNull
#Override
public Object instantiateItem(#NonNull ViewGroup container, int position) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.video_paly_layout, container, false);
TextView title = view.findViewById(R.id.videoTitle);
title.setText(videoList.get(position).getVideoTitle());
gradientView = view.findViewById(R.id.gradientView);
thumbNail = view.findViewById(R.id.videoThumbNail);
picasso.load(videoList.get(position).getVideoThumbNail())
.networkPolicy(NetworkPolicy.OFFLINE)
.into(thumbNail, new Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError(Exception e) {
picasso.load(videoList.get(position).getVideoThumbNail())
.error(R.drawable.ofklogo)
.into(thumbNail, new Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError(Exception e) {
}
});
}
});
layout = view.findViewById(R.id.videoPlayLayout);
youTubePlayerView = view.findViewById(R.id.youtube_player_view);
mLifeCycle.addObserver(youTubePlayerView);
new AddListener(youTubePlayerView, position).execute();
container.addView(view);
return view;
}
#Override
public void destroyItem(#NonNull ViewGroup container, int position, #NonNull Object object) {
container.removeView((CardView) object);
}
Recycler view holder layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="150dp"
android:layout_height="200dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:clickable="true"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackground"
app:cardCornerRadius="8dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/courseThumbNailImageView"
android:layout_width="match_parent"
android:layout_height="130dp"
android:layout_alignParentTop="true"
android:scaleType="fitXY"
android:src="#drawable/art_thumb" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/courseThumbNailImageView">
<TextView
android:id="#+id/courseTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:maxLines="2"
android:text="Course title"
android:textAlignment="center"
android:textColor="#android:color/black"
android:textStyle="bold" />
</RelativeLayout>
</RelativeLayout>
</androidx.cardview.widget.CardView>
view pager layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 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="200dp"
app:cardCornerRadius="8dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.pierfrancescosoffritti.androidyoutubeplayer.core.player.views.YouTubePlayerView
android:id="#+id/youtube_player_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:autoPlay="false" />
<ImageView
android:id="#+id/videoThumbNail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY" />
<View
android:id="#+id/gradientView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/gradient_drawable" />
<LinearLayout
android:id="#+id/videoPlayLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="16dp">
<ImageView
android:layout_width="48dp"
android:layout_height="48dp"
android:padding="8dp"
android:src="#drawable/play_button" />
<TextView
android:id="#+id/videoTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="2"
android:text="Course title"
android:textColor="#android:color/white"
android:textStyle="bold" />
</LinearLayout>
</RelativeLayout>
</androidx.cardview.widget.CardView>

When loading image using Picasso use: setIndicatorsEnabled(false)
picasso.load(videoList.get(position).getVideoThumbNail())
.networkPolicy(NetworkPolicy.OFFLINE)
.setIndicatorsEnabled(false)
.into(...);
The colors indicate this:
Green: Image is fetched from memory
Blue: Image is fetched from disk
Red: Image is fetched from network

Related

not getting id of a clicked child item inside RecyclerView item layout

I have implemented a RecyclerView inside a ConstraintLayout. I am having one child image element inside the layout. But when I click the child image, it always returns the ConstraintLayout, not the clicked image.
Could you please tell me why this is happening, what is the solution for this ?
I separately did bind listener to image, it is working but not able to get the RecyclerItem object. I need RecyclerItem object for the position to proceed.
I implemented it by binding elements via onBindViewholder method in Adapter. Below are the codes
customAdapter = new GridViewAdapter(recyclerItems, 1, this.getContext().getPackageName(),
new GridViewAdapter.OnItemClickListener(){
#Override
public void onItemClick(RecyclerItem item) {
CommonUtil.addFragment("REP", Constants.CONTAINER_HOME,
new ModifyFragment(), getActivity(), null);
}
}, R.layout.rec_view_item_stock, Constants.V_SPAN_LIST_8);
RecyclerView recyclerView = view.findViewById(R.id.stockListRecView);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setLayoutManager(new GridLayoutManager(this.getContext(), 1));
recyclerView.setAdapter(customAdapter);
//Adapter
public class GridViewAdapter extends RecyclerView.Adapter<GridViewAdapter.ViewHolder>{
private List<RecyclerItem> dataItems;
private int hSpan = 1;
private String packageName;
private final OnItemClickListener listener;
private int inflator;
private int vSpan;
public interface OnItemClickListener {
void onItemClick(RecyclerItem item);
}
public static class ViewHolder extends RecyclerView.ViewHolder {
private final ConstraintLayout constraintLayout;
private final TextView textView;
private final ImageView imageView;
private Object obj;
public ViewHolder(View view) {
super(view);
constraintLayout = (ConstraintLayout) view.findViewById(R.id.rec_content_layout);
textView = constraintLayout.findViewById(R.id.recTextView);
imageView = constraintLayout.findViewById(R.id.recImage);
}
public void bind(final RecyclerItem item, final OnItemClickListener listener) {
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
listener.onItemClick(item);
}
});
}
public TextView getTextView() {
return textView;
}
public ImageView getImageView() {
return imageView;
}
public Object getObj() {
return obj;
}
public void setObj(Object obj) {
this.obj = obj;
}
}
public GridViewAdapter(List<RecyclerItem> items, int spanCount, String packageName, OnItemClickListener listener,
int inflator, int vSpan) {
dataItems = items;
this.hSpan = spanCount;
this.packageName = packageName;
this.listener = listener;
this.inflator = inflator;
this.vSpan = vSpan;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int gridType) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(inflator, viewGroup, false);
ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
layoutParams.width = (viewGroup.getResources().getDisplayMetrics().widthPixels / hSpan) - 24;
if(Constants.V_SPAN_GRID == vSpan) {
layoutParams.height = layoutParams.width;
} else {
layoutParams.height = (viewGroup.getResources().getDisplayMetrics().widthPixels / vSpan);
}
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ViewHolder viewHolder, final int position) {
viewHolder.getTextView().setText(dataItems.get(position).getText());
int imgId = viewHolder.getImageView().getResources().getIdentifier(
dataItems.get(position).getImageName(), "drawable", packageName);
viewHolder.getImageView().setImageResource(imgId);
viewHolder.setObj(dataItems.get(position));
viewHolder.bind(dataItems.get(position), listener);
}
#Override
public int getItemCount() {
return dataItems.size();
}
//item layout
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/rec_content_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:layout_marginTop="8dp"
android:layout_marginLeft="4dp"
android:padding="8dp"
android:background="#drawable/border_top_bottom" >
<androidx.constraintlayout.widget.Guideline
android:id="#+id/viewstock_gline_1_v"
android:layout_width="1dp"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.6"/>
<ImageView
android:id="#+id/recImage"
android:background="#color/colorPrimaryDark"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp" />
<TextView
android:id="#+id/recTextView"
app:layout_constraintLeft_toRightOf="#+id/recImage"
app:layout_constraintTop_toTopOf="parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp" />
<ImageView
android:id="#+id/editstock_image"
android:src="#drawable/ic_edit_stock"
android:background="#color/colorPrimaryDark"
app:layout_constraintRight_toLeftOf="#+id/deletestock_image"
app:layout_constraintTop_toTopOf="parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp" />
<ImageView
android:id="#+id/deletestock_image"
android:src="#drawable/ic_delete_stock"
android:background="#color/colorPrimaryDark"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp" />
</androidx.constraintlayout.widget.ConstraintLayout>```
Try this!
public void bind(final RecyclerItem item, final OnItemClickListener listener, TextView itemTextView) {
itemTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
listener.onItemClick(item);
}
});
}
Pass every item text view instance from the view holder or fro onBindViewHolder itself implement onclick listener

Center ViewGroup views with Android-DraggableGridViewPager

I want to thanks zzhouj first of all, but I'm trying to use his Android-DraggableGridViewPager with some errors. this is his class:
Android-DraggableGridViewPager RAW
This is my layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="100">
<TextClock
android:gravity="center"
android:textColor="#color/colorBlack"
android:id="#+id/text_clock"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="15"
android:fontFamily="sans-serif-black"
android:text="20:15"
android:textSize="50sp"
android:textStyle="bold" />
<TextView
android:gravity="center"
android:id="#+id/text_date"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="10"
android:fontFamily="sans-serif-thin"
android:text="09/09/09"
android:textColor="#color/colorBlack"
android:textSize="20sp" />
<com.kangel.hybridlauncher.adapters.AppDraggableGridViewPager
android:foregroundGravity="center"
android:id="#+id/draggable_grid_view_pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="center"
android:layout_weight="75"
android:background="#color/colorAccent"
android:gravity="center" />
</LinearLayout>
I'm using it to create a simple gridview with draggable layout in, but if I reduce size of that ViewGroup, the element wont stay in center, because elements go on the left.
Do you have any ideas? Thanks in advance!
EDIT
public class AppArrayAdapter extends ArrayAdapter<AppObject> {
private Context context;
public AppArrayAdapter(#NonNull Context context, int resource) {
super(context, resource);
this.context = context;
}
#Override
public long getItemId(int position) {
return position;
}
#NonNull
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View v;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.gridview_app_layout, parent, false);
} else {
v = convertView;
}
LinearLayout layout = v.findViewById(R.id.single_item_core);
ImageView icon = layout.findViewById(R.id.app_layout);
TextView label = layout.findViewById(R.id.app_label);
final AppObject appObject = this.getItem(position);
icon.setImageDrawable(appObject.getIcon());
label.setText(appObject.getName());
return v;
}
#Override
public void add(#Nullable AppObject object) {
super.add(object);
notifyDataSetChanged();
}
#Override
public void addAll(#NonNull Collection<? extends AppObject> collection) {
super.addAll(collection);
notifyDataSetChanged();
}
#Override
public void addAll(AppObject... items) {
super.addAll(items);
notifyDataSetChanged();
}
#Override
public void insert(#Nullable AppObject object, int index) {
super.insert(object, index);
notifyDataSetChanged();
}
#Override
public void remove(#Nullable AppObject object) {
super.remove(object);
notifyDataSetChanged();
}
#Override
public void clear() {
super.clear();
notifyDataSetChanged();
}
}
EDIT
I set adapter in MainActivity:
DraggableGridViewPager mDraggableGridViewPager = mViewAppsScroll.findViewById(R.id.draggable_grid_view_pager);
mAppAdapter = new AppArrayAdapter(this, 0);
mAppAdapter.addAll(getList());
mDraggableGridViewPager.setAdapter(mAppAdapter);

Horizontal RecyclerView inside Vertical RecyclerView

I'm trying to make something like this.
So the idea is that i have vertical recyclerview with channels and on second position of channel I should have a horizontal recyclerview with relives.
I'm not sure how I'm supposed to do that, I tried messing with viewholders and guess I should make only one recyclerview in my channel_details layout, and another one as an item in item_channel_details, but I cant get it to work.
Here is my code.
ChannelDetailsActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_channel_details);
ImageView coverPhoto = (ImageView) findViewById(R.id.image_cover_details);
final HexagonImageView avatarPhoto = (HexagonImageView) findViewById(R.id.img_hex);
TextView toolbarText = (TextView) findViewById(R.id.txt_toolbar_title);
final Bundle b = getIntent().getExtras();
final MNetworkChannel parcelChannel =
b.getParcelable(Const.IntentData.H_CHANNEL_LIST);
final MVideosForChannel parcelVideosForChannel = b.getParcelable(Const.IntentData.D_VIDEOS_LIST);
setChannelsView();
setVideosView();
}
private void setChannelsView() {
rvRelive = (RecyclerView) findViewById(R.id.rv_relive_details);
rvRelive.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
adapterRelives = new ReliveAdapter();
rvRelive.setAdapter(adapterRelives);
if (getIntent() != null && getIntent().getParcelableExtra(Const.IntentData.D_RELIVE_LIST) != null) {
adapterRelives.setData(((ReliveMainPojo) getIntent().getParcelableExtra(Const.IntentData.D_RELIVE_LIST)).relives);
}
}
private void setVideosView() {
rvVideos = (RecyclerView) findViewById(R.id.rv_videos);
rvVideos.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
adapterVideos = new ChannelVideosAdapter();
rvVideos.setAdapter(adapterVideos);
if (getIntent() != null && getIntent().getParcelableExtra(Const.IntentData.D_VIDEOS_LIST) != null) {
adapterVideos.setData(((MVideosForChannel) getIntent().getParcelableExtra(Const.IntentData.D_VIDEOS_LIST)).experience);
}
}
ChannelDetails adapter:
public final class ChannelVideosAdapter extends RecyclerView.Adapter<ChannelVideosAdapter.ViewHolder> {
private List<MVideo> data = new ArrayList<>();
public ChannelVideosAdapter() {
}
public void setData(List<MVideo> newData) {
if (newData != null && !newData.isEmpty()) {
data = newData;
notifyDataSetChanged();
}
}
public void clearData() {
data.clear();
notifyDataSetChanged();
}
#Override
public final ChannelVideosAdapter.ViewHolder onCreateViewHolder(final ViewGroup parent, final int viewType) {
return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_video_recycle_tile, parent, false));
}
#Override
public final void onBindViewHolder(final ChannelVideosAdapter.ViewHolder holder, final int position) {
final MVideo video = data.get(position);
final String videoBackgroundImageUrl = video.asset.frame;
final String videoName = video.name;
ImageLoader.getInstance().displayImage(videoBackgroundImageUrl, holder.coverPhoto, new ImageLoadingListener() {
#Override
public void onLoadingStarted(String imageUri, View view) {
holder.videoLoading.setVisibility(View.VISIBLE);
}
#Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
holder.videoLoading.setVisibility(View.GONE);
}
#Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
holder.videoLoading.setVisibility(View.GONE);
}
#Override
public void onLoadingCancelled(String imageUri, View view) {
holder.videoLoading.setVisibility(View.GONE);
}
});
holder.videoName.setText(videoName);
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
VideoPlayerActivity.StartNewVideoPlayerActivity((ChannelDetailsActivity) holder.itemView.getContext(), video, true);
}
});
}
#Override
public final int getItemCount() {
return data.size();
}
final class ViewHolder extends RecyclerView.ViewHolder {
private final ImageView coverPhoto;
private final TextView videoName;
private final ProgressBar videoLoading;
ViewHolder(final View itemView) {
super(itemView);
coverPhoto = (ImageView) itemView.findViewById(R.id.img_thumbnail_background_video);
videoName = (TextView) itemView.findViewById(R.id.txt_video_name);
videoLoading = (ProgressBar) itemView.findViewById(R.id.pb_video_loading);
}
}
}
Relive Adapter:
public final class ReliveAdapter extends RecyclerView.Adapter<ReliveAdapter.ViewHolder> {
private List<Relive> data = new ArrayList<>();
public ReliveAdapter() {
}
public void setData(List<Relive> newData) {
if (newData != null && !newData.isEmpty()) {
data = newData;
notifyDataSetChanged();
}
}
public void clearData() {
data.clear();
notifyDataSetChanged();
}
#Override
public final ReliveAdapter.ViewHolder onCreateViewHolder(final ViewGroup parent, final int viewType) {
return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_relive_recycle_tile, parent, false));
}
#Override
public void onBindViewHolder(final ReliveAdapter.ViewHolder holder, final int position) {
final Relive relive = data.get(position);
final String reliveOwnerIconUrl = relive.owner.asset.large;
final String reliveCoverPhotoUrl = relive.asset.stream.thumbnail;
final String reliveDescription = relive.owner.name;
ImageLoader.getInstance().displayImage(reliveCoverPhotoUrl, holder.backgroundImage, new ImageLoadingListener() {
#Override
public void onLoadingStarted(String imageUri, View view) {
holder.imageLoading.setVisibility(View.VISIBLE);
}
#Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
holder.imageLoading.setVisibility(View.GONE);
}
#Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
holder.imageLoading.setVisibility(View.GONE);
ImageLoader.getInstance().displayImage(reliveOwnerIconUrl, holder.profilePicture, new ImageLoadingListener() {
#Override
public void onLoadingStarted(String imageUri, View view) {
holder.imageLoading.setVisibility(View.VISIBLE);
}
#Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
holder.imageLoading.setVisibility(View.GONE);
}
#Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
holder.imageLoading.setVisibility(View.GONE);
}
#Override
public void onLoadingCancelled(String imageUri, View view) {
holder.imageLoading.setVisibility(View.GONE);
}
});
holder.eyeIcon.setImageResource(R.drawable.relive);
}
#Override
public void onLoadingCancelled(String imageUri, View view) {
holder.imageLoading.setVisibility(View.GONE);
}
});
holder.reliveDescription.setText(reliveDescription);
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
RelivePlayerActivity.StartReliveReviewActivity((ChannelDetailsActivity) holder.itemView.getContext(), relive.asset.stream.url, relive.experienceGuid, relive.guid, holder.getAdapterPosition());
}
});
}
#Override
public final int getItemCount() {
return data.size();
}
final class ViewHolder extends RecyclerView.ViewHolder {
private final CircleImageView profilePicture;
private final ImageView eyeIcon;
private final ImageView backgroundImage;
private final TextView reliveDescription;
private final ProgressBar imageLoading;
ViewHolder(View itemView) {
super(itemView);
profilePicture = (CircleImageView) itemView.findViewById(R.id.profile_circle_image);
eyeIcon = (ImageView) itemView.findViewById(R.id.icon_circle_image);
backgroundImage = (ImageView) itemView.findViewById(R.id.thumbnail_image);
reliveDescription = (TextView) itemView.findViewById(R.id.description_textview);
imageLoading = (ProgressBar) itemView.findViewById(R.id.image_loading);
}
}
}
This is my layout:
activity_channel_details
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar_details"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#017789"
android:textAlignment="center">
<TextView
android:id="#+id/txt_toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="#android:color/white"
android:textStyle="bold" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_marginEnd="10dp"
android:background="#color/white_trans"
android:src="#drawable/zeality" />
</android.support.v7.widget.Toolbar>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
android:orientation="vertical">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffff"
android:orientation="vertical">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffff"
android:orientation="vertical">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/image_cover_details"
android:layout_width="match_parent"
android:layout_height="120dp"
android:adjustViewBounds="true"
android:scaleType="fitXY" />
<FrameLayout
android:id="#+id/frame"
android:layout_width="100dp"
android:layout_height="110dp"
android:layout_centerInParent="true">
<co.zeality.vrplayer.views.HexagonImageView
android:id="#+id/img_hex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:scaleType="fitXY" />
</FrameLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp">
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_videos"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:nestedScrollingEnabled="false" />
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_relive_details"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
item_video_recycle_tile
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/img_thumbnail_background_video"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true" />
<FrameLayout
android:id="#+id/frame_image"
android:layout_width="150dp"
android:layout_height="100dp"
android:layout_centerInParent="true">
<ProgressBar
android:id="#+id/pb_video_loading"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:foregroundGravity="center"
android:visibility="gone" />
<ImageView
android:layout_width="80dp"
android:layout_height="50dp"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="40dp"
android:src="#drawable/glasses" />
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/img_play_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginStart="10dp"
android:src="#drawable/play_no_circle" />
<TextView
android:id="#+id/txt_video_name"
android:layout_width="wrap_content"
android:layout_marginBottom="5dp"
android:layout_height="match_parent"
android:layout_below="#id/img_play_button"
android:layout_marginStart="5dp"
android:textColor="#FFF" />
</RelativeLayout>
</FrameLayout>
<View
android:layout_width="match_parent"
android:layout_height="5dp"
android:layout_alignParentBottom="true"
android:background="#660c7582"></View>
you should use one recyclerView (vertical) as parent and at time of bind view in adapter at position 1 you return one view which is contain recyclerView(horizontal) and load other adapter for that recyclerView. please refer diagram for proper understanding.
Main RecyclerView (vertical):
---------------------------
+ Item 1
---------------------------
+ Second RecyclerView (Horizontal)
---------------------------
+ Item 2
---------------------------
Parent RecyclerView Adapter code:
#Override
public int getItemViewType(int position) {
if (position == 1)
return 0;
else
return 1;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == 0) {
View v = LayoutInflater.from(context).inflate(R.layout.your_second_recylerView_layout, parent, false);
return new ViewHolder1(v);
}
else{
View v = LayoutInflater.from(context).inflate(R.layout.your_item_layout, parent, false);
return new ViewHolder2(v);
}
}
now you need to implement second Adapter for horizontal recycleView.

onClick is not working in cardView

I am developing an app in which i am using recyclerView and cardView.I am retrieving data from server and displaying it.When i am trying to implement onclick method on this,it is not working.:
Here is my code for main activity:
public class Tab_1_Activity extends Fragment {
private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
private RecyclerView.Adapter adapter;
SwipeRefreshLayout mSwipeRefreshLayout;
private Config config;
private List<ListItem> upcomingJobs = new ArrayList<>();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_tab_1, container, false);
recyclerView = (RecyclerView) v.findViewById(R.id.recyclerview);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
mSwipeRefreshLayout = (SwipeRefreshLayout) v.findViewById(R.id.swifeRefresh);
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
getData();
upcomingJobs.clear();
upcomingJobs.addAll(upcomingJobs);
// fire the event
adapter.notifyDataSetChanged();
// uAdapter.notifyDataSetChanged();
}
});
//Make call to AsyncTask
getData();
return v;
}
For Adapter Class:
public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder> implements View.OnClickListener {
List<ListItem> items;
public CardAdapter(String[] offer, String[] offerprice, Bitmap[] image, String[] price, String[] url) {
super();
items = new ArrayList<ListItem>();
for (int i = 0; i < offer.length; i++) {
ListItem item = new ListItem();
item.setoffer(offer[i]);
item.seturl(url[i]);
item.setofferprice(offerprice[i]);
item.setprice(price[i]);
item.setimage(image[i]);
items.add(item);
}
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.card_view_list, parent, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
ListItem list = items.get(position);
holder.imageView.setImageBitmap(list.getimage());
holder.offer.setText(list.getoffer());
holder.url.setText(list.geturl());
holder.offerprice.setText(list.getofferprice());
holder.price.setText(list.getprice());
holder.itemView.setOnClickListener(this) ;
}
#Override
public int getItemCount() {
return items.size();
}
#Override
public void onClick(View v) {
Intent i=new Intent(v.getContext(),Settings.class);
v.getContext().startActivity(i);
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public ImageView imageView;
public View view;
public TextView offer, offerprice, price, url;
public ViewHolder(View itemView) {
super(itemView);
imageView = (ImageView) itemView.findViewById(R.id.imageView);
offer = (TextView) itemView.findViewById(R.id.offer);
offerprice = (TextView) itemView.findViewById(R.id.offerprice);
price = (TextView) itemView.findViewById(R.id.offerprice);
url = (TextView) itemView.findViewById(R.id.url);
}
}
}
card_view_list.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground"
android:paddingLeft="2dp"
android:paddingRight="2dp"
android:paddingTop="10dp">
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground"
card_view:cardCornerRadius="2dp"
card_view:cardUseCompatPadding="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground"
android:scaleType="centerCrop"
/>
<TextView
android:id="#+id/url"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textAllCaps="true"
android:layout_below="#id/imageView"
android:textColor="#color/colorAccent"
android:textSize="18sp"
android:textStyle="bold"/>
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="3dp"
android:orientation="horizontal">
</LinearLayout>
<TextView
android:id="#+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/textSecondary"
android:textSize="16dp"
android:textStyle="bold|italic"
android:layout_below="#id/url"
android:layout_marginTop="10dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:id="#+id/offer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:textColor="#color/textSecondary"
android:textSize="16sp" />
<TextView
android:id="#+id/offerprice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:textSize="20sp"
android:textStyle="italic" />
</RelativeLayout>
</android.support.v7.widget.CardView>
you have to write a listener for RecyclerVeiw .
public interface ClickListener {
void onClick(View view, int position);
void onLongClick(View view, int position);
}
public static class RecyclerTouchListener implements RecyclerView.OnItemTouchListener {
private GestureDetector gestureDetector;
private MainActivity.ClickListener clickListener;
public RecyclerTouchListener(Context context, final RecyclerView recyclerView, final MainActivity.ClickListener clickListener) {
this.clickListener = clickListener;
gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
#Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
#Override
public void onLongPress(MotionEvent e) {
View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
if (child != null && clickListener != null) {
clickListener.onLongClick(child, recyclerView.getChildPosition(child));
}
}
});
}
#Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
View child = rv.findChildViewUnder(e.getX(), e.getY());
if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) {
clickListener.onClick(child, rv.getChildPosition(child));
}
return false;
}
#Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}
#Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
}
usage:
recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getApplicationContext(), recyclerView, new RecyclerTouchListener.ClickListener() {
#Override
public void onClick(View view, int position) {
Toast.makeText(getApplicationContext(), "item is selected!", Toast.LENGTH_SHORT).show();
}
#Override
public void onLongClick(View view, int position) {
}
}));
It didn't work for me too, what I did was give an id to the root layout element in my row layout (which in your case is card_view_list). Then reference it in my ViewHolder and set the click listener to that view. Try it out.
Hope it helps.

Android flipview in a fragment not working

Am trying to make a ui with flipview as in this tutorial. IThe tutorial deals with activities, but i want it in fragments case. That ie, the flip effect as well as its parent is a fragment. When i use activity it works, but on using fragment,only the empty textview shows up. Can anyone help me with this?
This is some part of the code
page.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff3333" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="300dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/banner"
android:id="#+id/head"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="20dp"
android:id="#+id/header"
android:gravity="center"/>
</RelativeLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/des"
android:textSize="30dp"
android:gravity="center"/>
</FrameLayout>
fragment_news_feed :
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:flipview="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<se.emilsjolander.flipview.FlipView
android:id="#+id/flip_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
flipview:orientation="vertical"
tools:context="com.excel.excelapp.fragment.NewsFeedFragment" >
</se.emilsjolander.flipview.FlipView>
<TextView
android:id="#+id/empty_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Empty!"
android:textSize="32sp"
android:visibility="gone" />
</merge>
NewsFeedFragment.java
public class NewsFeedFragment extends Fragment implements NewsFlipAdapter.Callback, FlipView.OnFlipListener, FlipView.OnOverFlipListener{
private FlipView mFlipView;
private NewsFlipAdapter mAdapter;
int i=0,no_of_items=7;
public NewsFeedFragment() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
LinearLayout wrapper = new LinearLayout(getActivity());
View view = inflater.inflate(R.layout.fragment_news_feed, wrapper, true);
setUpView(view);
return wrapper;
}
private void setUpView(View view) {
mFlipView = (FlipView) view.findViewById(R.id.flip_view);
mAdapter = new NewsFlipAdapter(getActivity());
mAdapter.setCallback(this);
mFlipView.setAdapter(mAdapter);
mFlipView.setOnFlipListener(this);
mFlipView.peakNext(false);
mFlipView.setOverFlipMode(OverFlipMode.RUBBER_BAND);
mFlipView.setEmptyView(view.findViewById(R.id.empty_view));
mFlipView.setOnOverFlipListener(this);
}
#Override
public void onPageRequested(int page) {
mFlipView.smoothFlipTo(page);
}
#Override
public void onFlippedToPage(FlipView v, int position, long id) {
Log.i("pageflip", "Page: " + position);
if(position > mFlipView.getPageCount()-3 && mFlipView.getPageCount()<30){
mAdapter.addItems(5);
}
}
#Override
public void onOverFlip(FlipView v, OverFlipMode mode,
boolean overFlippingPrevious, float overFlipDistance,
float flipDistancePerPage) {
Log.i("overflip", "overFlipDistance = "+overFlipDistance);
}
NewsFlipAdapter.java
public class NewsFlipAdapter extends BaseAdapter {
public interface Callback {
public void onPageRequested(int page);
}
static class Item {
static long id = 0;
long mId;
public Item() {
mId = id++;
}
long getId() {
return mId;
}
}
private LayoutInflater inflater;
private Callback callback;
private List<Item> items = new ArrayList<Item>();
public NewsFlipAdapter(Context context) {
inflater = LayoutInflater.from(context);
}
public void setCallback(Callback callback) {
this.callback = callback;
}
#Override
public int getCount() {
return items.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return items.get(position).getId();
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.page, parent, false);
holder.heading = (TextView) convertView.findViewById(R.id.header);
holder.description = (TextView) convertView.findViewById(R.id.des);
holder.header = (ImageView) convertView.findViewById(R.id.head);
// holder.firstPage = (Button) convertView.findViewById(R.id.first_page);
// holder.lastPage = (Button) convertView.findViewById(R.id.last_page);
// holder.firstPage.setOnClickListener(this);
// holder.lastPage.setOnClickListener(this);
// convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
static class ViewHolder {
TextView heading;
ImageView header;
TextView description;
}
/* #Override
public void onClick(View v) {
switch(v.getId()){
case R.id.first_page:
if(callback != null){
callback.onPageRequested(0);
}
break;
case R.id.last_page:
if(callback != null){
callback.onPageRequested(getCount()-1);
}
break;
}
}*/
public void addItems(int amount) {
TextView text;
for (int i = 0; i < amount; i++) {
items.add(new Item());
}
notifyDataSetChanged();
}
public void addItemsBefore(int amount) {
for (int i = 0; i < amount; i++) {
items.add(0, new Item());
}
notifyDataSetChanged();
}
I am not a 100 % sure about the following explanation, so please correct me if I am wrong.
The <merge> tag merges the inflated layout with it's parent view.
It generally doesn't seem to sit well with fragments as explained in more detail here.
So try to replace merge with LinearLayout in your fragment_newsfeed_layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:flipview="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<se.emilsjolander.flipview.FlipView
android:id="#+id/flip_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
flipview:orientation="vertical"
tools:context="com.excel.excelapp.fragment.NewsFeedFragment" >
</se.emilsjolander.flipview.FlipView>
<TextView
android:id="#+id/empty_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Empty!"
android:textSize="32sp"
android:visibility="gone" />
</LinearLayout>

Categories

Resources