Android RecyclerView list item doesn't show click animation - android

As recommended I replaced my ListView by RecyclerView. But now I can't get working the item click animation. Long press animation is working but if I only tap the item I get no tap animation.
I tried different solutions found around here but nothing helped. What is wrong with my code?
The layout that contains the RecyclerView:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/frame_main" tools:context=".MainActivity">
<android.support.v7.widget.RecyclerView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/recycler_view" />
</RelativeLayout>
This is the list item layout: (I also tried FrameLayout)
<?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="72dp"
android:clickable="true"
android:focusable="true"
android:background="?android:attr/selectableItemBackground">
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/cover"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00:00"
android:id="#+id/time"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="false"
android:layout_marginRight="16dp"
android:layout_marginTop="20dp"
style="#style/Widget.AppCompat.ListView"
android:singleLine="true" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="72dp"
android:layout_marginRight="56dp"
android:id="#+id/linearLayout"
android:gravity="center_vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Title"
android:id="#+id/title"
android:textSize="16dp"
style="#style/Widget.AppCompat.ListView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:singleLine="true" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Artist"
android:id="#+id/artist"
android:textSize="14dp"
style="#style/Widget.AppCompat.ListView"
android:layout_below="#+id/title"
android:layout_alignLeft="#+id/title"
android:layout_alignStart="#+id/title"
android:singleLine="true" />
</LinearLayout>
</RelativeLayout>
Code in my activity's onCreate():
recyclerview = (RecyclerView) findViewById(R.id.recycler_view);
mLayoutManager = new LinearLayoutManager(this);
recyclerview.setLayoutManager(mLayoutManager);
recyclerview.setHasFixedSize(true);
recyclerview.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
// enable swipelayout if scrolled to top
try {
int firstPos = mLayoutManager.findFirstCompletelyVisibleItemPosition();
if (firstPos > 0) {
swipeLayout.setEnabled(false);
} else {
swipeLayout.setEnabled(true);
if (recyclerview.getScrollState() == 1 && swipeLayout.isRefreshing()) {
recyclerview.stopScroll();
}
}
} catch (Exception e) {
}
}
});
recyclerview.addOnItemTouchListener(
new RecyclerItemClickListener(MainActivity.this, new RecyclerItemClickListener.OnItemClickListener() {
#Override
public void onItemClick(View view, int position) {
// do something works
}
})
);

I have read through your question, To achieve tap animation you only need to replace the code below found in your list items.
from
android:background="?android:attr/selectableItemBackground"
to
android:foreground="?android:attr/selectableItemBackground"
Hope it helps solve your problem

Related

how to maintain Visibllity of recyclerview item in adapter class

I have a layout like bellow in my Adapter:
<?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"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF">
<android.support.v7.widget.CardView
android:id="#+id/cardVisibleLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/txtCategoryNameTop"
card_view:cardElevation="2dp">
<LinearLayout
android:id="#+id/lnVisibleLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<com.joanzapata.iconify.widget.IconTextView
android:id="#+id/itxtArrow"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#null"
android:gravity="center"
android:text="#string/fa_chevron_down"
android:textColor="#color/orangePeel" />
<TextView
android:id="#+id/txtConnectToTeacher"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1.3"
android:gravity="center"
android:text="#string/contact_teacher" />
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="right"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:layout_weight="3.7"
android:gravity="right"
android:orientation="vertical">
<TextView
android:id="#+id/txtCourseName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp" />
<TextView
android:id="#+id/txtTeacherName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/txtCourseName"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp" />
</RelativeLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
<RelativeLayout
android:id="#+id/rlInvisibleLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/cardVisibleLayout"
android:animateLayoutChanges="true"
android:background="#E0E0E0"
android:visibility="gone">
<RelativeLayout
android:id="#+id/rlLessonLink"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="15dp">
<ImageView
android:id="#+id/imgLessonLink"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
app:srcCompat="#drawable/ic_circle" />
<TextView
android:id="#+id/txtLessonLink"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/imgLessonLink"
android:text="#string/lesson_link"
android:textColor="#color/black" />
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
I am visible/gone RelativeLayout with this id rlInvisibleLayout but when I scroll the RecyclerView I lost it.
Like bellow:
#Override
public void onBindViewHolder(#NonNull final ViewHolder viewHolder, int position) {
.....
viewHolder.cardVisibleLayout.setOnClickListener(view -> {
if (viewHolder.rlInvisibleLayout.getVisibility() == View.VISIBLE) {
viewHolder.rlInvisibleLayout.setVisibility(View.GONE);
viewHolder.iconTextView.setText(R.string.fa_chevron_down);
} else {
viewHolder.rlInvisibleLayout.setVisibility(View.VISIBLE);
viewHolder.iconTextView.setText(R.string.fa_chevron_up);
}
});
....
}
Try this
add a boolean flag in your POJO / DataModelClass like this
public class Pojo
{
boolean isOpen;
public boolean isOpen() {
return isOpen;
}
public void setOpen(boolean open) {
isOpen = open;
}
}
Than you have to main that boolean flag whenever user click cardVisibleLayout
SAMPLE CODE
#Override
public void onBindViewHolder(#NonNull ViewHolder viewHolder, int position) {
// check here the flag and maintain visibility of item based on flag
if (arrayList.get(position).isOpen()){
viewHolder.rlInvisibleLayout.setVisibility(View.VISIBLE);
}else {
viewHolder.rlInvisibleLayout.setVisibility(View.GONE);
}
viewHolder.cardVisibleLayout.setOnClickListener(view -> {
if (viewHolder.rlInvisibleLayout.getVisibility() == View.VISIBLE) {
viewHolder.rlInvisibleLayout.setVisibility(View.GONE);
viewHolder.iconTextView.setText(R.string.fa_chevron_down);
arrayList.get(position).setOpen(false);// set flag false when you hide the item
} else {
arrayList.get(position).setOpen(true);// set flag true when you show the item
viewHolder.rlInvisibleLayout.setVisibility(View.VISIBLE);
viewHolder.iconTextView.setText(R.string.fa_chevron_up);
}
});

Horizontal recycler view on top of vertical recycler veiw

This is the individual item's layout file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#DDE6EC"
tools:context="com.example.personal.newsfeeder.MainActivity">
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/section_recycler_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/tools"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginTop="16dp"
android:id="#+id/card_view"
android:layout_gravity="center"
card_view:cardCornerRadius="8dp"
card_view:cardElevation="10dp"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/avatar_section"
android:layout_width="match_parent"
android:layout_height="72dp"
android:padding="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:background="#drawable/circular_image"
android:id="#+id/avatar_image"
/>
<TextView
android:id="#+id/avatar_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/avatar_image"
android:layout_marginLeft="4dp"
tools:text="Title"
android:textStyle="bold"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="#616161"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/avatar_name"
android:layout_toRightOf="#id/avatar_image"
android:layout_marginLeft="4dp"
tools:text="Subhead"
android:id="#+id/avatar_subhead"
android:textAppearance="?android:textAppearanceSmall"
android:textColor="#616161"/>
</RelativeLayout>
<ImageView
android:layout_height="wrap_content"
android:layout_width="match_parent"
tools:src="#drawable/placeholder"
android:id="#+id/image_view"
android:scaleType="centerCrop"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="16dp"
android:text="Keep Moving"
android:paddingLeft="16dp"
android:textAppearance="?android:textAppearanceMedium"
android:textAllCaps="true"
android:textStyle="bold"
android:id="#+id/the_title"
/>f
<TextView
android:id="#+id/the_three_lines"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="3"
android:padding="16dp"
tools:text="#string/description"
android:ellipsize="end"
android:lineSpacingMultiplier="1.2"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ImageView
android:id="#+id/heart_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/heart"
android:padding="8dp"
/>
<ImageView
android:id="#+id/bookmark_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/bookmark"
android:padding="8dp"
/>
</LinearLayout>
</LinearLayout>
enter code here
</android.support.v7.widget.CardView>
</LinearLayout>
This is the recycler's Adapter code
#Override
public void onBindViewHolder(ArticleViewHolder holder, int position) {
holder.mAvatarView.setText(mArticles.get(position).getmAvatarInitial());
holder.mAvatarNameView.setText(mArticles.get(position).getmAvatarName());
holder.mAvatarSubView.setText(mArticles.get(position).getmAvatarSub());
mRowIndex++;
if (position == 0) {
holder.horizontalList.setLayoutManager(new LinearLayoutManager(holder.context, LinearLayoutManager.HORIZONTAL, false));
holder.horizontalAdapter = new SectionRVAdapter(holder.context);
holder.horizontalList.setAdapter(holder.horizontalAdapter);
holder.horizontalAdapter.setDataset(holder.sections);
}
String imageUrl = mArticles.get(position).getmImageURL();
Picasso.with(mContext)
.load(imageUrl)
.placeholder(R.drawable.placeholder)
.error(R.drawable.error_img)
.into(holder.mImageView);
holder.mTheTitleView.setText(mArticles.get(position).getmTheTitle());
holder.mTheThreeLinesView.setText(mArticles.get(position).getmTheThreeLines());
}
Now i want a horizontal recycler view over the vertical recycler view. But at present the horizontal recycler view is repeated after every 5 cardviews.
I am checking if position is equal to 0 in my onBindViewHolder method but still why is it created multiple times?
I hope i made my question clear.
holder.setIsRecyclable(false);
Add this line in public void onBindViewHolder(ArticleViewHolder holder, int position) {}
I hope this will help you

RecyclerView not loading properly

I'm trying to create a chat app which should load chats with recyclerview and an edittext below the recyclerview and an imagebutton to the right of the edittext.
I have a relativelayout within which I have a recyclerview and another relative layout. Within this 2nd relativelayout, I have an edittext and an imagebutton. However the recyclerview is not loading properly.
I want the recyclerview to load till the start of the edittext without any white space in between.
layout.xml file
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_im"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.abdralabs.talksee.IMActivity"
android:weightSum="10">
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_im"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:layout_weight="8.5"/>
<RelativeLayout
android:id="#+id/rl_im"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="12dp"
android:orientation="vertical"
android:layout_weight="1.5">
<EditText
android:id="#+id/et_im"
android:layout_width="290dp"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter message..."
android:inputType="textPersonName"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ImageButton
android:id="#+id/ib_im"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#android:drawable/ic_menu_send"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
</RelativeLayout>
activity.java file
public class IMActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private ImAdapter imAdapter;
private List<ChatMsg> chatMsgList = new ArrayList<>();
EditText etIp;
Button setIp;
Button setOn;
EditText messageInput;
Button sendButton;
ServiceConnection serviceConnection;
TalkSeeService.TalkSeeBinder talkSeeBinder;
String otherUserName;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
bindService(new Intent(IMActivity.this, TalkSeeService.class), new ServiceConnection() {
#Override
public void onServiceConnected(ComponentName name, IBinder service) {
talkSeeBinder = (TalkSeeService.TalkSeeBinder)service;
}
#Override
public void onServiceDisconnected(ComponentName name) {
talkSeeBinder = null;
}
},BIND_AUTO_CREATE);
setContentView(R.layout.activity_im);
Intent intent = getIntent();
otherUserName = intent.getStringExtra("otherUserName");
recyclerView = (RecyclerView)findViewById(R.id.rv_im);
imAdapter = new ImAdapter(chatMsgList);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
recyclerView.setAdapter(imAdapter);
prepareChatData();
}
private void prepareChatData() {
ChatMsg chatMsg = new ChatMsg(otherUserName);
chatMsgList.add(chatMsg);
imAdapter.notifyDataSetChanged();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_swipe, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId())
{
case R.id.action_settings:
break;
case R.id.log_out:
Intent i2 = new Intent(this,LauncherActivity.class);
i2.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
LoggedInSharedPreference.clearUserName(getApplicationContext());
startActivity(i2);
break;
case R.id.recent_conversations:
Intent rcIntent = new Intent(this,HistoryActivity.class);
startActivity(rcIntent);
break;
default:
Toast.makeText(this,"Somthing went wrong. Please try again!",Toast.LENGTH_SHORT).show();
break;
}
return true;
}
}
Please help me. Thanks.
Edit your XML file like this:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_im"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.abdralabs.talksee.IMActivity"
android:weightSum="10">
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_im"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:layout_weight="8.5"/>
<RelativeLayout
android:id="#+id/rl_im"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="12dp"
android:orientation="vertical"
android:layout_weight="1.5">
<EditText
android:id="#+id/et_im"
android:layout_width="290dp"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter message..."
android:inputType="textPersonName"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ImageButton
android:id="#+id/ib_im"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#android:drawable/ic_menu_send"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
</RelativeLayout>
You should use LinearLayout when using layout_weight attribute :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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/activity_im"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.abdralabs.talksee.IMActivity"
android:weightSum="10">
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_im"
android:layout_width="match_parent"
android:layout_height="0dp"
android:scrollbars="vertical"
android:layout_weight="8.5"/>
<RelativeLayout
android:id="#+id/rl_im"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="12dp"
android:orientation="vertical"
android:layout_weight="1.5">
<EditText
android:id="#+id/et_im"
android:layout_width="290dp"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter message..."
android:inputType="textPersonName"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ImageButton
android:id="#+id/ib_im"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#android:drawable/ic_menu_send"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
</LinearLayout>
Try this:
<?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"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.abdralabs.talksee.IMActivity"
android:id="#+id/activity_im"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_im"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/rl_im"/>
<RelativeLayout
android:id="#+id/rl_im"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="12dp">
<EditText
android:id="#+id/et_im"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter message..."
android:inputType="textPersonName"
android:layout_toLeftOf="#+id/ib_im"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ImageButton
android:id="#+id/ib_im"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#android:drawable/ic_menu_send"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
</RelativeLayout>
I think the problem is you are changing the data of chatMsgList , which is the local variable.
When you call notifyDataSetChanged() , it wont work .
To make it work , create a new function with any name and call it in prepareChatData() like this
private void prepareChatData() {
ChatMsg chatMsg = new ChatMsg(otherUserName);
chatMsgList.add(chatMsg);
imAdapter.swapData(chatMsgList);
}
Inside your Adapter implement this method
public void swapData(Cursor chatMsgList) {
myChatlistArray = chatMsgList;
notifyDataSetChanged();
}

RecyclerView won't Scroll

I made a fragment which contains 3 RecyclerView, 1 which is a horizontal RV works, but the other 2 which is vertical doesn't work. I couldn't scroll through it.
(image link: [http://s15.postimg.org/lc6bxz9ej/so1.png][2])
HomeFragment.java #AfterViews
LinearLayoutManager catLM = new LinearLayoutManager(mContext);
mRVcatList.setLayoutManager(catLM);
mRVcatList.setAdapter(new CategoryAdapter(initCategories()));
LinearLayoutManager hotLM = new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false);
mRVhotList.setLayoutManager(hotLM);
mRVhotList.setAdapter(new HotItemsAdapter(initHotItems()));
LinearLayoutManager brandLM = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
mRVbrandList.setLayoutManager(brandLM);
mRVbrandList.setAdapter(new BrandAdapter(initBrands()));
mRVbrandList.addOnItemTouchListener(new HotItemClickListener(mContext, new HotItemClickListener.OnItemClickListener() {
#Override
public void onItemClick(View view, int position) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.mFLframe, new ProductListFragment_());
transaction.addToBackStack(null);
transaction.commit();
}
}));
mRVcatList.addOnItemTouchListener(new HotItemClickListener(mContext, new HotItemClickListener.OnItemClickListener() {
#Override
public void onItemClick(View view, int position) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.mFLframe, new CategoryFragment_());
transaction.addToBackStack(null);
((MainActivity) mContext).hideLogo();
transaction.commit();
}
}));
mRVhotList.addOnItemTouchListener(new HotItemClickListener(mContext, new HotItemClickListener.OnItemClickListener() {
#Override
public void onItemClick(View view, int position) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.mFLframe, new DetailFragment_());
transaction.addToBackStack(null);
((MainActivity) mContext).hideLogo();
transaction.commit();
}
}));
And here is my XML file,
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<!--//HOT-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/mRLtopHot">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hot Items"
android:textColor="#color/ColorPrimaryDark"
android:paddingLeft="5dp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/mTVhot"
android:textSize="#dimen/bebas_medium" />
<View
android:layout_width="wrap_content"
android:layout_height="2dip"
android:background="#00B7F6"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/mRVhotList"
android:layout_width="match_parent"
android:layout_height="140dp"
android:padding="5dp"
android:background="#FFFFFF"
android:layout_below="#+id/mRLtopHot"/>
<!--CAT-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#FFFFFF"
android:layout_marginTop="10dp"
android:id="#+id/mRLtopCat"
android:layout_below="#+id/mRVhotList"
android:layout_centerHorizontal="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Categories"
android:paddingLeft="5dp"
android:textColor="#color/ColorPrimaryDark"
android:id="#+id/mTVcat"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:clickable="false"
android:textSize="#dimen/bebas_medium" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/mIVcatCollapse"
android:paddingRight="5dp"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<View
android:id="#+id/view1"
android:layout_width="wrap_content"
android:layout_height="2dip"
android:background="#00B7F6"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/mRVcatList"
android:layout_width="match_parent"
android:layout_height="140dp"
android:background="#FFFFFF"
android:layout_below="#+id/mRLtopCat"/>
<!--BRAND-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="10dp"
android:id="#+id/mRLtopBrand"
android:background="#FFFFFF"
android:layout_below="#+id/mRVcatList"
android:layout_centerHorizontal="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Brands"
android:paddingLeft="5dp"
android:textSize="#dimen/bebas_medium"
android:textColor="#color/ColorPrimaryDark"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/mTVbrand" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="5dp"
android:id="#+id/mIVbrandCollapse"
android:background="#drawable/abc_ic_menu_moreoverflow_mtrl_alpha"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<View
android:layout_width="wrap_content"
android:layout_height="2dip"
android:background="#00B7F6"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/mRVbrandList"
android:layout_width="match_parent"
android:layout_height="140dp"
android:background="#FFFFFF"
android:layout_below="#+id/mRLtopBrand"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
Previously, I made the "Categories" and "Brand" tab closed and could be expanded when clicked (it's just a simple usage of setVisibility), and the RecyclerView(s) did scroll! But I don't use that UI anymore. Unfortunately, this problem arises.
How could I make the vertical RecyclerView works? Thanks!
You can't have a list style layout inside a ScrollView (The ScrollView will destroy the listview/gridview/recyclerview scroll function)
You must get the height of the RecyclerView to make it fixed. Here is an example: Android list view inside a scroll view
Or, you can use this library but it use a ListView: https://github.com/PaoloRotolo/ExpandableHeightListView (maybe will work with RecyclerView)

Android SwipeRefreshLayout how to implement canChildScrollUp if child is not a ListView or ScrollView

Hi i have a SwipeRefreshLayout that contains a relative layout which contains a scroll view and another view currently when i scroll down the list sometimes wont scroll back up instead it runs the onRefresh function. How do i prevent it from running this unless the view is at the top? i had read about canChildScrollUp but not found a way to implement it correctly.
heres my layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/swipe_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/profile"
android:background="#drawable/homepage_image_1">
<com.alldaypa.alldaypa.customviews.CircularImageView
android:layout_width="75dp"
android:layout_height="75dp"
app:border_color="#FFFFFF"
app:border_width="8dp"
app:shadow="false"
android:id="#+id/profile_image"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="70dp"
android:layout_marginTop="20dp"
android:src="#drawable/homepage_noimage_male"
android:clickable="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/profile_image"
android:layout_alignRight="#+id/profile_image"
android:src="#drawable/homepage_profile_add"
android:layout_marginRight="3dp"
android:layout_marginBottom="3dp"
/>
<TextView android:id="#+id/profile_name"
android:text="Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_alignTop="#+id/profile_image"
android:layout_toRightOf="#+id/profile_image"
android:layout_marginTop="20dp"
android:textColor="#color/darkgrey"
android:layout_marginLeft="10dp"/>
<TextView android:id="#+id/company"
android:text="Company"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:layout_below="#+id/profile_name"
android:layout_alignLeft="#+id/profile_name"
android:textColor="#color/darkgrey"
android:layout_marginTop="-2dp"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/availability"
android:background="#color/white"
android:padding="20dp"
android:layout_below="#+id/profile">
<TextView android:id="#+id/profile_status_title"
android:text="Your current availability status:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textColor="#color/darkgrey"/>
<TextView android:id="#+id/profile_status"
android:text="Available"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="32sp"
android:layout_below="#+id/profile_status_title"
android:layout_centerHorizontal="true"
android:textColor="#color/green"
android:layout_marginTop="-2dp"/>
<TextView android:id="#+id/profile_status_reason"
android:text="On: 01924 501370"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:layout_below="#+id/profile_status"
android:layout_centerHorizontal="true"
android:textColor="#color/pink"/>
</RelativeLayout>
<TextView
android:id="#+id/messages"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:layout_below="#+id/availability"
android:ellipsize="end"
android:lines="1"
android:background="#color/pink"
android:textColor="#color/white"
android:textSize="14sp"
android:paddingLeft="10dp"
android:paddingTop="5dp"
android:gravity="left"
android:text="Messages" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/messages"
android:background="#color/midgrey">
<com.alldaypa.alldaypa.customviews.SwipeListView
xmlns:swipe="http://schemas.android.com/apk/res-auto"
android:id="#+id/list"
android:listSelector="#00000000"
android:layout_width="fill_parent"
android:layout_height="400dp"
swipe:swipeFrontView="#+id/front"
swipe:swipeBackView="#+id/back"
swipe:swipeActionLeft="reveal"
swipe:swipeMode="left"
swipe:swipeCloseAllItemsWhenMoveList="true"
swipe:swipeOpenOnLongPress="false"
swipe:swipeAnimationTime="0"
swipe:swipeOffsetLeft="200dp"
swipe:swipeOffsetRight="0dp"
android:focusable="false"
/>
<ImageView android:layout_height="1dp"
android:layout_width="match_parent"
android:background="#color/searchbg"
android:layout_below="#+id/list"
android:layout_alignLeft="#+id/list"
android:id="#+id/line"
/>
<Button
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#drawable/listitem"
android:textColor="#color/darkgrey"
android:textSize="18sp"
android:text="View all messages"
android:layout_below="#+id/line"
android:gravity="center"
android:layout_centerVertical="true"
android:id="#+id/viewall"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
</RelativeLayout>
</ScrollView>
<include layout="#layout/elv_undo_popup" android:id="#+id/undo" android:visibility="gone"/>
</RelativeLayout>
</android.support.v4.widget.SwipeRefreshLayout>
and heres where i have implemented my class
swipeLayout = (SwipeRefreshLayout)v.findViewById(R.id.swipe_container);
swipeLayout.setColorSchemeColors(Color.parseColor("#ed037c"), Color.parseColor("#c40053"),Color.parseColor("#ffffff"), Color.parseColor("#51af50"));
swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override public void onRefresh() {
swipeLayout.setRefreshing(true);
Log.d("Swipe", "Refreshing Number");
( new Handler()).postDelayed(new Runnable() {
#Override public void run() {
updateLocalData();
}
}, 3000);
}
});
thanks to #Twibit i found a way to make this work. the trick is to detect the position of the scrollview if the position is 0 make set the SwipeRefreshLayout to be enabled else disabled.
scrollView.getViewTreeObserver().addOnScrollChangedListener(new OnScrollChangedListener() {
#Override
public void onScrollChanged() {
int scrollY = scrollView.getScrollY();
if(scrollY == 0) swipeLayout.setEnabled(true);
else swipeLayout.setEnabled(false);
}
});
You might have to enable / disable your swipe layout if your scroll view is or isn't on top.
You can look at this link for inspiration : https://gist.github.com/Frikish/10025057
Another example that might help you : https://developer.android.com/samples/SwipeRefreshMultipleViews/src/com.example.android.swiperefreshmultipleviews/MultiSwipeRefreshLayout.html
I had another problem with horizontal scroll : HorizontalScrollView inside SwipeRefreshLayout
Extend SwipeRefreshLayout and overwrite the method canChildScrollUp() which determines whether your views can scroll up.
//if you have several scrollable views,just iterate
for (View view : ...) {
if (view != null && view.isShown() && !canViewScrollUp(view)) {
// If the view is shown, and can not scroll upwards, return false and start the
// gesture.
return false;
}
return true;
Iterate through your views and use the method canViewScrollUp() to check if any of them can not scroll up
private static boolean canViewScrollUp(View view) {
if (android.os.Build.VERSION.SDK_INT >= 14) {
// For ICS and above we can call canScrollVertically() to determine this
return ViewCompat.canScrollVertically(view, -1);
} else {
if (view instanceof AbsListView) {
// Pre-ICS we need to manually check the first visible item and the child view's top
// value
final AbsListView listView = (AbsListView) view;
return listView.getChildCount() > 0 &&
(listView.getFirstVisiblePosition() > 0
|| listView.getChildAt(0).getTop() < listView.getPaddingTop());
} else {
// For all other view types we just check the getScrollY() value
return view.getScrollY() > 0;
}
}

Categories

Resources