This is my layout:
<?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" >
<LinearLayout
android:id="#+id/featuredContent"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:background="#ccc"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="#+id/panelpager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
I already implements AbsListView.OnScrollListener to my ListView and I can have the Y of first item, but how can I hide the featuredContent when scrolling to bottom so I can see ListView in my whole screen?
listNews.setOnScrollListener(new AbsListView.OnScrollListener() {
#Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
View firstItem = listNews.getChildAt(0);
int top = (firstItem == null) ? 0 : firstItem.getTop();
}
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
});
LinearLayout ll = (LinearLayout)findViewById(R.id.featuredContent);
if (firstVisibleItem == 0)
{
ll.setVisibility(View.VISIBLE);
}
else
{
ll.setVisibility(View.GONE);
}
View.Gone will consider that this view never exist. therefore, there will not be a space in its place. unlike View.INVISIBLE that while reserve position empty.
Related
How can I monitor RecyclerView's scroll whether it is scrolling up or down?
Is the below code a correct way to detect scrolling?
MessageRecyclerView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return false;
}
});
With the OnScrollListener.
Usage:
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrollStateChanged(final RecyclerView recyclerView, final int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
#Override
public void onScrolled(final RecyclerView recyclerView, final int dx, final int dy) {
super.onScrolled(recyclerView, dx, dy);
}
});
Use OnScrollListener for this:
mMessageRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(final RecyclerView recyclerView, final int dx, final int dy) {
if (dy > 0) {
//scroll up
} else {
//scroll down
}
}
});
if you are using Recyclerview inside scrollview recyclerview scroll will not be smooth sometime becauseof recyclerview have default scroll. Instead of scroll use NestedScrollView.
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/app_back">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/toprel"
android:layout_width="match_parent"
android:layout_height="140dp">
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"/>
<com.viewpagerindicator.CirclePageIndicator
android:id="#+id/indicator"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:gravity="bottom"
android:padding="10dip"
app:centered="true"
app:fillColor="#color/text_black_light"
app:pageColor="#ffffff"/>
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/homecat_rec"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|top"
android:layout_marginTop="#dimen/margin_4"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
and use yourrecyclerview.setNestedScrollingEnabled(false); in Activity class.
To display data in table layout with the first column frozen, the others can scroll horizontally, I have used 2 gridviews as below XML file content:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.gridviews.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<GridView
android:id="#+id/gridViewID"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:columnWidth="50dp"
android:horizontalSpacing="1dp"
android:numColumns="1"
android:paddingTop="5dp"
android:stretchMode="columnWidth"
android:verticalSpacing="1dp" />
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<GridView
android:id="#+id/gridViewDays"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnWidth="100dp"
android:horizontalSpacing="1dp"
android:numColumns="5"
android:paddingTop="5dp"
android:scrollbarStyle="outsideOverlay"
android:stretchMode="columnWidth"
android:verticalSpacing="1dp" />
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
</RelativeLayout>
and my code:
...
BaseAdapter adapterId = new BaseAdapter() {
#Override
public int getCount() {
return dataList.size();
}
#Override
public Object getItem(int position) {
return dataList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView textView = new TextView(context);
textView.setText(String.valueOf(dataList.get(position).Id));
textView.setLayoutParams(new GridView.LayoutParams(GridView.AUTO_FIT, 70));
return textView;
}
};
BaseAdapter adapterDays = new BaseAdapter() {
#Override
public int getCount() {
return dataList.size() * 5;
}
#Override
public Object getItem(int position) {
return dataList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView textView = new TextView(context);
int mod = position % 5;
int idx = position / 5;
switch (mod) {
case 0:
textView.setText(String.valueOf(dataList.get(idx).Mon));
break;
case 1:
textView.setText(String.valueOf(dataList.get(idx).Tue));
break;
case 2:
textView.setText(String.valueOf(dataList.get(idx).Wed));
break;
case 3:
textView.setText(String.valueOf(dataList.get(idx).Thu));
break;
case 4:
textView.setText(String.valueOf(dataList.get(idx).Fri));
break;
}
textView.setLayoutParams(new GridView.LayoutParams(GridView.AUTO_FIT, 70));
return textView;
}
};
gridViewID.setAdapter(adapterId);
gridViewDays.setAdapter(adapterDays);
// Horizontal scrolling of gridViewDays
LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) gridViewDays.getLayoutParams();
linearParams.width = 300;
gridViewDays.setLayoutParams(linearParams);
// Vertical scrolling...
gridViewID.setOnScrollListener(new AbsListView.OnScrollListener() {
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
int firstVisibleItem = view.getFirstVisiblePosition();
gridViewID.setSelection(firstVisibleItem);
gridViewDays.setSelection(firstVisibleItem * 5);
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
}
});
...
With these code, of course when I move my hand out of the screen, the gridViewDays gridview will have the same selection with the gridViewID as the image below:
However, when I keep my hand touch on the screen to scroll the gridViewID, the gridViewDays will look like the image below (it does not scroll while gridViewID is scrolling)
So, how can I make the gridViewDays gridview vertically scrolling the same as the gridViewID? I have also tried some methods such as smoothScrollToPosition, smoothScrollByOffset, smoothScrollToPositionFromTop... inside onScroll but they do not work
Update: although the answers (#vrundpurohit's and mine) below are working, however, I have found that the app has poor performance with big data (I mean when the data list has too many items).
use NonScrollGridView. and wrap both in single ScrollView
Here is code for NonScrollGridView..
public class NonScrollGridView extends GridView {
public NonScrollGridView(Context context) {
super(context);
}
public NonScrollGridView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public NonScrollGridView(Context context, AttributeSet attrs,
int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
}
}
EDIT:
Here is how you can do this.
<ScrollView android:id="#+id/scroll" ... >
<NonScrollGridView />
<RelativeLayout android:id="#+id/head1" ... >
<!-- header for 1st GridView -->
</RelativeLayout>
<HorizontalScrollView ... >
<HorizontalScrollView ... >
<NonScrollGridView />
<RelativeLayout android:id="#+id/head2" ... >
<!-- header for 1st GridView -->
</RelativeLayout>
</HorizontalScrollView>
</HorizontalScrollView>
</ScrollView>
Now for making both headers sticky.. do following.
scroll.getViewTreeObserver().addOnScrollChangedListener(
new ViewTreeObserver.OnScrollChangedListener() {
public void onScrollChanged() {
ViewCompat.setTranslationY(head1, who.getScrollY());
ViewCompat.setTranslationY(head2, who.getScrollY());
}
});
Happy Coding..
Thanks to #vrundpurohit comment, I have searched more in S.O and found
#dennisdrew's answer at
How to put GridView inside ScrollView
From there, I use the ExpandableHeightGridView class of #tacone at
Gridview height gets cut
to solve my issue.
My modified code:
...
final ExpandableHeightGridView gridViewID = (ExpandableHeightGridView) findViewById(R.id.gridViewID);
final ExpandableHeightGridView gridViewDays = (ExpandableHeightGridView) findViewById(R.id.gridViewDays);
gridViewID.setExpanded(true);
gridViewDays.setExpanded(true);
...
Layout 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="match_parent"
tools:context="com.example.gridviews.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<com.example.gridviews.ExpandableHeightGridView
android:id="#+id/gridViewID"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:columnWidth="50dp"
android:horizontalSpacing="1dp"
android:numColumns="1"
android:stretchMode="columnWidth"
android:verticalSpacing="1dp" />
<HorizontalScrollView
android:id="#+id/horizontalScrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<com.example.gridviews.ExpandableHeightGridView
android:id="#+id/gridViewDays"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnWidth="100dp"
android:horizontalSpacing="1dp"
android:numColumns="5"
android:scrollbarStyle="outsideOverlay"
android:stretchMode="columnWidth"
android:verticalSpacing="1dp" />
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
</ScrollView>
Thank you all!
I have a CustomListView where I'm showing some text and getting an image displayed using Picasso's library. I created a xml file inside the drawable folder and for drawable-21 folder for the Ripple Effect but for some reason the effect is not going above the ImageView.
Here's my files:
drawable:
listview_item_background.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid
android:color="#ffdadada"/>
</shape>
</item>
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid
android:color="#ffffff"/>
</shape>
</item>
</selector>
drawable-v21:
listview_item_background.xml
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#ffdadada">
<item android:drawable="#android:color/white"/>
</ripple>
activity_main:
<?xml version="1.0"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="150dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:expandedTitleMarginStart="20dp"
app:expandedTitleMarginEnd="20dp"
app:contentScrim="#color/colorPrimary"
android:background="#color/colorPrimary">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:gravity="start|bottom"
app:layout_collapseMode="parallax"
android:background="#color/colorPrimary"
android:orientation="vertical">
...
</LinearLayout>
<android.support.v7.widget.Toolbar
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:layout_collapseMode="pin"
app:theme="#style/ToolbarTheme"
android:background="#android:color/transparent"
android:id="#+id/main_toolbar">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:id="#+id/toolbar_title"
android:textSize="20sp"
android:textColor="#ffffff"/>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:id="#+id/scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:background="#eeeeee">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:ignore="UselessParent">
<packagename.NestedListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:divider="#eeeeee"
android:drawSelectorOnTop="true"/>
</LinearLayout>
</FrameLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
NestedListView:
public class NestedListView extends ListView implements View.OnTouchListener, AbsListView.OnScrollListener {
private int listViewTouchAction;
private static final int MAXIMUM_LIST_ITEMS_VIEWABLE = 99;
public NestedListView(Context context, AttributeSet attrs) {
super(context, attrs);
listViewTouchAction = -1;
setOnScrollListener(this);
setOnTouchListener(this);
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if (getAdapter() != null && getAdapter().getCount() > MAXIMUM_LIST_ITEMS_VIEWABLE) {
if (listViewTouchAction == MotionEvent.ACTION_MOVE) {
scrollBy(0, -1);
}
}
}
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
#SuppressLint("DrawAllocation")
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int newHeight = 0;
final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
if (heightMode != MeasureSpec.EXACTLY) {
ListAdapter listAdapter = getAdapter();
if (listAdapter != null && !listAdapter.isEmpty()) {
int listPosition;
for (listPosition = 0; listPosition < listAdapter.getCount()
&& listPosition < MAXIMUM_LIST_ITEMS_VIEWABLE; listPosition++) {
View listItem = listAdapter.getView(listPosition, null, this);
//now it will not throw a NPE if listItem is a ViewGroup instance
if (listItem instanceof ViewGroup) {
listItem.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
listItem.measure(widthMeasureSpec, heightMeasureSpec);
newHeight += listItem.getMeasuredHeight();
}
newHeight += getDividerHeight() * listPosition;
}
if ((heightMode == View.MeasureSpec.AT_MOST) && (newHeight > heightSize)) {
if (newHeight > heightSize) {
newHeight = heightSize;
}
}
} else {
newHeight = getMeasuredHeight();
}
setMeasuredDimension(getMeasuredWidth(), newHeight);
}
#Override
public boolean onTouch(View v, MotionEvent event) {
if (getAdapter() != null && getAdapter().getCount() > MAXIMUM_LIST_ITEMS_VIEWABLE) {
if (listViewTouchAction == MotionEvent.ACTION_MOVE) {
scrollBy(0, 1);
}
}
return false;
}
MainActivity:
NestedListView list = (NestedListView)findViewById(android.R.id.list);
CustomList adapter = new CustomList(MainActivity.this, myRssFeed.getList());
adapter.addAll();
list.setAdapter(adapter);
custom_listview_item:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="2dp"
app:cardElevation="2dp"
android:layout_marginBottom="#dimen/cardMarginVertical"
android:layout_marginLeft="#dimen/cardMarginHorizontal"
android:layout_marginRight="#dimen/cardMarginHorizontal"
android:layout_marginTop="#dimen/cardMarginVertical"
app:cardPreventCornerOverlap="false"
app:contentPadding="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#drawable/listview_item_background"
android:id="#+id/ln">
<ImageView
android:layout_width="match_parent"
android:layout_height="170dp"
android:background="#d6d6d6"
android:contentDescription="#null"
android:id="#+id/image"
android:scaleType="centerCrop" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingRight="16dp"
android:paddingLeft="16dp"
android:layout_marginBottom="8dp"
android:textSize="18sp"
android:fontFamily="sans-serif"
tools:ignore="HardcodedText,UnusedAttribute"
android:id="#+id/title"/>
<Button
android:layout_width="match_parent"
android:layout_height="36dp"
android:layout_marginTop="16dp"
android:paddingEnd="6dp"
android:paddingRight="6dp"
android:background="#drawable/listview_item_background"
android:text="#string/condividi"
android:textStyle="bold"
android:textSize="16sp"
android:textColor="#color/material_text_dialogs"
android:gravity="center|center_vertical|center_horizontal"
android:stateListAnimator="#null"
tools:ignore="RtlHardcoded,RtlSymmetry,UnusedAttribute"
android:id="#+id/condividi"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</FrameLayout>
CustomList class:
public class CustomList extends ArrayAdapter<RSSItem> {
private static Activity context = null;
private final List<RSSItem> web;
public CustomList(Activity context, List<RSSItem> web) {
super(context, R.layout.new_listview, web);
CustomList.context = context;
this.web = web;
}
#Override
public View getView(final int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
#SuppressLint({"ViewHolder", "InflateParams"}) final View rowView = inflater.inflate(R.layout.new_listview, null, true);
ImageView imageView = (ImageView)rowView.findViewById(R.id.image);
Picasso.with(context).load(web.get(position).getImage()).into(imageView);
TextView textView = (TextView)rowView.findViewById(R.id.title);
textView.setText(Html.fromHtml(web.get(position).getTitle()));
LinearLayout linearLayout = (LinearLayout)rowView.findViewById(R.id.notizia);
linearLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String url = web.get(position).getLink();
if (!url.startsWith("http://") && !url.startsWith("https://"))
url = "http://" + url;
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
String title = "Completa azione usando:";
Intent chooser = Intent.createChooser(browserIntent, title);
context.startActivity(chooser);
}
});
Button button = (Button)rowView.findViewById(R.id.condividi);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
return rowView;
}
}
That's how I display the ImageView.
Could you help me please?
I managed to fix the issue without an extra layout in my case.:
<CardView ...>
<RelativeLayout
...
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackground">
<ImageView .../>
</RelativeLayout>
</CardView>
Few days back I was also facing same issue. I solved my issue by wrapping ImageView inside FrameLayout and applying android:foreground property on FrameLayout. You also have to set android:clickable property of FrameLayout to true or should set onClickListener to FrameLayout.
Please check my answer in following thread in following link.
Let me know if it helps.
i have customlistview.i successfully added footerView in my listview.this is a source
void loadDataLastTransactions(int limit) {
footerView = getActivity().getLayoutInflater().
inflate(R.layout.listview_footer_layout, null);
if (cashTransactionlist.size() <= limit) {
transactionAdapter = new TransactionAdapter(getActivity(), cashTransactionlist);
} else {
List<Transaction> sixTramsactions = cashTransactionlist.subList(0, limit);
transactionAdapter = new TransactionAdapter(getActivity(), sixTramsactions);
}
transactionsList.addFooterView(footerView, null, false);
transactionsList.setAdapter(transactionAdapter);
}
this is a my footerView's xml code
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="7dip"
android:paddingBottom="7dip"
android:orientation="vertical"
android:layout_gravity="center_horizontal"
android:gravity="center_vertical|left"
>
<LinearLayout
android:id="#+id/footer_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/u_transaction_view_all"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="#color/u_blue"
android:textSize="#dimen/u_common_text_size"
android:text="#string/u_transaction_view_all"
android:padding="8dp"
/>
</LinearLayout>
</LinearLayout>
my footerView's xml looks like a pic
this is a my mainactivity.xml code
<LinearLayout 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="match_parent"
android:background="#f2f2f2"
android:id="#+id/dashboard_main_layout"
android:clickable="true"
android:orientation="vertical"
android:layout_gravity="center_horizontal"
android:gravity="center_vertical|left"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:orientation="vertical">
<LinearLayout
android:id="#+id/u_dashboard_expandable_container"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#d9d9d9"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<GridView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="3"
android:stretchMode="columnWidth"
android:id="#+id/dashboard_header_gridview" />
<View
android:layout_width="match_parent"
android:background="#ffffff"
android:layout_height="match_parent"
android:layout_marginLeft="-1dp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#d9d9d9"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="center_horizontal"
android:gravity="center_vertical|left">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#d9d9d9"/>
<LinearLayout
android:id="#+id/fragments_transaction_list_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/fragments_cardview"
android:orientation="vertical"
android:layout_gravity="center_horizontal"
android:gravity="center_vertical|left"
>
<android.custom.CustomListview
android:id="#+id/u_transaction_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animationCache="false"
android:cacheColorHint="#00000000"
android:clipToPadding="false"
android:divider="#null"
android:dividerHeight="0dp"
android:listSelector="#00000000"
android:scrollbars="none"
android:scrollingCache="false"
android:smoothScrollbar="true" >
</android.custom.CustomListview>
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
public class CustomListview extends ListView implements View.OnTouchListener, OnScrollListener {
private int listViewTouchAction;
private static final int MAXIMUM_LIST_ITEMS_VIEWABLE = 99;
public CustomListview(Context context, AttributeSet attrs) {
super(context, attrs);
listViewTouchAction = -1;
setOnScrollListener(this);
setOnTouchListener(this);
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if (getAdapter() != null && getAdapter().getCount() > MAXIMUM_LIST_ITEMS_VIEWABLE) {
if (listViewTouchAction == MotionEvent.ACTION_MOVE) {
scrollBy(0, -1);
}
}
}
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int newHeight = 0;
final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
if (heightMode != MeasureSpec.EXACTLY) {
ListAdapter listAdapter = getAdapter();
if (listAdapter != null && !listAdapter.isEmpty()) {
int listPosition = 0;
for (listPosition = 0; listPosition < listAdapter.getCount()
&& listPosition < MAXIMUM_LIST_ITEMS_VIEWABLE; listPosition++) {
View listItem = listAdapter.getView(listPosition, null, this);
if (listItem instanceof ViewGroup) {
listItem.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
listItem.measure(widthMeasureSpec, heightMeasureSpec);
newHeight += listItem.getMeasuredHeight();
}
newHeight += getDividerHeight() * listPosition;
}
if ((heightMode == MeasureSpec.AT_MOST) && (newHeight > heightSize)) {
if (newHeight > heightSize) {
newHeight = heightSize;
}
}
} else {
newHeight = getMeasuredHeight();
}
setMeasuredDimension(getMeasuredWidth(), newHeight);
}
#Override
public boolean onTouch(View v, MotionEvent event) {
if (getAdapter() != null && getAdapter().getCount() > MAXIMUM_LIST_ITEMS_VIEWABLE) {
if (listViewTouchAction == MotionEvent.ACTION_MOVE) {
scrollBy(0, 1);
}
}
return false;
}
}
when i run my app my footer view is left possition.in my option i have problem in activity_main.xml file because footerView's xml is correct.what is a wrong if anyone knows solution please help me
thanks everyone
Property layout_gravity affects an element aligning it relative to its parent, while gravity property aligns child views of that view. Example:
If you apply
android:gravity="center"
android:layout_gravity="left"
to a TextView it means that TextView container will be aligned left and the text inside will be centered.
Catch 22:
When you add your footer layout to ListView, it may have a RelativeLayout parent, and RelativeLayout does not support the layout_gravity property. In that case, fix would be to add:
layout_centerHorizontal="true"
layout_alignParentBottom="true"
to the root of your footer layout.
Hi I am stuck with the issue of hiding relative layout when scrolling a list view.
Required:
When i scroll list view , relative layout should be invisible
when i didnt scroll relative layout should be visible
Function.java
lv.setOnScrollListener(new OnScrollListener() {
#Override
public void onScrollStateChanged(AbsListView view, int scrollState)
{
// TODO Auto-generated method stub
// rel.setVisibility(View.GONE);
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem,int visibleItemCount, int totalItemCount)
{
rel.setVisibility(View.INVISIBLE);
if(firstVisibleItem == 0)
{
}
else
{
rel.setVisibility(View.VISIBLE);
}
}
});
mylayout.xml
<?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="fill_parent" >
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#b5b5b5"
android:dividerHeight="1dp" />
<LinearLayout
android:id="#+id/myrelative"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#FF0000"
android:orientation="horizontal"
>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
</RelativeLayout>
please guide me to solve this problem
Try This
private int mLastFirstVisibleItem;
private OnScrollListener lead_scrolllist = new OnScrollListener()
{
#Override
public void onScrollStateChanged(AbsListView view, int scrollState)
{
if (view.getId() == LeadDetail_listview.getId())
{
final int currentFirstVisibleItem = LeadDetail_listview.getFirstVisiblePosition();
if (currentFirstVisibleItem > mLastFirstVisibleItem)
{
Linear_leadfooter.setVisibility(View.GONE);
}
else if (currentFirstVisibleItem < mLastFirstVisibleItem)
{
Linear_leadfooter.setVisibility(View.VISIBLE);
}
mLastFirstVisibleItem = currentFirstVisibleItem;
}
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount)
{
}
};