ListView inside a ScrollView in Android - android

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.

Related

List view displaying only one record when Android scroll view is used

I am new to android and I am displaying a ListView getting data from a webservice. Below listview there are some other views/controls on the activity.Now the issue I am getting is that when the webservice returns more than one record controls below the listview are getting hidden at the bottom of window for that I tried to use scrollView in my android activity but the issue I am getting after using scroll view is that only one record is getting displayed in the listview. So the problem is 2 way :
when I implement listview with other controls at the bottom of it I am not able to scroll when listview displays multiple records.
when I use scrollView with and put the listview and other controls inside scrollview , listview displays only one record.
My xml for this is like this:
<LinearLayout 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="kanix.highrise.com.highrise.generate_material_requisition">
<!-- TODO: Update blank fragment layout -->
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/lstTaskQuantity"
android:layout_weight="1" >
</ListView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<TextView
android:id="#+id/txtLddsdfabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="16dp"
android:text="Req. From :"
android:layout_weight="1"
android:textColor="#fff" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="date"
android:id="#+id/etDate"
android:layout_weight=".5"
android:hint="DD/MM/YYYY"/>
<ImageButton
android:layout_width="35dp"
android:layout_height="35dp"
android:id="#+id/btnimgcal"
android:src="#drawable/calender"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<TextView
android:id="#+id/txtLddsdfadsbel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="16dp"
android:text="For Days :"
android:layout_weight="1"
android:textColor="#fff" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:text="0"
android:id="#+id/editText"
android:layout_weight="1.69" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<TextView
android:id="#+id/txtLddsddfgfadsbel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="16dp"
android:text="Quantity :"
android:layout_weight="1"
android:textColor="#fff" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:text="0"
android:singleLine="true"
android:width="70dp"
android:id="#+id/etQuantity"
android:layout_weight="1.60" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_gravity="center"
android:orientation="vertical"
>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#0080FF"
android:background="#fff"
android:text="Generate Requisition"
android:id="#+id/btnSave" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
You shouldn't put a listview in scrollview if still you want to place then set the height of listview programatically with this method.
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = listView.getPaddingTop()
+ listView.getPaddingBottom();
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
if (listItem instanceof ViewGroup) {
listItem.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight
+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
}

How to avoid ListView put rows with TextView transparent

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.

Two listview inside scrollView

I have two listview. i need to scroll one listview as the continuation of another list view. In between the list there should be a text view. if i use the two listview and textview inside a scrollview icould not able to scroll both the list. if i use the listview without scrollview i can see the two listview but scrolling is not continues.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FAF0BE" >
<Button
android:id="#+id/btnheader"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="#A1CAF1"
android:text="DisoderDetail" />
<TextView
android:id="#+id/txtTmtDnam"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/btnheader"
android:layout_marginLeft="#dimen/marginLLeft"
android:layout_marginTop="#dimen/marginTopp"
android:text="jgjggjh"
android:textColor="#000000" />
<TextView
android:id="#+id/txtdisDdtail"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/txtTmtDnam"
android:layout_marginLeft="#dimen/marginLLeft"
android:layout_marginTop="#dimen/marginTopp"
android:text="jgjggjh hjhhhjh jhjhh"
android:textColor="#000000" />
<RelativeLayout
android:id="#+id/Rvlytt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/lyt2"
android:layout_below="#id/txtdisDdtail"
android:layout_centerHorizontal="true"
android:layout_marginBottom="#dimen/marginBottomm"
android:layout_marginTop="#dimen/marginTop"
android:orientation="horizontal" >
<ImageView
android:id="#+id/starimg1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp"
android:onClick="star1Clicked"
android:src="#drawable/starunselected"
android:tag="1" />
<ImageView
android:id="#+id/starimg2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="#+id/starimg1"
android:onClick="star2Clicked"
android:src="#drawable/starunselected"
android:tag="2" />
<ImageView
android:id="#+id/starimg3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="#+id/starimg2"
android:onClick="star3Clicked"
android:src="#drawable/starunselected"
android:tag="3" />
<ImageView
android:id="#+id/starimg4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="#+id/starimg3"
android:onClick="star4Clicked"
android:src="#drawable/starunselected"
android:tag="4" />
</RelativeLayout>
<ScrollView
android:id="#+id/Scrlvw"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_below="#id/Rvlytt2" >
<RelativeLayout
android:id="#+id/rvl1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/txtDcmt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="#dimen/marginTop"
android:text="comment"
android:textColor="#000000" />
<ListView
android:id="#+id/lstvwDis11"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/txtDcmt" >
</ListView>
</RelativeLayout>
<RelativeLayout
android:id="#+id/rvl2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#id/rvl1"
>
<TextView
android:id="#+id/txtDrate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#id/lstvwDis11"
android:layout_marginTop="#dimen/marginTop"
android:text="Rate"
android:textColor="#000000" />
<ListView
android:id="#+id/lstvwDis12"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/txtDrate" >
</ListView>
</RelativeLayout>
</ScrollView>
<RelativeLayout
android:id="#+id/Rvlt22"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<EditText
android:id="#+id/Edtcmmt"
android:layout_width="250dp"
android:layout_height="50dp"
android:layout_marginLeft="5dp"
android:background="#ffffff"
android:hint="Add a comment"
android:textColor="#000000" />
<Button
android:id="#+id/btnpost"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/Edtcmmt"
android:onClick="OnPostbtnclick"
android:text="Post" />
</RelativeLayout>
</RelativeLayout>
This is not correct design for starters but if you want to stick with the design one quick fix that will make your code work is to put both of your listViews inside one more list view and put that under scroll view. that should solve your problem of scrolling.
When placing ListView inside ScrollView I use the following method to set listView height and the scrolling works properly. Use this after you've added all views to your listview.
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom();
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
if (listItem instanceof ViewGroup) {
listItem.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
}
}

Set gravity in list item layout

I have a problem with setting gravity of a list item layout. I have a list layout which has 2 textviews. The first one has right gravity and second one has left gravity. When I load list view I always get right gravity.
Here is my code:
<?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" >
<TextView
android:id="#+id/poem_verse_list_item_01"
style="#style/TextAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:gravity="right" />
<TextView
android:id="#+id/poem_verse_list_item_02"
style="#style/TextAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:gravity="left" />
</LinearLayout>
And here is my Cursor and its ViewBinder:
public class CustomViewBinder implements ViewBinder {
#Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
int getIndex = cursor.getColumnIndex(ClubCP.COLUMN_POSITION);
int position = cursor.getInt(getIndex);
// Log.i("ViewBinder position",position);
TextView right =(TextView)view;
int viewId = view.getId();
switch(position){
case 0 :
// Right = 0,
if(viewId==R.id.poem_verse_list_item_02){
view.setVisibility(view.GONE);
}
else if(viewId==R.id.poem_verse_list_item_01){
view.setVisibility(view.VISIBLE);
}
//left.setVisibility(left.GONE);
//right.setGravity(Gravity.RIGHT);
// text.setVisibility(text.GONE);
// return true;
case 1:
// Left = 1,
if(viewId==R.id.poem_verse_list_item_02){
view.setVisibility(view.GONE);
}
else if(viewId==R.id.poem_verse_list_item_01){
view.setVisibility(view.VISIBLE);
}
//left.setVisibility(left.GONE);
//right.setGravity(Gravity.LEFT);
// return true;
case 2:
case 3:
case 4:
case -1:
}
return false;
}
}
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.poem_verse_list_item, c,
columns, new int[] {R.id.poem_verse_list_item_01,R.id.poem_verse_list_item_02}, 0);
I tested a lot but each time I get only right or left gravity for both of them... I hope some one can help me. Thanks for your time.
This is my activity including ListView layout:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/FrameLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff64512f"
android:paddingBottom="0dp"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
tools:context=".Read_peom" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="#drawable/list_top_repeat" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:scaleType="fitXY"
android:src="#drawable/list_bottom_repeat" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:scaleType="fitXY"
android:src="#drawable/list_left_repeat" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"
android:scaleType="fitXY"
android:src="#drawable/list_right_repeat" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="#drawable/list_top_left_no_repeat" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:src="#drawable/list_top_right_no_repeat" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:src="#drawable/list_bottom_left_no_repeat" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:src="#drawable/list_bottom_right_no_repeat" />
<TextView
android:id="#+id/txt_poem_container_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<ListView
android:id="#+id/list_poem_container"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal|center"
android:layout_margin="#dimen/list_top_margin"
android:choiceMode="none"
android:clickable="false"
android:longClickable="false"
android:padding="5dp" >
</ListView>
Remove android:layout_gravity from both the TextViews since android:layout_width="match_parent"
OR
make android:layout_width="wrap_content" and remove android:gravity from both the TextViews.
ELSE
You may also try a RelativeLayout with TextViews having android:layout_width="wrap_content" and android:layout_alignParentLeft or android:layout_alignParentRight set to true accordingly.
Edit:
To debug the issue do the following. Create a ArrayAdapter with static data and set it as the list adapter.
String[] arr = {"a","b","c"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.poem_verse_list_item, R.id.poem_verse_list_item_01, arr);
setListAdapter(adapter);
}
Then set some value using android:text to the TextViews like this.
<TextView
android:id="#+id/poem_verse_list_item_01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:gravity="right"
android:text="poem_verse_list_item_01" />
<TextView
android:id="#+id/poem_verse_list_item_02"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:gravity="left"
android:text="poem_verse_list_item_02"/>
So if the problem persists then the issue is not with the view binder. I would suggest you to redesign your main layout file containing the ListView.
Try change android:orientation="vertical"
to
android:orientation="horizontal"
Hope this helped you

Disable reloading items in listview upon removing view from its parent view group

I have a layout with a header panel and a listview.
Here is the 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"
android:id="#+id/mainFragmentLayout">
<RelativeLayout
android:id="#+id/header"
android:background="#drawable/upper_panel"
android:layout_height="40dp"
android:layout_width="fill_parent">
<TextView android:id="#+id/news_label"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerVertical="true"
android:gravity="left"
android:text="#string/news_label"
android:textSize="15sp"
android:textStyle="bold"
android:layout_marginLeft="54dp"
android:textColor="#color/white"
/>
<Button android:id="#+id/refreshButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#drawable/btm_refresh"
android:layout_centerVertical="true"
android:layout_marginRight="8dp"
/>
</RelativeLayout>
<ListView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="120"
android:id="#+id/feedMeMore"
android:fastScrollEnabled="false"
android:layout_gravity="center"/>
In onTouchListener I remove and add RelativeLayot header.
public boolean onTouch(View view, MotionEvent motionEvent) {
int action=motionEvent.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
startY = motionEvent.getY();
break;
case MotionEvent.ACTION_MOVE:
float nowY = motionEvent.getY();
if (startY - nowY > 10)
{
if (mainLayout.indexOfChild(headerView) >= 0)
mainLayout.removeView(headerView);
}
else if (startY - nowY < - 100)
{
if (mainLayout.indexOfChild(headerView) < 0)
mainLayout.addView(headerView, 0);
}
}
break;
}
return false;
}
When the header is removed all showed items in the listview is reloaded.
I have there an image and text, that is loaded from web and every time it reload the text and the image.
How to turn off this reloading?
According to me that is because Your headerView is aligned at the top(even though on linearlayout) and list view is below it.
So when you remove your header view it make listview changes its height, so all the views are redrawn.
As a solution you can use a dummy view behind the headerview(Need to change the parent view as RelativeLayout) whose height is as same as your original header view, and now your list view will be below that dummy header view and not the original header view.
Something like this.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/mainFragmentLayout">
<RelativeLayout
android:id="#+id/header"
android:background="#drawable/upper_panel"
android:layout_height="40dp"
android:layout_alignParentTop="true"
android:layout_width="fill_parent">
<TextView android:id="#+id/news_label"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerVertical="true"
android:gravity="left"
android:text="#string/news_label"
android:textSize="15sp"
android:textStyle="bold"
android:layout_marginLeft="54dp"
android:textColor="#color/white"
/>
<Button android:id="#+id/refreshButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#drawable/btm_refresh"
android:layout_centerVertical="true"
android:layout_marginRight="8dp"
/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/dummyHeader"
android:background="#drawable/upper_panel"
android:layout_height="40dp"
android:layout_alignParentTop="true"
android:layout_width="fill_parent"/>
<ListView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="120"
android:id="#+id/feedMeMore"
android:fastScrollEnabled="false"
android:layout_gravity="center"/>

Categories

Resources