I have created a custom expandable list view and a custom adapter. The list view consists in 3 XML layouts (please ignore the dumb use of table layout!):
One that defines the ExpandableListView
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ExpandableListView
android:id="#+id/expandable_list"
android:groupIndicator="#null"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#00000000" >
<!-- android:listSelector="#android:color/transparent" -->
</ExpandableListView>
</LinearLayout>
Another that defines the parent (group):
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="1"
android:paddingTop="0dip"
android:layout_gravity="center_vertical"
android:gravity="center_vertical">
<TableRow android:layout_gravity="center_vertical">
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"/>
<TableLayout
android:layout_width="wrap_content"
android:layout_height="100dip"
android:stretchColumns="1"
android:paddingTop="0dip"
android:layout_gravity="center_vertical"
android:gravity="center_vertical">
<TableRow android:layout_gravity="center_vertical">
<TextView
android:id="#+id/list_item_text_view"
android:textColor="#color/tab_sel"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_gravity="left|center_vertical"
android:gravity="center_vertical"
android:fillViewport="true"
android:textSize="17sp"
android:layout_marginLeft="10dip"
android:textStyle="bold"/>
</TableRow>
</TableLayout>
<ImageView
android:layout_width="25dip"
android:id="#+id/rightcheck"
android:layout_height="25dip"
android:layout_gravity="left|center_vertical"
android:scaleType="centerCrop"
android:background="#drawable/sectionuncheck"
android:layout_marginRight="8sp"
android:layout_marginTop="0dip"/>
</TableRow>
</TableLayout>
And the last one defines the children:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="1"
android:paddingTop="0dip"
android:layout_gravity="top">
<!-- android:background="#drawable/bar_bg" -->
<TableRow>
<ImageView
android:id="#+id/image"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_marginLeft="20dip"
android:src="#drawable/arrow"
android:layout_gravity="center_vertical"
android:scaleType="centerCrop"/>
<TableLayout
android:layout_width="wrap_content"
android:layout_height="65dp"
android:stretchColumns="1"
android:paddingTop="0dip"
android:layout_gravity="top"
android:gravity="center_vertical" >
<TableRow>
<TextView
android:id="#+id/list_item_text_child"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_gravity="left|center_vertical"
android:textSize="15sp"
android:layout_marginLeft="20dip"
android:layout_marginRight="35dip"
android:text=""
android:textColor="#003479"/>
</TableRow>
<TableRow >
<TextView
android:id="#+id/text"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_gravity="left|center_vertical"
android:textSize="15sp"
android:layout_marginLeft="20dip"
android:layout_marginRight="35dip"
android:text=""
android:textColor="#44B5E7"/>
</TableRow>
</TableLayout>
<ImageView
android:id="#+id/ivProdCheck"
android:layout_width="25dip"
android:layout_height="25dip"
android:layout_gravity="left|center_vertical"
android:scaleType="centerCrop"
android:layout_marginRight="8dp"
android:background="#drawable/check"
android:visibility="gone"/>
</TableRow>
</TableLayout>
Every time I click on a child, it gets selected or unselected, meaning that the ivProdCheck gets visible or not
if(is_preffered == 0)
{
Glob.getPreferred().add(prod_curent);
image.setVisibility(View.VISIBLE);
}
else
{
image.setVisibility(View.GONE);
}
Same thing happens in the adapter, to display the checked/uncheck items on activity start:
#Override
//in this method you must set the text to see the children on the list
public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup)
{
/*........*/
if(Glob.getPreferred() != null)
{
for (Product to_compare : Glob.getPreferred())
{
if(to_compare.id_produs == id_produs_curent)
{
image.setVisibility(View.VISIBLE);
break;
}
}
}
view.setTag(holder);
//return the entire view
return view;
}
When I click on the group/parent, I select/deselect all children and change the image according to that
if(counter != prod_in_cat)
{
for(Product p : cat_curent.getProd())
{
Glob.getPreferred().add(p);
}
imageParent.setImageResource(R.drawable.sectioncheck);
}
else
{
imageParent.setImageResource(R.drawable.sectionuncheck);
}
Problem statement
Every time a group is selected (meaning all children are selected), if I then deselect one child, I also want to change the parent image. Also, if I manually select/deselect all children in one group, I want to change the group image too. Same thing goes for onCreate().
I tried this on child click (the logs show what it's supposed to, so no error on that side):
if(counter != cat_curent.getProduse().size())
{
// Log.e("!= -> ","counter " + counter);
// Log.e("!= -> ","cat_curent" + cat_curent.getProduse().size());
imageParent.setImageResource(R.drawable.sectioncheck);
}
else
{
// Log.e("!= -> ","counter " + counter);
// Log.e("!= -> ","cat_curent" + cat_curent.getProduse().size());
imageParent.setImageResource(R.drawable.sectionuncheck);
}
and this on onCreate()
if((prod_pref > 0) && (prod_pref < prod_cat_glob))
{
Log.e("Log 4", "Categoria " + i + " Produse " + prod_pref);
imageParent.setImageResource(R.drawable.sectionuncheck); //NullPointerException here
}
else if(prod_pref == prod_cat_glob)
{
imageParent.setImageResource(R.drawable.sectioncheck); //NullPointerException here
}
and every time I get NullPointerException when I use setImageResource()
What's the catch here?
Nevermind, I got it working. The data source for my list view was always updated when I clicked a parent or child. The problem is the list wasn't refreshing. I had something similar to fix a few days ago and trying .notifyDataSetChanged() didn't work so I didn't use it for this. It turns out however, that this works. Thanks anyway #m0skit0. Still not sure however, why the NPE. I'll try to figure it out.
Related
In my listview, there is edittext field for each row. When I click "submit" button, I iterated all the rows and try to get the value from text field and edit text field. Eg, there are five rows in list view. user can see Only three rows at a time. User need to scroll to see the other rows.
When I click on "Submit" button below listview, I iterate all the rows and try to get the values. If the items is out of visible, it always return null.
Thus, how should I try to get the value of edit text field which is out of screen.
ListView Layout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="32dip"
android:orientation="horizontal" >
<TextView
android:id="#+id/txt_tv1"
android:layout_width="140dip"
android:layout_height="wrap_content"
android:gravity="left"
android:layout_gravity="center_vertical"
android:text="#string/tv1"
android:textStyle="bold"/>
<View
android:layout_width="1dip"
android:layout_height="match_parent"
android:background="#color/white" />
<TextView
android:id="#+id/txt_tv2"
android:layout_width="70dip"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_gravity="center_vertical"
android:text="#string/tv2"
android:textStyle="bold"
android:ellipsize="none"
android:scrollHorizontally="false"/>
<View
android:layout_width="1dip"
android:layout_height="match_parent"
android:background="#color/white" />
<TextView
android:id="#+id/txt_tv3"
android:layout_width="70dip"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_gravity="center_vertical"
android:text="#string/tv3"
android:textStyle="bold"
android:ellipsize="none"
android:scrollHorizontally="false"/>
</LinearLayout>
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_marginTop="#dimen/padding"
android:layout_weight="1">
</ListView>
<Button
android:id="#+id/btn_submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/submit"/>
</LinearLayout>
List View Item Layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linear_item"
android:layout_width="match_parent"
android:gravity="center_vertical"
android:orientation="horizontal" >
<TextView
android:id="#+id/txt_pck_tv1"
android:layout_width="140dip"
android:layout_height="wrap_content"
android:gravity="left"
android:text="AAAA"
android:textStyle="bold"/>
<View
android:layout_width="1dip"
android:layout_height="match_parent"
android:background="#color/white" />
<TextView
android:id="#+id/txt_pck_tv2"
android:layout_width="70dip"
android:layout_height="wrap_content"
android:gravity="right"
android:textStyle="bold"
android:textColor="#color/red"
android:text="BBB" />
<View
android:layout_width="1dip"
android:layout_height="match_parent"
android:background="#color/white" />
<EditText
android:id="#+id/txt_pck_edt1"
android:layout_width="70dip"
android:layout_height="match_parent"
android:layout_gravity="right"
android:ems="10"
android:gravity="right|center_vertical"
android:inputType="number"
android:textColor="#color/green"
android:textStyle="bold" />
</LinearLayout>
Button On Click Event
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btn_submit: {
int count = 0;
EditText mTxtEdt1;
TextView mTxtTv1;
for (int i = 0; i < mListView.getCount(); i++) {
v = mListView.getChildAt(i);
mTxtTv1 = (TextView) v.findViewById(R.id.txt_pck_tv1);
mTxtEdt1;= (EditText) v.findViewById(R.id.txt_pck_edt1);
String tvValue = mTxtTv1.getText().toString();
}
}
}
}
v = mListView.getChildAt(i); return null when the child is out of view.
So how to get the value from it.
Any Suggestion???
Well that is one thing about the ListView it only gives you the views for those which are present on the screen at current time. So you will have to save or maintain data when your views are on screen and changed rather then getting them on a button click.
For example you can use TextWatcher to listen onTextChanged event of both and save data there on the basis of listview adapter position.
Hey guys I'm newbie at Android development, and I would like to build a sub item for each listview item.
Just like this:
ListView 1
sub item 1
ListView 1
sub item 1
I though that it would be esier to create a new relative layout for each listview item, so my XML file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/wrap_layout"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:orientation="horizontal" >
<RelativeLayout
android:id="#+id/first_col"
android:layout_width="fill_parent"
android:layout_height="200dp" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="200dp"
android:layout_alignParentBottom="true"
android:orientation="vertical" >
<ImageView
android:id="#+id/bg_image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="#drawable/overlay_bg" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="9dp" >
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/current_event"
android:layout_alignParentLeft="true"
android:layout_marginBottom="6dp"
android:letterSpacing="1.2"
android:shadowColor="#color/dark_grey"
android:shadowDx="-2"
android:shadowDy="2"
android:shadowRadius="0.01"
android:textColor="#color/white"
android:textSize="22sp" />
<ImageView
android:layout_width="17dp"
android:layout_height="17dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/persons" />
<TextView
android:id="#+id/current_day"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/name"
android:layout_alignParentBottom="true"
android:text="TextView"
android:textColor="#color/grey"
android:textSize="13sp" />
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:id="#+id/second_col"
android:layout_width="fill_parent"
android:layout_marginTop="200dp"
android:layout_height="200dp" >
<TextView
android:id="#+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="15dp"
android:layout_marginTop="24dp"
android:text="TextView" />
</RelativeLayout>
</RelativeLayout>
Each item is divided by two columns, the second column should be hidden column, thats why I changed the layout height to 200dp.
And to show the full layout, I'm doing that:
public View getView(final int position, View convertView, ViewGroup parent) {
// here you let SimpleAdapter built the view normally.
final View v = super.getView(position, convertView, parent);
final RelativeLayout lebg = (RelativeLayout) v.findViewById(R.id.wrap_layout);
lebg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View b) {
lebg.getLayoutParams().height = 400;
}
});
return v;
}
I just need to change the height from 200 to 400 to show the full Listview item.
The issue:
Is not working that well because when I click at the listview it only changes the height when I scroll...
Is there any better way to do what I am trying?
Thanks.
When you change LayoutParams you have to call requestLayout to see changes:
lebg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View b) {
lebg.getLayoutParams().height = 400;
requestLayout();
}
});
I'm developing a Chat App, so I create a list of friends.
Take a look at my ListView declaration at layout.xml
<ListView
android:listSelector="#android:color/transparent"
android:id="#+id/usersList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:background="#android:color/transparent"
android:cacheColorHint="#android:color/transparent"
android:divider="#android:color/transparent"
android:dividerPadding="2dp"
android:overScrollFooter="#null"
android:scrollingCache="false"
android:transcriptMode="normal"
/>
And the behavior of this ListView is annoying.
When I run my app in a phone with Android 2.3 the TextView itens of my ListView becomes transparent just like the image below:
Sometimes when a press the itens the color of TextView back to normality.
When I run my app in a phone with Android 4.1 this problem not happens.
My user item xml:
<?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:background="#android:color/white"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="5dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<ImageView
android:layout_width="65dp"
android:layout_height="65dp"
android:layout_gravity="center"
android:layout_margin="5dp"
android:adjustViewBounds="true"
android:src="#drawable/quick_blox_user" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:orientation="vertical" >
<TextView
android:cacheColorHint="#android:color/transparent"
android:id="#+id/text_view_user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="asdasdad"
android:textColor="#color/text_gray"
android:textSize="25sp" />
<TextView
android:cacheColorHint="#android:color/transparent"
android:id="#+id/text_view_user_last_activity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="asdasdad"
android:textColor="#color/text_gray_light"
android:textSize="12sp" />
<LinearLayout
android:id="#+id/layout_pending_messages"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/button_message_pending_number"
android:focusable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:background="#color/color_new_message_title"
android:padding="2dp"
android:text="12"
android:textColor="#android:color/white"
android:textSize="24sp" />
<TextView
android:id="#+id/text_view_user_last_activity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:text="#string/pending_messages"
android:textColor="#color/color_new_message"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
My Adapter:
public class UserAdapter extends BaseCustomAdapter<UserItem> {
public UserAdapter(Activity a, List<UserItem> d) {
super(a, d);
}
#Override
public View getView(int position, View view, ViewGroup parent) {
if(view == null) {
view = LayoutInflater.from(activity).inflate( R.layout.user_quickblox_item, null);
}
UserItem user = data.get(position);
String userLogin = user.getUser().getLogin();
int index = userLogin.indexOf("#");
if(index != -1) {
userLogin = userLogin.substring(0, index);
}
int count = ChatMessageDAO.getInstance(activity).countMessagePendentBySenderId(user.getUser().getId());
View viewPendingMessages = view.findViewById(R.id.layout_pending_messages);
if(count == 0) {
viewPendingMessages.setVisibility(View.GONE);
} else {
viewPendingMessages.setVisibility(View.VISIBLE);
Button buttonMessageNumberIndicator = (Button)view.findViewById(R.id.button_message_pending_number);
buttonMessageNumberIndicator.setText(String.valueOf(count));
}
TextView textViewUser = (TextView)view.findViewById(R.id.text_view_user);
textViewUser.setText(userLogin);
TextView textViewUserLastActivity = (TextView)view.findViewById(R.id.text_view_user_last_activity);
long diff = new Date().getTime() - user.getUser().getLastRequestAt().getTime();
long diffInDays = TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS);
if(diffInDays == 0) {
long diffInHours = TimeUnit.HOURS.convert(diff, TimeUnit.MILLISECONDS);
if(diffInHours == 0) {
long diffInMinutes = TimeUnit.MINUTES.convert(diff, TimeUnit.MILLISECONDS);
if(diffInMinutes < 0) {
textViewUserLastActivity.setText("Esta online agora");
} else {
textViewUserLastActivity.setText("Entrou hoje " + diffInMinutes+ " minuto(s) atrás");
}
} else {
textViewUserLastActivity.setText("Entrou hoje " + diffInHours+ " horas atrás");
}
} else if(diffInDays == 1) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(user.getUser().getLastRequestAt());
int hour = calendar.get(Calendar.HOUR_OF_DAY);
textViewUserLastActivity.setText("Entrou ontem as " + hour + " horas");
} else {
textViewUserLastActivity.setText("Entrou " + diffInDays + " dia(s) atras");
}
return view;
}
}
How to avoid this problem?
There is a chance that the TextViews are not showing at all. not the colour is changing. you can check that by setting a background for the textviews.
A few mistakes in the item xml I would like to point out:
You have two items in the horizontal layout. the linear layout that contains the image and the linear layout that contains textview. In this case you can't have them both match_parent. the total size would be screen size * 2.
when setting weight for a linear layout in a horizontal container you set the width to 0dp.
your weight sum is 1 + 0.5 = 1.5. means that the image will reserve 2/3 of screen size and textview 1/3. looks un-logical.
These un logical values could cause different layout display for different os version.
Solution:
<?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:background="#android:color/white"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="5dp" >
<LinearLayout
**android:layout_width="0dp"**
android:layout_height="match_parent"
**android:layout_weight="1"**
android:orientation="vertical" >
<ImageView
android:layout_width="65dp"
android:layout_height="65dp"
android:layout_gravity="center"
android:layout_margin="5dp"
android:adjustViewBounds="true"
android:src="#drawable/quick_blox_user" />
</LinearLayout>
<LinearLayout
**android:layout_width="0dp"**
android:layout_height="match_parent"
**android:layout_weight="2"**
android:orientation="vertical" >
<TextView
android:cacheColorHint="#android:color/transparent"
android:id="#+id/text_view_user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="asdasdad"
android:textColor="#color/text_gray"
android:textSize="25sp" />
<TextView
android:cacheColorHint="#android:color/transparent"
android:id="#+id/text_view_user_last_activity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="asdasdad"
android:textColor="#color/text_gray_light"
android:textSize="12sp" />
<LinearLayout
android:id="#+id/layout_pending_messages"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/button_message_pending_number"
android:focusable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:background="#color/color_new_message_title"
android:padding="2dp"
android:text="12"
android:textColor="#android:color/white"
android:textSize="24sp" />
<TextView
android:id="#+id/text_view_user_last_activity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:text="#string/pending_messages"
android:textColor="#color/color_new_message"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
What solved my problem was android:background:
<LinearLayout
android:background="#android:color/white
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:orientation="vertical" >
<TextView
android:cacheColorHint="#android:color/transparent"
android:id="#+id/text_view_user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="asdasdad"
android:textColor="#color/text_gray"
android:textSize="25sp" />
<TextView
android:cacheColorHint="#android:color/transparent"
android:id="#+id/text_view_user_last_activity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="asdasdad"
android:textColor="#color/text_gray_light"
android:textSize="12sp" />
At my LinearLayout which has the TextViews at my user_item.xml(The layout of itens of the List). I put background color as White. Its strange because the main ViewGroup of this layout is already set with background color White. Now My itens appears normally.
An Annoying solution for an Annoying problem.
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;
}
}
I have researched a lot regarding this topic and I'm finally posting this question cause I did not find what I searched for.
I have a layout with Listview inside a ScrollView.
Listview is used to display the comments from various users.
The layout is as follows :
Layout.xml :
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/panel_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff" >
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:scaleType="centerCrop"
android:src="#drawable/image" />
<View
android:id="#+id/image_divider"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_below="#+id/image"
android:background="#c2c2c2" />
<TextView
android:id="#+id/prod_name_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/image_divider"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:gravity="left"
android:text="SUV car by Nissaan"
android:textAlignment="gravity"
android:textColor="#000000"
android:textStyle="bold" />
<TextView
android:id="#+id/productCost_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/prod_name_text"
android:layout_below="#+id/prod_name_text"
android:text="$150"
android:textColor="#000000"
android:textStyle="bold" />
<TextView
android:id="#+id/likes_details_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/prod_name_text"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#+id/productCost_text"
android:text="rob + 1,124"
android:textColor="#000000" />
<ImageButton
android:id="#+id/dreamitBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/likes_details_text"
android:layout_margin="10dp"
android:background="#null"
android:src="#drawable/dreamplus" />
<ImageButton
android:id="#+id/addtoListBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/likes_details_text"
android:layout_margin="10dp"
android:layout_toRightOf="#+id/dreamitBtn"
android:background="#null"
android:src="#drawable/add_to_list_bg" />
<View
android:id="#+id/_first_divider"
android:layout_width="fill_parent"
android:layout_height="8dp"
android:layout_below="#+id/addtoListBtn"
android:background="#838B8B" />
<TabHost
android:id="#+id/tabHost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/_first_divider"
android:layout_marginTop="10dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</LinearLayout>
</TabHost>
<Button
android:id="#+id/cost"
android:layout_width="40dp"
android:layout_height="30dp"
android:layout_alignRight="#+id/image"
android:layout_alignTop="#+id/addtoListBtn"
android:layout_marginLeft="120dp"
android:layout_toRightOf="#+id/addtoListBtn"
android:background="#drawable/buy_button"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:text="60$"
android:textSize="12sp"
android:textStyle="bold" />
</RelativeLayout>
<View
android:id="#+id/second_divider"
android:layout_width="fill_parent"
android:layout_height="8dp"
android:layout_marginTop="10dp"
android:background="#838B8B" />
<RelativeLayout
android:id="#+id/relative2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF" >
<TextView
android:id="#+id/comments_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/upper_divider"
android:layout_alignParentLeft="true"
android:text="1"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="#+id/comments_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#+id/comments_count"
android:text="Comment(s)"
android:textSize="20sp"
android:textStyle="bold" />
<View
android:id="#+id/upper_divider"
android:layout_width="fill_parent"
android:layout_height="0.5dp"
android:layout_alignBottom="#id/comments_text"
android:background="#000000" />
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/upper_divider"
>
</ListView>
<View
android:id="#+id/lower_divider"
android:layout_width="fill_parent"
android:layout_height="0.5dp"
android:layout_alignBottom="#id/list"
android:background="#000000" />
<EditText
android:id="#+id/comments_edit"
android:layout_width="220dp"
android:layout_height="fill_parent"
android:layout_below="#+id/lower_divider"
android:background="#FFFFFF"
android:hint="Enter your comment"
android:paddingLeft="8dp"
android:paddingTop="10dp" />
<View
android:layout_width="1dp"
android:layout_height="fill_parent"
android:layout_marginBottom="0dp"
android:background="#000000" />
<Button
android:id="#+id/post_button"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_below="#+id/lower_divider"
android:layout_toRightOf="#+id/comments_edit"
android:background="#FFFFFF"
android:text="Post" />
</RelativeLayout>
</LinearLayout>
</ScrollView>
And the layout looks like :
So the layout consists of three parts : (Thick Grey Line acts as a divider)
First Part :
Includes Webservice call for image and cost,product name and users etc.
Second Part :
Has 3 tabs where all the tabs content will be retrieved from single web service.
Third Part :
This is where the actual listview exists but it fails to show. The edit text at the bottom is actually below the listview which fails to show up. This also includes a web service call.
I have added a header to listview.
Solutions that I have tried so far :
I have used a helper class to set the height of Listview dynamically(atleast thats what I think it does) :
The class is as follows :
public class Helper {
public static void getListViewSize(ListView myListView) {
ListAdapter myListAdapter = myListView.getAdapter();
if (myListAdapter == null) {
// do nothing return null
return;
}
// set listAdapter in loop for getting final size
int totalHeight = myListView.getPaddingBottom() + myListView.getPaddingTop();
for (int size = 0; size < myListAdapter.getCount(); size++) {
View listItem = myListAdapter.getView(size, null, myListView);
if (listItem instanceof ViewGroup)
listItem.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
// setting listview item in adapter
ViewGroup.LayoutParams params = myListView.getLayoutParams();
System.out.println("List view id: "+myListView.getId());
params.height = totalHeight
+ (myListView.getDividerHeight() * (myListAdapter
.getCount() - 1));
System.out.println("The params height is " + params.height);
myListView.setLayoutParams(params);
// print height of adapter on log
Log.i("height of listItem:", String.valueOf(totalHeight));
}
}
And I have called this as
Helper.getListViewSize(MainActivity.listView);
after setting the adapter to list.
Second Solution:
listView.setOnTouchListener(new ListView.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
// Disallow ScrollView to intercept touch events.
v.getParent().requestDisallowInterceptTouchEvent(true);
break;
case MotionEvent.ACTION_UP:
// Allow ScrollView to intercept touch events.
v.getParent().requestDisallowInterceptTouchEvent(false);
break;
}
// Handle ListView touch events.
v.onTouchEvent(event);
return true;
}
});
From what I read,this syncs the scrollview according to listview.
Web Service Call :
I am calling the webservice for comments as soon as the above layout launches. I can see the web service being called as I can see the response printed in the logcat. But it does not set the listview.
Note :
I have implemented the webservice call of listview separately and it works and sets the comments to the listview. So, the real problem exists with the fact that I have used scrollview inside a listview.
Any help will be much appreciated. Thanks in advance.
ListView inside a ScrollView is not a good practice because listview has it own scrolling if it doesnt fit on the screen.