Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I was able to solve this issue by using scroll listener on the listview. (tested on 2.1)</p> <p>Lets say for each list row I have a layout like the one below. There is a content part and a header part. It doesn't matter what view type you use for header or content. </p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:background="#FFFFFF"&gt; &lt;ImageView android:id="@+id/content" android:layout_width="fill_parent" android:layout_height="300dp" android:scaleType="centerCrop" android:src="@drawable/pic" android:background="#aaaaff" android:layout_marginTop="40dp"/&gt; &lt;TextView android:id="@+id/header" android:layout_width="fill_parent" android:layout_height="40dp" android:padding="12dp" android:text="Deneme Row" android:textColor="#000000" android:background="#99ffffff"/&gt; &lt;/RelativeLayout&gt; </code></pre> <p>The test layout for the activity is as in the following:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" &gt; &lt;ListView android:id="@+id/list" android:layout_width="fill_parent" android:layout_height="fill_parent" &gt; &lt;/ListView&gt; &lt;/LinearLayout&gt; </code></pre> <p>Finally the code for the activity is given below. Here I had to have an adapter which uses ViewHolder to store the header view and also a variable to keep track of the change in the scroll for each successive scroll events (previousTop). This is because of the fact that offsetTopAndBottom() changes offset of the view related to the previous location of it. </p> <pre><code>public class TestActivity extends Activity implements AbsListView.OnScrollListener{ ListView list; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); list = (ListView) findViewById(R.id.list); list.setAdapter(new Adapter(this)); list.setOnScrollListener(this); } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { //the listview has only few children (of course according to the height of each child) who are visible for(int i=0; i &lt; list.getChildCount(); i++){ View child = list.getChildAt(i); ViewHolder holder = (ViewHolder) child.getTag(); //if the view is the first item at the top we will do some processing if(i == 0){ boolean isAtBottom = child.getHeight() &lt;= holder.header.getBottom(); int offset = holder.previousTop - child.getTop(); if(!(isAtBottom &amp;&amp; offset &gt; 0)){ holder.previousTop = child.getTop(); holder.header.offsetTopAndBottom(offset); holder.header.invalidate(); } } //if the view is not the first item it "may" need some correction because of view re-use else if (holder.header.getTop() != 0) { int offset = -1 * holder.header.getTop(); holder.header.offsetTopAndBottom(offset); holder.previousTop = 0; holder.header.invalidate(); } } } @Override public void onScrollStateChanged(AbsListView view, int scrollState) {} private static class Adapter extends ArrayAdapter&lt;String&gt; { public Adapter(Context context) { super(context, R.layout.row, R.id.header); for(int i=0; i &lt; 50; i++){ add(Integer.toString(i)); } } @Override public View getView(int position, View convertView, ViewGroup parent) { if(convertView == null){ convertView = LayoutInflater.from(getContext()).inflate(R.layout.row, parent, false); ViewHolder holder = new ViewHolder(); holder.header = (TextView) convertView.findViewById(R.id.header); convertView.setTag(holder); } ViewHolder holder = (ViewHolder) convertView.getTag(); holder.header.setText(getItem(position)); return convertView; } } private static class ViewHolder { TextView header; int previousTop = 0; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload