How can i design custom seekbar below image like
does anyone have an idea please guide me. Adavance thanks to all
build.gradle add this dependecncy
implementation 'com.android.support:recyclerview-v7:27.1.1'
Java Class
recyclerView = findViewById(R.id.recycler_view);
LinearLayoutManager layoutManager = new LinearLayoutManager(getBaseContext());
layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
recyclerView.setLayoutManager(layoutManager);
seekBarList = new ArrayList();
for (int i = 0; i < 10; i++) {
seekBarList.add(i);
}
SeekBarAdapter seekBarAdapter = new SeekBarAdapter(MainActivity.this, seekBarList);
recyclerView.setAdapter(seekBarAdapter);
Adapter Class
public class SeekBarAdapter extends RecyclerView.Adapter<SeekBarAdapter.MyViewHolder> {
private Context context;
private ArrayList<Integer> seekList;
public SeekBarAdapter(MainActivity mainActivity, ArrayList<Integer> barCodeGetList) {
this.context = mainActivity;
this.seekList = barCodeGetList;
}
#NonNull
public SeekBarAdapter.MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.seekbaritem, parent, false);
return new SeekBarAdapter.MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(#NonNull SeekBarAdapter.MyViewHolder holder, #SuppressLint("RecyclerView") final int position) {
if (position > 0) {
holder.img_value_indicater.setVisibility(View.GONE);
}
holder.txt_value.setText(seekList.get(position).toString());
}
#Override
public int getItemCount() {
return seekList.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView txt_value;
View view_value;
ImageView img_value_indicater;
LinearLayout lay_seekbar_margin;
MyViewHolder(View view) {
super(view);
txt_value = view.findViewById(R.id.txt_value);
view_value = view.findViewById(R.id.view_value);
img_value_indicater = view.findViewById(R.id.img_select_indicater);
lay_seekbar_margin = view.findViewById(R.id.lay_seekmargin);
}
}
}
Adapter layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:orientation="vertical">
<LinearLayout
android:id="#+id/lay_seekmargin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:orientation="vertical">
<View
android:id="#+id/view_value"
android:layout_width="1dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:background="#000000" />
<TextView
android:id="#+id/txt_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp" />
<ImageView
android:id="#+id/img_select_indicater"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_seek_indicator" />
</LinearLayout>
</LinearLayout>
Related
I use navigationview. I want to use two icons in navigation view. Currently, when I use .seticon, it changes 2 icons. I want to define icons in two icons separately. I want to define separate icons for navIconNew and navIcon. How can we do that?
Navigation Adapter
public class NavigationAdapter extends RecyclerView.Adapter<NavigationAdapter.ViewHolder> {
Context context;
ArrayList<NavigationDataModel> arrayList = new ArrayList<>();
public NavigationAdapter(Context context, ArrayList<NavigationDataModel> arrayList) {
this.context = context;
this.arrayList = arrayList;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.navigationrecyclerview_adapter11, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.navIcon.setImageResource(arrayList.get(position).getIcon());
holder.navIconNew.setImageResource(arrayList.get(position).getIcon());
holder.rootView.setBackgroundColor(arrayList.get(position).getColor());
holder.navTitle.setText(arrayList.get(position).getTitle());
}
#Override
public int getItemCount() {
return arrayList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView navIcon, navIconNew;
TextView navTitle;
LinearLayout rootView;
public ViewHolder(View itemView) {
super(itemView);
rootView = itemView.findViewById(R.id.rootView);
navIcon = itemView.findViewById(R.id.navIcon);
navIconNew = itemView.findViewById(R.id.navIconNew);
navTitle = itemView.findViewById(R.id.navTitle);
}
}
navigationrecyclerview_adapter11
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="#+id/rootView"
android:orientation="horizontal"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/navIcon"
android:layout_margin="10dp"
android:layout_marginTop="10dp"
android:layout_alignParentLeft="true"
android:tint="#color/white"
android:layout_width="30dp"
android:layout_height="30dp"
/>
<ImageView
android:id="#+id/navIconNew"
android:layout_margin="10dp"
android:layout_marginTop="10dp"
android:layout_alignParentLeft="true"
android:tint="#color/white"
android:layout_width="30dp"
android:layout_height="30dp"
/>
<TextView
android:id="#+id/navTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:lineSpacingMultiplier="0.6"
android:text="Item"
android:fontFamily="#font/sanspro_semibold"
android:textColor="#color/white"
android:textSize="20dp"
android:layout_marginTop="10dp"
/>
</LinearLayout>
Activity
NavigationDataModel model8 = new NavigationDataModel();
model8.setColor(ContextCompat.getColor(this, R.color.red));
model8.setTitle("Mesaj At");
model8.setIcon(R.drawable.unread);
arrayList.add(model8);
I have a recycler inside fragment in view pager.
The problem that it put likes on user accounts in UI randomly but in DB everything is fine.
In logs, I see that random is not influence on BD. So the bug is only in UI part. After the refresh of the list, it can appear again and after that disappear.
I would like to share code but I already don't have an I idea where the problem could be. It might be in adapter/refresh list listener / XML or any other places. Please let me know what part of the code you need and I will provide it. Maybe it is a bug of recycler as itself and I can't fix it.
Adapter class code:
public class SearchAdapter extends RecyclerView.Adapter<SearchAdapter.UserViewHolder>{
private List<FsUser> fsUserList = new ArrayList<>();
private OnItemClickListener.OnItemClickCallback onItemClickCallback;
private OnItemClickListener.OnItemClickCallback onChatClickCallback;
private OnItemClickListener.OnItemClickCallback onLikeClickCallback;
private Context context;
public SearchAdapter(OnItemClickListener.OnItemClickCallback onItemClickCallback,
OnItemClickListener.OnItemClickCallback onChatClickCallback,
OnItemClickListener.OnItemClickCallback onLikeClickCallback) {
this.onItemClickCallback = onItemClickCallback;
this.onChatClickCallback = onChatClickCallback;
this.onLikeClickCallback = onLikeClickCallback;
}
public void addUsers(List<FsUser> userList) {
fsUserList.addAll(userList);
notifyItemRangeInserted(fsUserList.size() - userList.size(), fsUserList.size());
}
public void clearData(){
fsUserList.clear();
notifyDataSetChanged();
}
#NonNull
#Override
public UserViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
context = parent.getContext();
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_user, parent, false);
return new UserViewHolder(v);
}
#Override
public void onBindViewHolder(#NonNull UserViewHolder holder, int position) {
FsUser fsUser = fsUserList.get(position);
holder.bind(fsUser, position);
}
#Override
public int getItemCount() {
return fsUserList.size();
}
public String getLastItemId(){
return fsUserList.get(fsUserList.size() - 1).getUid();
}
class UserViewHolder extends RecyclerView.ViewHolder {
RelativeLayout container;
ImageView imageView, like, chat;
TextView name, country;
private LottieAnimationView animationView;
UserViewHolder(View itemView) {
super(itemView);
context = itemView.getContext();
container = itemView.findViewById(R.id.item_user_container);
imageView = itemView.findViewById(R.id.user_img);
like = itemView.findViewById(R.id.search_btn_like);
chat = itemView.findViewById(R.id.search_btn_chat);
name = itemView.findViewById(R.id.user_name);
country = itemView.findViewById(R.id.user_country);
animationView = itemView.findViewById(R.id.lottieAnimationView);
}
void bind(FsUser fsUser, int position){
ViewCompat.setTransitionName(imageView, fsUser.getName());
if (FirebaseUtils.isUserExist() && fsUser.getUid() != null) {
new FriendRepository().isLiked(fsUser.getUid(), flag -> {
if (flag) {
like.setBackground(ContextCompat.getDrawable(context, R.drawable.ic_favorite));
animationView.setVisibility(View.VISIBLE);
}
});
}
if(fsUser.getUid() != null) {
chat.setOnClickListener(new OnItemClickListener(position, onChatClickCallback));
like.setOnClickListener(new OnItemClickListener(position, onLikeClickCallback));
}
imageView.setOnClickListener(new OnItemClickListener(position, onItemClickCallback));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
if(fsUser.getImage().equals("default")){
Glide.with(context).load(context.getResources().getDrawable(R.drawable.default_avatar)).into(imageView);
} else {
Glide.with(context).load(fsUser.getImage()).thumbnail(0.5f).into(imageView);
}
name.setText(fsUser.getName());
country.setText(fsUser.getCountry());
ValueAnimator animator = ValueAnimator.ofFloat(0f, 1f).setDuration(500);
animator.addUpdateListener(valueAnimator ->
animationView.setProgress((Float) valueAnimator.getAnimatedValue()));
if (animationView.getProgress() == 0f) {
animator.start();
} else {
animationView.setProgress(0f);
}
}
}
}
And xml file of RecyclerView item:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/item_user_container"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:layout_width="#dimen/user_cv_width"
android:layout_height="#dimen/user_cv_height"
android:layout_margin="#dimen/dp4"
android:elevation="#dimen/dp4">
<RelativeLayout
android:id="#+id/item_user_main_relative_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout
android:id="#+id/item_user_top_relative_container"
android:layout_width="#dimen/user_rl_width"
android:layout_height="#dimen/user_rl_height">
<ImageView
android:id="#+id/user_img"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="centerCrop"
android:src="#drawable/default_avatar" />
<RelativeLayout
android:id="#+id/item_user_top_relative"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/user_item_bg"
android:orientation="vertical">
<TextView
android:id="#+id/user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/dp4"
android:textColor="#android:color/white"
android:textSize="#dimen/medium_text_size" />
<TextView
android:id="#+id/user_country"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
android:layout_marginStart="#dimen/dp4"
android:textSize="#dimen/medium_text_size" />
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:id="#+id/item_user_bottom_relative_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/dp12">
<ImageView
android:id="#+id/search_btn_like"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/heart_outline"
android:contentDescription="#string/search_btn_like_desc"/>
<com.airbnb.lottie.LottieAnimationView
android:id="#+id/lottieAnimationView"
android:visibility="gone"
android:layout_width="#dimen/lottie_animation_view_size"
android:layout_height="#dimen/lottie_animation_view_size"
app:lottie_loop="true"
app:lottie_autoPlay="true"
app:lottie_fileName="like.json"/>
</RelativeLayout>
<ImageView
android:id="#+id/search_btn_chat"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="#dimen/dp12"
android:layout_weight="1"
android:src="#drawable/message_outline" />
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
Override this two methods inside your adapter
#Override
public long getItemId(int position) {
return position;
}
#Override
public int getItemViewType(int position) {
return position;
}
I'm using Recyclerview in one of my activity, but I'm not able to show any data in recyclerView.
<?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="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="56dp"
android:background="#color/colorPrimary"
android:id="#+id/toolBar">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_back_light"
android:layout_gravity="center"
android:layout_marginStart="16dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Sushi"
android:textColor="#color/white"
android:textStyle="bold"
android:layout_gravity="center"
android:layout_marginStart="12dp"
android:textSize="18sp"/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/chatMessages"
android:clipToPadding="false"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:layout_below="#+id/toolBar"
android:layout_above="#+id/messageInputView"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:paddingStart="12dp"
android:paddingEnd="12dp"
android:id="#+id/messageInputView">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:paddingStart="12dp"
android:paddingEnd="12dp"
android:background="#D9D9D9"
android:id="#+id/border"
android:layout_alignParentTop="true"/>
<EditText
android:id="#+id/messageInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:background="#color/transparent"
android:layout_toLeftOf="#+id/messageSendButton"
android:hint="Type a message..."
android:textColorHint="#80282a2b"
android:inputType="textAutoCorrect|textAutoComplete|textMultiLine|textCapSentences"/>
<ImageView
android:id="#+id/messageSendButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_send"
android:onClick="onClick"
android:padding="4dp"
android:layout_marginTop="12dp"
android:layout_marginBottom="12dp"
android:background="#drawable/send_message_background"/>
</RelativeLayout>
</RelativeLayout>
All callback methods of RecyclerView.Adapter<RecyclerView.ViewHolder> not calling on calling notifydatasetchanged().
Adapter code here :
public class ChatMessagesAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static String TAG = "ChatMessagesAdapter";
private ArrayList<ChatMessages> mChatMessages;
private Context mContext;
public ChatMessagesAdapter(ArrayList<ChatMessages> chatMessages, Context context){
mContext = context.getApplicationContext();
mChatMessages = chatMessages;
Log.d("ChatActivity Adapter", String.valueOf(mChatMessages.size()));
setHasStableIds(true);
}
public synchronized void refreshData(ArrayList<ChatMessages> feeds){
Log.d("ChatActivity Adapter", String.valueOf(feeds.size()));
mChatMessages.clear();
mChatMessages.addAll(feeds);
Log.d("ChatActivity Adapter", String.valueOf(mChatMessages.size()));
notifyDataSetChanged();
}
class IncomingMessageHolder extends RecyclerView.ViewHolder {
#BindView(R.id.messageText) TextView mMessageText;
#BindView(R.id.messageTime) TextView mMessageTime;
IncomingMessageHolder(View itemView) {
super(itemView);
ButterKnife.bind(this,itemView);
}
}
class OutgoingMessageHolder extends RecyclerView.ViewHolder {
#BindView(R.id.messageText) TextView mMessageText;
#BindView(R.id.messageTime) TextView mMessageTime;
OutgoingMessageHolder(View itemView) {
super(itemView);
ButterKnife.bind(this,itemView);
}
}
#Override
public int getItemViewType(int position) {
Log.d("ChatActivity Adapter","getItemViewType "+mChatMessages.get(position).getMessageStatus());
if (mChatMessages.get(position).getMessageStatus() == 2){
return 1;
} else {
return 0;
}
}
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
Log.d("ChatActivity Adapter","onCreateViewHolder");
switch (viewType){
case 0 :
View outgoingMessageView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_outcoming_text_message,parent,false);
return new OutgoingMessageHolder(outgoingMessageView);
default :
View incomingMessageView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_incoming_text_message, parent, false);
return new IncomingMessageHolder(incomingMessageView);
}
}
#Override
public void onBindViewHolder(#NonNull final RecyclerView.ViewHolder holder, int position) {
Log.d("ChatActivity Adapter","onBindViewHolder");
switch (holder.getItemViewType()){
case 0:
OutgoingMessageHolder outgoingMessageHolder = (OutgoingMessageHolder) holder;
outgoingMessageHolder.mMessageText.setText(mChatMessages.get(position).getMessage());
outgoingMessageHolder.mMessageTime.setText(
UtilsFuntions.getDurationString(mChatMessages.get(position).getTimeStamp())
);
break;
case 1:
IncomingMessageHolder incomingMessageHolder = (IncomingMessageHolder) holder;
incomingMessageHolder.mMessageText.setText(mChatMessages.get(position).getMessage());
incomingMessageHolder.mMessageTime.setText(
UtilsFuntions.getDurationString(mChatMessages.get(position).getTimeStamp())
);
break;
}
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public int getItemCount() {
Log.d("ChatActivity Adapter", String.valueOf(mChatMessages.size()));
return mChatMessages.size();
}
}
Code for initalizing recyclerView:
mLayoutManager = new LinearLayoutManager(getApplicationContext());
mLayoutManager.setItemPrefetchEnabled(true);
mChatMessages.setItemViewCacheSize(0);
mChatMessages.setDrawingCacheEnabled(true);
mChatMessages.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
mChatMessages.setLayoutManager(mLayoutManager);
mChatMessages.setHasFixedSize(true);
mChatMessagesAdapter = new ChatMessagesAdapter(mChatItems,
getApplicationContext());
mChatMessages.addOnScrollListener(listerOnScroll);
mChatMessages.setAdapter(mChatMessagesAdapter);
ChatMessages chatMessage = new ChatMessages();
chatMessage.setMessage(mChatMessage.getText().toString().trim());
chatMessage.setMessageStatus(2);
chatMessage.setTimeStamp(System.currentTimeMillis());
mChatItems.add(chatMessage);
mChatMessagesAdapter.refreshData(mChatItems);
But when even I touch recyclerview or try to scroll recyclerview all callbacks called and recyclerview get populated. I don't know why this is happening. Please help me to solve this
I want to insert/show horizontal recyclerview into specific position (at this example position:1)
Vertical recyclerview - displays TextView + shape (rectange)
Horizontal recyclerview - displays ImageViews (next to each other)
.XML's
horizontal_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/horizontal_recyclerview"
android:layout_width="368dp"
android:layout_height="wrap_content"
android:layout_marginBottom="60dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="2dp"
android:padding="0dp"/>
<android.support.constraint.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:id="#+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.mkalejs.training.MainActivity">
vertical_layout.xml
<android.support.v7.widget.RecyclerView
android:id="#+id/vertical_recyclerview"
android:layout_width="368dp"
android:layout_height="wrap_content"
android:layout_marginBottom="60dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="2dp"
android:padding="0dp"/>
</android.support.constraint.ConstraintLayout>
vertical_layout_item.xml
<EditText
android:id="#+id/lbl_url_text"
android:layout_width="100dp"
android:layout_height="50dp"
android:background="#android:color/transparent"
android:inputType="textPersonName"
android:padding="5dp"
android:layout_gravity="center"
android:text="Name" />
<View
android:id="#+id/rectangle"
android:layout_width="195dp"
android:layout_height="100dp"
android:background="#drawable/rectangle" />
horizontal_layout_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp"
android:visibility="visible"
android:weightSum="1">
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/img_horizontal_slider"
android:layout_width="33dp"
android:layout_height="33dp"
android:adjustViewBounds="true"
android:visibility="visible" />
</LinearLayout>
There might be a small misunderstanding in recyclerview concept. I'm not sure where I can call horizontal_recyclerview" to not return it as a null (as I am now) and populate it with data. Is the XML structure wrong? Feel free to update code parts.
THIS SHOULD BE OKAY
MainActivity.class
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.vertical_layout);
ArrayList<PictureData> bundleOfData = prepareData();
RecyclerView rv = (RecyclerView) findViewById(R.id.vertical_recyclerview);
VerticalAdapter adapter = new VerticalAdapter(bundleOfData, this);
rv.setAdapter(adapter);
//rv.setNestedScrollingEnabled(true);
LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
rv.setLayoutManager(layoutManager);
}
VerticalAdapter.class
public class VerticalAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
private ArrayList<PictureData> pictures;
Context context;
public VerticalAdapter(ArrayList<PictureData> pictures, Context context)
{
this.pictures = pictures;
this.context = context;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView;
switch (viewType) {
case 1:
itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.horizontal_layout_item, parent, false);
return new HorizontalViewHolder(itemView);
default:
itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.vertical_layout_item, parent, false);
return new VerticalViewHolder(itemView);
}
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
switch (holder.getItemViewType()){
case 1:
HorizontalViewHolder hVh = (HorizontalViewHolder) holder;
HorizontalAdapter horizontal_adapter = new HorizontalAdapter(pictures, context);
hVh.horizontal_rec.setAdapter(horizontal_adapter);
break;
default:
VerticalViewHolder vVh = (VerticalViewHolder) holder;
vVh.textView.setText(pictures.get(position).getPictureUrl());
break;
}
}
#Override
public int getItemCount() {return pictures.size();}
#Override
public int getItemViewType(int position) {
if(position == 1){
return 1;
}else{
return 0;
}
}
}
HorizontalViewHolder.class
I GET A ERROR HERE. OBVIOUSLY, I DON'T HAVE (img_horizontal_slider) IN HORIZONTAL_LAYOUT, but putting ImageView + RecyclerView in one layout file and call here would feel wrong (?)
public class HorizontalViewHolder extends RecyclerView.ViewHolder{
public ImageView imagePositionInLayout;
public RecyclerView horizontal_rec;
public HorizontalViewHolder(View itemView) {
super(itemView);
LinearLayoutManager layoutManager = new LinearLayoutManager(itemView.getContext(), LinearLayoutManager.HORIZONTAL, false);
horizontal_rec = itemView.findViewById(R.id.vertical_recyclerview);
imagePositionInLayout = itemView.findViewById(R.id.img_horizontal_slider);
horizontal_rec.setLayoutManager(layoutManager);
}
public ImageView getView() {
return imagePositionInLayout;
}
}
HorizontalAdapter.class
ublic class HorizontalAdapter extends RecyclerView.Adapter<HorizontalViewHolder> {
private ArrayList<PictureData> pictures;
Context context;
public HorizontalAdapter(ArrayList<PictureData> pictures, Context context) {
this.pictures = pictures;
this.context = context;
}
#Override
public HorizontalViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.horizontal_layout_item, parent, false);
return new HorizontalViewHolder(view);
}
#Override
public void onBindViewHolder(HorizontalViewHolder holder, int position)
{
Picasso.Builder builder = new Picasso.Builder(context);
Picasso picasso = builder.build();
picasso
.load("http://nuclearpixel.com/content/icons/2010-02-09_stellar_icons_from_space_from_2005/earth_128.png")
.placeholder(R.mipmap.ic_launcher_round)
.into(holder.imagePositionInLayout);
}
#Override
public int getItemCount() {
return pictures.size();
}
}
I'm new to Android so I hope people will give some good explanation on what is wrong and how to fix it. Every suggestion will be gladly accepted. I will highly appreciate if someone will actually look into this and explain, because I feel pretty lost right now.
How do I archive a layout for my RecyclerView that looks like this:
I have tried creating it but it did ever look like in the giudlines.
This is taken form the Material Design Guidlines, but I could not find any xml Layouts, except Sketch and or PSDs.
Are there any ressources directl in xml?
Edit 1: I only need the single list item XML layout
Edit 2: I know how to use & implement a RecyclerView
create a .xml that have what you want inside example:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/gradient_bg"
android:orientation="horizontal"
android:layout_margin="1dp"
android:padding="1dip" >
<LinearLayout android:id="#+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="17dip"
android:layout_alignParentLeft="true"
android:layout_marginRight="2dip">
<ImageView
android:id="#+id/gasImagem"
android:contentDescription="cover"
android:layout_width="100dip"
android:layout_height="100dip"
/>
</LinearLayout>
<TextView
android:id="#+id/gasTitulo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/thumbnail"
android:layout_toRightOf="#+id/thumbnail"
android:textColor="#040404"
android:layout_marginTop="30dp"
android:typeface="sans"
android:textSize="20sp"
android:textStyle="bold"/>
<TextView
android:id="#+id/gasPreco"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="#000000"
android:textStyle="bold"
android:layout_marginTop="95dp"
android:layout_toRightOf="#+id/thumbnail"/>
<Button
android:id="#+id/btCarro"
android:layout_width="50dp"
android:layout_height="30dp"
android:background = "#drawable/roundedbutton"
android:layout_marginTop="95dp"
android:layout_marginLeft="310dp"
android:drawableTop="#drawable/shoppingcart"
android:textAlignment="center"
/>
</RelativeLayout>
After this create an adapter example
public class MyAdaptadorRecycler extends RecyclerView.Adapter<MyAdaptadorRecycler.ViewHolder> {
private List<Produto>gasList;
private LayoutInflater layout;
public MyAdaptadorRecycler(Context c,List<Produto>l){
gasList = l;
layout = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = layout.inflate(R.layout.list_row,parent,false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.ivcapa.setImageResource(gasList.get(position).getImagem());
holder.tvtitulo.setText(gasList.get(position).getNome());
holder.tvPreco.setText(String.valueOf(gasList.get(position).getPreco()) + "€");
final int posicao = position;
holder.bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Carrinho: ", Toast.LENGTH_SHORT).show();
}
});
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(view.getContext(), "Recycle Click", Toast.LENGTH_SHORT).show();
}
});
}
#Override
public int getItemCount() {
return gasList.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
protected TextView tvtitulo, tvPreco;
protected ImageView ivcapa;
protected Button bt;
public ViewHolder(View itemView) {
super(itemView);
this.tvtitulo = (TextView) itemView.findViewById(R.id.gasTitulo);
this.ivcapa = (ImageView) itemView.findViewById(R.id.gasImagem);
this.tvPreco = (TextView)itemView.findViewById(R.id.gasPreco);
this.bt = (Button)itemView.findViewById(R.id.btCarro);
}
}
}
Maybe you will need a divider exemple:
public class DividerItemDecoration extends RecyclerView.ItemDecoration {
private final int mVerticalSpaceHeight;
public DividerItemDecoration(int mVerticalSpaceHeight) {
this.mVerticalSpaceHeight = mVerticalSpaceHeight;
}
#Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
RecyclerView.State state) {
outRect.bottom = mVerticalSpaceHeight;
//outRect.left = mVerticalSpaceHeight;
//outRect.right = mVerticalSpaceHeight;
}
}
Then in your mainActivity you need to do this:
LinearLayoutManager llm = new LinearLayoutManager(this);
this.rv.setLayoutManager(llm);
rv.addItemDecoration(new DividerItemDecoration(20));
rv.setHasFixedSize(true);
nr= 1;
this.listaPordutos = new ArrayList<Produto>();
this.adapatadorLivros = new MyAdaptadorRecycler(this, listaPordutos);
rv.setAdapter(this.adapatadorLivros);
This is just my exemples, that I use to create a program
hope this can help you, any doubt just say :)
Refer this official documentation link on how to use RecyclerView.LayoutManager http://developer.android.com/training/material/lists-cards.html